One: Variable
What is a variable: A variable is an indicator (symbol) associated with an object.
The role of variables: used to bind a data object and reuse it later.
Variable naming:
1. The variable name must start with a letter or an underscore, followed by a letter or an underscore.
2. You cannot use keywords in python as variable names.
3. Variable names in python are case-sensitive.
Two: Assignment statement:
Symbol: =
Syntax:
variablename = expression
or: variablename1 = variablename2 = expression
or: variablename1, variablename2, ... = expression
or: variablename 1, variable name 2, ... = sequence
function: used to bind (or associate) a variable to an object.
Description:
1. When the variable does not exist, create the variable and bind the object.
2. When the variable exists, change the binding relationship of the variable.
3. A variable can only be bound to one object.
4. Two variables can be bound to an object at the same time
Example:
>> a = 1
>> b = 2
>> c = a + b
>> c
3
python association/binding/reference meaning
1, association/binding/reference all refer to the association between variables and an object.
2. Variables in python have no type
variable Example:
a = b = c = 100 + 200 # a,b,c同时绑定同一个对象 300
a = 1
b = 2
c = 3
#上面三个可以写为如下:
a, b, c, = 1, 2, 3
Example 2: Variable exchange exercise,
given two variables a= 100 and b= 200, how to make a and b exchange binding objects without creating a new object:
方法一:定义一个新变量:x
x = a
a = b
b = x 此时,a = 200 b = 100
方法二:
a, b = b, a 这样也可以达到交换绑定对象的效果