(I'm just a brick-and-http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/: 001431611988455689d4c116b2c4ed6aec000776c00ed52000)
Now that we know how to start and exit the Python interactive environment, we can formally start writing Python code.
Before you write your code, do not use copy-paste to paste the code from the page onto your own computer. Write a program also pay attention to a feeling, you need a letter to the code to knock yourself into, in the process of tapping the code, beginners often knock the wrong code, so you need to carefully check, control, to the fastest speed master how to write the program.
In the interactive environment prompt >>>
, enter the code directly, press ENTER, you can immediately get the code execution results. Now, try the input 100+200
and see if the result is 300:
>>> 100+200300
Quite simply, any valid mathematical calculation can be calculated.
If you want Python to print out the specified text, you can use a print()
function and enclose the text you want to print in single or double quotation marks, but you can't mix single and double quotes:
>>> print(‘hello, world‘)hello, world
This text, enclosed in single or double quotation marks, is called a string in the program, and we will often encounter them in the future.
Finally, with the exit()
exit Python, our first Python program is done! The only drawback is that it's not saved and you'll need to enter the code again the next time you run it.
command-line mode and Python interaction mode
Note the distinction between command-line mode and Python interaction mode.
See similar C:\>
command-line mode that is provided in Windows:
In command-line mode, you can execute python
into the Python interactive environment or python hello.py
run a .py
file.
See >>>
is in the Python interactive environment:
In a python interactive environment, only Python code can be entered and executed immediately.
In addition, running files in command-line mode differs from running .py
python code directly in a python interactive environment. The python interactive environment automatically prints out the results of each line of Python code, but running the Python code directly does not.
For example, in the Python interactive environment, enter:
>>> 100 + 200 + 300600
You can see the results directly 600
.
However, write a calc.py
file that reads as follows:
100 + 200 + 300
Then execute in command-line mode:
C:\work>python calc.py
found no output at all.
This is normal. To output the result, you must print()
print it yourself. Make a calc.py
makeover:
print(100 + 200 + 300)
Then you can see the result:
C:\work>python calc.py600
Summary
Under the Python interactive command line, you can enter the code directly, then execute it, and get the results immediately.
Python Learning--the first Python program