[The use of print function, input function, Python keywords and how to comment code] ------- PYTHON basics 2

1. Use of print() function

The print() function can print out the results and display them on the screen. It is the most commonly used function in python. One or more results can be output at a time, and the interval symbol between each output result can be set by parameter sep.

1. The print() function can only print one result at a time

for example:

print(100)

The above command prints a value, and the result output is: 100

2. The print() function can print multiple results at one time

When printing multiple results, separate each result with a comma in the function.
for example:

print(100, 60, 0)

The above command prints three values, and the result output is: 100 60 0

3. Setting of interval symbol

The interval symbol between multiple output results can be set through the sep parameter.
If the sep parameter is not set, the interval symbol between the output results is a space by default.
Note: sep is the abbreviation of the English word separation, the translation of separation is: separate, interval

The following command sets the interval symbol to comma, and the output result is: 100,60,0

print(100, 60, 0, sep=',')

The following command sets the interval symbol to underscore_, and the output result is: 100_60_0

print(100, 60, 0, sep='_')

Second, the use of input () function

The input() function is used to generate a prompt to the user, then obtain the content entered by the user, and assign the content to the specified variable.
Note: The input content is all string type,
if you need to perform numerical calculation on the input content, you need to use the int() or float() function to convert the string type to a numeric type.

1. input() function format

Variable = input('Prompt information for users to view')

2. Basic use of input() function

a = input('请输入你的信息')
print(a)

Run the program, first the screen will display a string: Please enter your information
and then you can enter any information on the screen, such as input: learning
After inputting, press the Enter key, then run the second line of the program and output the variable a The content, namely: learning

3. Application of input() function

1) input() function application - addition calculator

The following program is a simple addition calculator, please enter information according to the prompts:

print('这是一个计算两数相加的程序')
a = input('请输入第一个整数,输完后按回车键:')
b = input('请输入第二个整数,输完后按回车键:')
a = int(a)
b = int(b)
c = a + b
print(a,'+',b,'=',c)

output

这是一个计算两数相加的程序
请输入第一个整数,输完后按回车键:2
请输入第二个整数,输完后按回车键:4
2 + 4 = 6

The above program can also be written as follows, with the same result:

print('这是一个计算两数相加的程序')
a = int(input('请输入第一个整数,输完后按回车键:'))
b = int(input('请输入第二个整数,输完后按回车键:'))
print(a,'+',b,'=',a+b)

2) Input() function application - calculate the area

Prompt the user for the width and height of a rectangle, then output the area of ​​the rectangle:

print('这是一个计算长方形面积的程序')
length = float(input('请输入长方形的长,输完后按回车键:'))
width = float(input('请输入长方形的宽,输完后按回车键:'))
print('长方形的面积为:', length*width)
输出
```python3
这是一个计算长方形面积的程序
请输入长方形的长,输完后按回车键:4
请输入长方形的宽,输完后按回车键:5
长方形的面积为: 20.0

3) Input() function application—calculate area

Prompt the user to enter the distance run and the elapsed time, and then output the corresponding speed:

print('这是一个计算您速度的程序')
s = float(input('请输入您奔跑的路程(单位是米),输完后按回车键:'))
t = float(input('请输入您所用的时间(单位是秒),输完后按回车键:'))
print('您奔跑的速度是:', s/t, '(米/秒)')

output

这是一个计算您速度的程序
请输入您奔跑的路程(单位是米),输完后按回车键:6
请输入您所用的时间(单位是秒),输完后按回车键:3
您奔跑的速度是: 2.0 (/)

4. Summary

• Understand the flow of a program, mainly including input, processing, and output.
• The input() function is used to generate a prompt to the user, then obtain the user input content, and assign the content to the specified variable.
• The content input through the input() function is all string type.

3. List of python keywords

Python's standard library provides a keyword module that can output all keywords of the current version.
code show as below:

import keyword
print(keyword.kwlist)

output

['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

4. Statements and comments

1. Definition and writing method of sentences

In general, an instruction is a statement.
A statement can occupy only one line.

For example, the following program has 5 statements:

A = 10
B = 15
C = A * B
print(C)
print('你好')

A statement can occupy multiple lines, separated by backslash ().
For example, the following program consists of two statements, 1 to 4 lines are the first statement, and line 5 is the second statement.

A= 1 + 2 + \
3 + 4 \
+ 5 + 6 + 7 + \
8 + 9 + 10
print(A)

Multiple statements can be placed on the same line, separated by semicolons (;).
For example, the following one-liner consists of 4 statements:

a = 10;b = 15;c = a * b;print(c)

Note: In the program writing specification, it is recommended that a statement occupy only one line.

2. Definition and writing method of annotation

1) Single line comment:

Starting with #, the line of characters after # will not be executed by the program.
For example, the following programs are all single-line comments:

a = 10    #给变量a赋值为10
Name = '小明'     #变量Name的内容是'小明'
#这行程序不会被执行
#以下程序输出变量a的内容
print(a)

2) Multi-line comments:

Use 3 single quotes ''' or 3 double quotes """ as the beginning and end of the comment respectively.
The following program is a multi-line comment: all programs between the 3 single quotes ''' will not be executed.

a = 10    #给变量a赋值为10
Name = '小明'     #变量Name的内容是'小明'
#这行程序不会被执行
#以下程序输出变量a的内容
print(a)

Note: The quotation marks must correspond one-to-one. For example, if there are 3 single quotation marks ''' at the beginning, then there must be 3 single quotation marks """ at the end.

3. Summary

• An instruction is a statement, and it is recommended that a statement occupy only one line.
• Comments are explanations and descriptions of the code.
• There are single-line comments (#) and multi-line comments (3 single quotes ''' or 3 double quotes """)

Guess you like

Origin blog.csdn.net/weixin_47296493/article/details/130048137