Python tutorial (2.1) -- The First Python program, python2.1

Source: Internet
Author: User

Python tutorial (2.1) -- The First Python program, python2.1

In this section, we will learn how to write a simple Python program.

 

We know that many programming languages are learning how to output "Hello, world" at the beginning, right? Now let's learn how to output "Hello, world" in Python ".

 

Interaction Mode

 

Enter the Python interaction mode and enter (note >>> as a Python command prompt, not part of the input ):

 

>>> print('Hello, world!')

The following output is displayed:

 

Hello, world!

 

'Hello, world! 'Is a Python string. in Python, strings are enclosed by single quotation marks (') or double quotation marks. Print () is a function of Python used to print information.

 

As mentioned before, when using Python to calculate the expression value, it is directly input into the expression:

 

>>> 1 + 12

 

In fact, print () can also be used, which is equivalent to no print:

 

>>> print(1 + 1)2

 

In the print (1 + 1) Statement, Python calculates 1 + 1 equals 2, and then outputs 2. Print () can print both strings and numbers. Note that print () does not print "1 + 1", but result 2 of 1 + 1. Print () can print the expression is not rigorous, because Python first calculates the value of the expression, and then outputs the value using print.

 

In fact, we need to output "Hello, world! ", You can also write it like this:

 

>>> 'Hello, world!'

 

However, the output is as follows:

 

'Hello, world!'

 

When a string is output, print () is used to output the content of the string. if not used, the content of the string and quotation marks on both sides are output.

 

Run the source code file

 

Run the Python program. You can enter the code in the Python interaction mode, or write the code first, save it in A. py file, and then run the file.

 

Create a. py file, open it in a text editor, and enter the following code:

 

print('Hello, world!')

 

Note that there is no Python command prompt, because it is not in interactive mode.

 

Save the file and double-click it. You will find that the window will pop out. Why? This is because the time for output of a sentence is very short, and it will be printed before you respond.

 

To pause a program, you can use the input () function of Python. We will discuss this function in detail later. Now you only need to know That input () will wait until you press enter.

 

input()

 

Then the whole program is:

 

print('Hello, world!')input()

 

Now there is no problem just now, right?

 

Finally, pay attention to one problem. In Python interaction mode, you can directly enter an expression:

 

>>> 12 + 3446

 

But in the Python source code file? Let's try:

 

12 + 34input()

 

You will find no output! Therefore, the input expression can be directly used for calculation only in Python interaction mode. In the. py file, use print () for 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!

 

Of course, there are more methods to be discussed later. After print () is printed, a line break is printed by default.

 

Note

 

There is also a simple concept called comment ). Many programming languages Support annotation. The function of annotation is to interpret code, and the compiler/Interpreter ignores it. In Python, annotations start with # and end with line breaks:

 

# this is a comment

 

Annotations can be used in the Code:

 

print('Hello, world!')    # displays 'Hello, world!' on the consoleprint(15 + 30)            # calculate 15 + 30 and display the result

 

In Python interaction mode, you can also use Annotations:

 

>>> 1 + 2 + 3    # calculate 1 + 2 + 36

 

Summary

 

1. print the information using print.

2. in Python interaction mode, print () is used to print strings, which is different from directly input strings.

3. In Python interaction mode, you can directly enter an expression for calculation, but not in the. py file.

4. Use input () to pause the program until the input line break.

5. Python annotations start.

 

Exercise

 

1. output the following information in the Python interaction mode and in the. py file:

 

I love Python.

Do you?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.