Before we formally write the first Python program, let's understand what is the command line mode and Python interactive mode.
Command line mode
Select "Command Prompt" in the Windows start menu to enter the command line mode, and its prompt is similar to C:\>:
Python interactive mode
Knock on the command python in the command line mode, and you will see a bunch of text output similar to the following, and then enter the Python interactive mode, and its prompt is >>>.
Enter exit() in the Python interactive mode and press Enter to exit the Python interactive mode and return to the command line mode:
You can also directly select the Python (command line) menu item through the start menu to directly enter the Python interactive mode
However, the window will be closed directly after entering exit() and will not return to the command line mode.
Knowing how to start and exit the interactive mode of Python, we can officially start writing Python code.
Before writing the code, please do not use "copy"-"paste" to paste the code from the page to your own computer.
Writing a program also pays attention to a feeling. You need to type in the code letter by letter. In the process of typing the code, beginners often type the wrong code: wrong spelling, wrong capitalization, mixed Chinese and English punctuation, mixed spaces and Tab key,
You need to check and compare carefully to master how to write programs as quickly as possible.
At the prompt >>> in the interactive mode, directly enter the code and press Enter to get the code execution result immediately. Now, try to enter 100+200 to see if the calculation result is 300:
>>> 100+200 300
Very simple, any effective mathematical calculation can be calculated.
If you want Python to print the specified text, you can use the print() function, and then enclose the text you want to print in single or double quotes, but you cannot mix single and double quotes:
>>> print('hello, world') hello, world
This kind of text enclosed in single or double quotes is called a string in the program, and we will often encounter it in the future.
Finally, exit Python with exit(), and our first Python program is complete! The only shortcoming is that it has not been saved, and the code must be entered again next time it is run.
Command line mode and Python interactive mode
Please pay attention to distinguish between command line mode and Python interactive mode.
In the command line mode, you can execute python to enter the Python interactive environment, or execute python hello.py to run a .py file.
Execute a .py file
Execute in command line mode. If you type a command python hello.py, you will see the following error:
The error message No such file or directory means that this hello.py is in the current directory
, You must switch the current directory to the directory where hello.py is located before it can be executed normally:
In addition, there is a difference between running .py files in the command line mode and running Python code directly in the Python interactive environment. The Python interactive environment will automatically print the results of each line of Python code, but it will not be possible to run the Python code directly.
For example, in the Python interactive environment, enter:
>>> 100 + 200 + 300 600
You can see the result 600 directly.
However, write a calc.py file with the following content:
100 + 200 + 300
Then execute in command line mode:
No output was found.
this is normal. If you want to output the result, you must use print() to print it out yourself. Modify calc.py:
print(100 + 200 + 300)
Execute again, you can see the result:
C:\work>python calc.py 600
Finally, the code in the Python interactive mode is to input one line and execute one line, while running the .py file directly in the command line mode is to execute all the code in the file at one time. It can be seen that the Python interactive mode is mainly used for debugging Python code, and it is also convenient for beginners to learn. It is not an environment for officially running Python code.
Article source: Liao Xuefeng's official website