From scratch to learn Python Series - Lecture 2: First Python Program

In the previous lesson we have learned the language Python and Python programming environment required to run the installation, then we can write Python programs, do not know is not can not wait. First, we tell you what needs to be written in Python programs.

Authoring tools code

Interactive environment

We open the Windows "command prompt" tool, enter the command pythonand then press Enter to get into Python's interactive environment. The so-called interactive environment that we enter a carriage return line of code, the code will be executed immediately, if the output code is the result, then the result will be displayed in the window. E.g:

Python 3.7.6
Type "help", "copyright", "credits" or "license" for more information.
>>> 2 * 3
6
>>> 2 + 3
5
Tips : Use macOS system users need to open the "terminal" tool, enter python3 into the interactive environment.

If you want to exit the interactive environment, you can be entered in an interactive environment quit(), as follows.

>>> quit()

Text Editor - Visual Studio Code

Visual Studio Code (commonly referred to as VS Code) was developed by a Microsoft code editor artifact that can run on Windows, Linux and other operating systems macOS. It supports syntax highlighting, auto-completion, multi-point editing, debugging run a series of convenient features, and can support a variety of programming languages. At this stage, I strongly recommend that you use VS Code to write Python code. To download, install and use on the VS Code, I recommend reading the name on know almost "VScode installation" article. FIG VS Code using the user interface.

Integrated Development Environment - PyCharm

If you use Python to develop commercial projects, we recommend you use professional tools PyCharm. PyCharm Czech JetBrains is developed for Python development integrated development environment (IDE), the so-called Integrated Development Environment said that this tool is provided to write code, run and debug the code, code analysis, code version control and other functions, therefore particularly suitable for the development of commercial projects. In JetBrains official website provides on PyCharm the download link , where Community Edition (Community) is free but the function is relatively weak, Pro (Professional) is very powerful, but requires an annual or monthly fee to use, new users can try 30 days. PyCharm on how to use Python to develop, we have to explain to everyone in subsequent lessons.

hello, world

According to industry practice, we first learn any program written in a programming language is output hello, world, because this code is the great Dennis Ritchie (the inventor of the C language) in his monumental book The C Programming Language the first piece of code written.

print('hello, world')

Run the program

VS Code suggest that you use to write the above code, after editing the code will save the code to the user's home directory, we recommend that you name the file hello.py. The user's home directory is the user login name with the same name used by the system folder with you. For example: when you log on to Windows systems use a named Administratoruser, then the user's home directory name also Administrator. For Windows users, you can set the display the user's home directory on your desktop, so you can easily find this folder; if there is no user's home directory on the desktop, can be found in the C drive called the name Usersor 用户under the folder, your the user's home directory in this folder.

After saving up your code, you can open the "command prompt" tool. Command-line tool will usually prompt the user's home directory as the default working directory. This time you can type a dircommand and see if you can see just saved hello.pythe file, if the file is found, you can execute the following command, see on the screen is not output hello, world.

python hello.py
Again : If you use macOS system, need python3 hello.py to run the program commands.

You can try the above procedures single quotes hello, worldreplaced by other content; you can also try to write a few print(preferably one per line yo) and see what happens.

Comment your code

Note is an important part of the programming language used to explain the role of the code in the source code to enhance readability. Of course, we can also source code snippet no need to run through to remove comments, so when you need to remove the comment symbol code on it. Simply put, comments will make the code easier to understand but will not affect the results of the implementation of the program .

  1. Single-line comments - in the beginning of the space portion and #, comment out an entire line.
  2. Multi-line comments - three at the beginning of quotation marks, three closing quotes, commonly used to add a large section of instructional content.
"""
第一个Python程序 - hello, world
向伟大的Dennis M. Ritchie先生致敬

Version: 0.1
Author: 骆昊
"""
print('hello, world')
# print("你好,世界!")

to sum up

Here we have the first Python program up and running, is not it great sense of accomplishment? As long as you stick to continue, and then learn a few lessons, I can take you to make games, write beautiful reptiles, develop micro-channel robots. Write program itself is a cool thing , he might just as English in the future, a lot of people need to master a skill .

Tips : If you think this column is not bad, we must remember to praise collection point yo!
发布了348 篇原创文章 · 获赞 235 · 访问量 70万+

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/104927406