In this section, let's learn how to write a simple Python program.
We know that many programming languages begin by learning how to Output "Hello, World," right? So, now let's learn how to use Python to output "Hello, world."
Interactive mode
Enter Python interactive mode, input (note >>> is a python command prompt, not part of your input):
Print ('Hello, world! ')
You can get the following output:
Hello, world!.
' Hello, world! ' is a python string, in Python, the string is enclosed in single quotation marks (') or double quotation marks ("). Print () is a Python function for printing information.
As I said before, the value of an expression in Python is entered directly into an expression:
>>> 1 + 12
You can actually use print (), with no print () equivalent:
Print (1 + 1)2
In the print (1 + 1) statement, Python first calculates that the sum is equal to 2 and then 2 is output. Visible print () either prints a string or prints a number. Note that print () prints not "1 + 1", but the result of 1 + 1 2. It is also not prudent to say that print () prints an expression, because Python calculates the value of the expression first, and then prints the value out with print ().
In fact, to output "Hello, world!", you can also write this directly:
' Hello, world!. '
But the resulting output is this:
' Hello, world!. '
When you output a string, use print () to output the contents of the string, without using the contents of the string and the quotation marks on both sides.
Running Source code files
Run the Python program, you can enter the code in Python interactive mode, or you can write the code first, save it in a. py file, and then run the file.
Create a. py file, open it with a text editor, and enter the following code:
Print ('Hello, world! ')
Note there is no Python command prompt, because it is not in interactive mode.
Save the file, and then double-click Run. You'll notice that the window flashed and exited. Why is that? This is because the time to output a sentence is very short, you have not responded to print out.
To pause a program, you can use the Python input () function. We will discuss this function in detail later. Now just know that input () will wait for input until you press ENTER.
Input ()
So the whole program is:
Print ('Hello, world! ' ) input ()
You don't have a question right now, do you?
Finally, pay attention to a problem. In Python interactive mode, you can enter an expression directly:
>>> 12 + 3446
But what about the Python source code file? We can try it:
+ + +input ()
You will find no output! Therefore, only in Python interactive mode can you directly enter an expression for calculation, in the. py file, use print () to output.
Finally, there is only one line of output in our program, how can we print multiple lines? The answer is, so you can use multiple print ():
Print ('Hello, world! ' )print('Hello, python! ')
Output:
Hello, world!. Hello, python!.
There are, of course, more ways to discuss it later. When print () finishes printing, a newline character is printed by default.
Comments
There is one last simple concept called annotation (comment). Many programming languages support annotations, and the role of annotations is to interpret the code, which the compiler/interpreter ignores. Comments in Python begin with # and end with a newline character:
# This is a comment
You can use annotations in your code:
Print ('Hello, world! ') # displays ' Hello, world! ' on the console Print (+ +) # Calculate + Display the result
In Python interactive mode, annotations can also be used:
>>> 1 + 2 + 3 # calculate 1 + 2 + 36
Summary
1. Use print () for printing information.
2. In Python interactive mode, using print () prints the string differently than the direct input string.
3. Python interactive mode can be entered directly into the expression for evaluation,. py file will not work.
4. Use input () to pause the program until you enter a line break.
5. Python comments begin with #.
Practice
1. In Python interactive mode and in the. py file, output the following information:
I Love Python.
Did you?
Python Tutorials (2.1)--The first Python program