Home  >  Article  >  Backend Development  >  Detailed explanation and examples of Python variables

Detailed explanation and examples of Python variables

高洛峰
高洛峰Original
2017-03-28 17:22:451659browse

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 is an integer
print a
a = 'imooc' # a becomes a string
print a
This kind of language whose variable type is not fixed is called a dynamic language, and its counterpart is a 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 a comment):
int a = 123; // a is an integer type variable
a = "mooc"; // Error: cannot assign a string to Integer variables
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 right side The expression x + 2 gets the result 12, which is then assigned to the 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 string of 'ABC' in memory;
2. Creates a name in memory is a variable and points 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 out the content of variable b. Is it 'ABC' or 'XYZ'? If you understand it from 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:
When a = 'ABC' is executed, the interpreter creates the string 'ABC' and the variable a, and points a to 'ABC':

Detailed explanation and examples of Python variables

Execute b = a, the interpreter creates variable b, and points b to the string 'ABC' pointed to by a:

Detailed explanation and examples of Python variables

Execute a = 'XYZ', the interpreter creates string 'XYZ', and changed the pointer of a to 'XYZ', but b has not changed:

Detailed explanation and examples of Python variables

The above is the detailed content of Detailed explanation and examples of Python variables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn