This article mainly describes how to define a
python variables,
variablesThe concept is basically consistent with the equation variables of the junior algebra, just in the computer program,
variablesNot only can it be a number, it can also be
any data type。
Variables are represented by a variable name in the program, and the variable name must be a combination of uppercase and lowercase English, numeric, and _, and cannot start with a number, such as:
A = 1
Variable a is an integer.
t_007 = ' T007 '
The variable t_007 is a string.
Answer = True
The variable Answer is a Boolean value of True.
In Python, equals = is an assignment statement that can assign any data type to a variable, the same variable can be repeatedly assigned, and can be a variable of different types, for example:
#-*-Coding:utf-8-*-a = 123 # A is an integer print (a) a = ' ABC ' # a changed to string print (a)
This type of variable itself is called Dynamic language , which corresponds to static language . Static languages must specify the variable type when defining the variable, and if the type does not match, an error is given. For example, Java is a static language, and assignment statements are as follows (//for comments):
int a = 123; A is an integer type variable a = "ABC"; Error: Cannot assign string to integer variable
This is why dynamic languages are more flexible than static languages.
Do not equate an equal sign of an assignment statement with a mathematical equal sign. For example, the following code:
x = 10x = x + 2
If mathematically understood x = x + 2 that is not true anyway, in the program, the assignment statement first calculates the right expression x + 2, gets the result, and assigns it to the variable x. Since the value before x is re-assigned, the value ofx is changed to a number of ten.
Finally, it is important to understand the representation of variables in computer memory. When we write:
A = ' ABC '
, the Python interpreter did two things:
1. Create a string of ' ABC ' in memory;
2. Create a variable named a in memory and point it to ' ABC '.