Python simple tutorial, python simple tutorial
Use source files
Now let's start programming again. When you learn a new programming language, the first program you write and run is usually a "Hello World" program, which has become a tradition. When you run the "Hello World" program, all it does is say "Hello World ". As Simon Cozens [1], who proposed the "Hello World" program, said: "It is a traditional spell of the god of programming and can help you better learn languages ."
Start the editor you selected, enter the following program, and save ithelloworld.py。
Example 3.2 use source files
#!/usr/bin/python
# Filename : helloworld.py
print
'Hello World'
(Source file: code/helloworld. py)
To run this program, open the shell (Linux terminal or DOS prompt), and then type the commandPythonHelloworld. py. If you use IDLE, use the menu Edit-> Run Script or the keyboard shortcut.Ctrl-F5. The output is as follows.
Output
$ python helloworld.py
Hello World
If the output you get is the same as the one shown above, congratulations! -- You have successfully run your first Python program.
In case you get an error, make sure that the program you typedAccurateAnd then run the program. Note that Python is case sensitive, that isprint
AndPrint
Different -- note that the first one is in lower casep
The last one is in uppercase.P
. In addition, make sure there are no spaces or tabs before the start character of each line-we will discuss why this is important later.
How it works
Let's think about the first two lines of this program. They are calledNote-- Any content on the right of the # symbol is annotated. Annotations are mainly used as notes provided to program readers.
Python should have at least the special form of comments in the first line. It is calledOrganization row-- The first two characters of the source file are #!, Followed by a program. This line tells your Linux/Unix system when youRunWhich interpreter should be run for your program. This will be explained in detail in the next section. Note: You can always specify the interpreter directly on the command line to run your program on any platform. Just like a commandPythonHelloworld. pySame.
Important
Rational use of comments in your program to explain some important details-this will help readers of your program easily understand what the program is doing. Remember, this reader may be you six months later!
After the annotation, a PythonStatement-- It only prints the text "Hello World ".print
It is actually an operator, and "Hello World" is called a string-don't worry, we will explain these terms in detail later.