In Python, the concept of variables is basically the same as the equation variables in junior high school algebra.
For example, for the equation y=x*x
, x
is the variable. When x=2
, the calculation result is 4
, and when x=5
, the calculation result is 25
.
It’s just that in computer programs, variables can not only be numbers, but also any data type.
In Python programs, variables are represented by a variable name. The variable name must be a combination of uppercase and lowercase English, numbers and underscores (_), and cannot start with a number, for example:
a = 1 变量a是一个整数。
t_007 = 'T007' 变量t_007是一个字符串。
In Python, the equal sign =
is an assignment statement. Any data type can be assigned to a variable. The same variable can be assigned repeatedly, and it can be a variable of different types, for example:
a = 123 # a是整数
print a
a = 'imooc' # a变为字符串
print a
This kind of language whose variable type is not fixed is called a dynamic language, and the corresponding language is static language.
Static language must specify the variable type when defining a variable. If the type does not match when assigning a value, an error will be reported. For example, Java is a static language, and the assignment statement is as follows (// represents comment ):
int a = 123; // a是整数类型变量
a = "mooc"; // 错误:不能把字符串赋给整型变量
Compared with static languages, dynamic languages are more flexible, for this reason.
Please do not equate the equal sign of the assignment statement with the mathematical equal sign. For example, the following code:
x = 10
x = x + 2
If you understand x = x + 2 mathematically, it is not true anyway. In the program, the assignment statement first calculates the expression on the right x + 2. Get the result 12 and assign it to variable x. Since the previous value of x was 10, after reassignment, the value of x becomes 12.
Finally, it is also important to understand how variables are represented in computer memory. When we write: a = 'ABC'
, the Python interpreter does two things:
1. Creates a 'ABC'
in memory String;
2. Create a variable named a
in the memory and point it to 'ABC'
.
You can also assign a variable a to another variable b. This operation actually points variable b to the data pointed to by variable a. For example, the following code:
a = 'ABC'
b = a
a = 'XYZ'
print b
The last line prints Is the content of variable b 'ABC' or 'XYZ'? If you understand it in a mathematical sense, you will mistakenly conclude that b and a are the same and should also be 'XYZ', but in fact the value of b is 'ABC'. Let us execute the code line by line and you can see what happens. What happened:
Executed a = 'ABC'
, the interpreter created the string 'ABC' and the variable a, and pointed a to 'ABC':
Execution b = a
, the interpreter creates variable b and points b to the string 'ABC' pointed to by a:
Executiona = 'XYZ'
, the interpreter creates the string 'XYZ' and changes the pointer of a to 'XYZ', but b does not change:
So, the final result of printing variable b
is naturally 'ABC'
.
The above is the detailed content of Getting Started with Python: Variables. For more information, please follow other related articles on the PHP Chinese website!