This article mainly analyzes Python variable details and instance-related information in depth, which has some reference value. if you are interested, you can refer to it in Python, equal Sign = is a value assignment statement that can assign any data type to a variable. the same variable can be assigned multiple times and can be of different types, for example:
A = 123 # a is an integer
Print
A = 'imooc '# a is a string
Print
This variable is a dynamic language and corresponds to a static language.
The static language must specify the variable type when defining the variable. if the type does not match during the assignment, an error is returned. For example, if Java is a static language, the value assignment statement is as follows (// represents a comment ):
Int a = 123; // a is an integer variable.
A = "mooc"; // error: the string cannot be assigned to an integer variable.
This is why dynamic languages are more flexible than static languages.
Do not equate the equals sign of the value assignment statement with the equals sign of mathematics. For example, the following code:
X = 10
X = x + 2
If you understand x = x + 2 in mathematics, it is not true in any case. in the program, the value assignment statement calculates the expression x + 2 on the right. expected result 12 is displayed, then assign the variable x. Since the value before x is 10, after the value is re-assigned, the value of x is changed to 12.
Finally, it is important to understand the representation of variables in computer memory. When we write: a = 'abc', the Python interpreter does two things:
1. a 'ABC' string is created in the memory;
2. create a variable named a in the memory and point it to 'ABC '.
You can also assign a value to variable a to 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 = 'XYZ'
Print B
In the last row, whether the content of variable B is 'ABC' or 'XYZ '? In a mathematical sense, B and A are the same, and it should be 'XYZ', but in fact the value of B is 'ABC ', let's execute the code line by line to see what happened:
Run a = 'abc'. the interpreter creates the string 'ABC' and variable a, and points a to 'ABC ':
The above is a detailed explanation of Python variables and the details of the instance. For more information, see other related articles in the first PHP community!