The second hurdle: variable assignment - 0 introductory to advanced (with exercises) | Python basic grammar

1.  naming variables

1.1 knowledge points

(1) can only be a word;

(2) only contain letters, numbers and underscores;

(3) does not start with a number;

(4) contained in the content description data as possible;

(5) Do not use the Python function name or keyword.

For example: If the information is name, you can name the variable name; if the information is digital, then the variable name should be called number. If a word can not meet the requirements, you can also use two words underlined stitching my_teacher.

1.2 Notes

Many Meng new at the time just to get into the habit would be to the variable named a, b, c, etc. letters. Such a variable name completely not read any information, variable name once more that he fainted, chaos.

Note also point is, Python comes with the system default language keywords can not be used as a variable named.

2. Assignment

Assignment is represented by [=].

In the code of the world, the assignment symbol = is not equal to the left of the right meaning, merely represent assignment action.

Representatives about equal on both sides of the symbol, is a comparison operator ==, although They look like, but it is representative of an entirely different meaning.

3. Assignment of several ways

3.1 single assignment:

int_type = 10 # 赋值为整型变量
float_type = 10.1 # 赋值为浮点型变量
string_type = "字符串" # 赋值为字符串类型

3.2 unified assignment:

a = b = c = 10
# 相当于a = 10, b = 10, c = 10

3.3 Symmetric assignment:

a, b, c = 1, 2.2, "字符串"
# 相当于a = 1, b = 2.2, c = "字符串"

Repeat Assignment 3.4

a = 1
a = 2
print(a)
# 2
# 最后打印结果a的值为2
# 当多次赋值后,变量名会指向新的空间

Exercises

Students, to consciously practice, the answer is in the public number, number of public reply signal to [answer].

1. Will the [520] on which is more suitable for the following variables?

A. 52zero。
B. a。
C. number。
D. 数字。

2. Will [ 'class'] is more suitable on which of the following variables?

A. class。
B. banji。
C. string。
D. classes。

3. The following [about] the assignment is correct?

A. number == 520。
B. number === 520。
C. and = '和'。
D. name = '曾小贤'。

4. The following code prints the result is?

name = '大白'
age = 5
name = '曾小贤'
age = 35
print(name + '今年' + str(age) + '岁了')

5. Press the required print out this sentence "My teachers name is Li Lei, Han Meimei is the name of your teacher."

Requirement 1: the "Li Lei", "Han Meimei" variable names were named, and assigned to the variable.

Requirement 2: the "name" named variable names, and assigned to the variable.

Requirement 3: Naming variable name calling, print out this sentence.

 

 

Daily weekly share real Python code data entry, advanced materials, basic grammar, crawlers, data analysis, web sites, machine learning, deep learning and so on.


Micro-channel group (concern " Python family " together it easy to learn Python)

QQ group ( 983,031,854 )

Released six original articles · won praise 50 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_34409973/article/details/104221966