Python variable details and examples

Source: Internet
Author: User
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:
A = 123 # A is an integer
Print a
A = ' Imooc ' # A becomes a 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 = "MOOC"; 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 = 10
x = 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, obtains the result 12, and assigns the variable x. Since the value before X is 10, the value of X becomes 12 after the value is re-assigned.
Finally, it is important to understand the representation of variables in computer memory. When we wrote: 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 '.
You can also assign a variable A to another variable B, which actually points the variable B to the data that the variable a points to, such as the following code:
A = ' ABC '
b = A
A = ' XYZ '
Print B
Does the last line print out the contents of variable b exactly ' ABC ' or ' XYZ '? If you understand mathematically, you will mistakenly conclude that B and a are the same and should be ' XYZ ', but actually the value of B is ' ABC ', and let us execute the code in one line, and we can see exactly what happened:
Execute a = ' abc ', the interpreter creates the string ' abc ' and variable A, and points a to ' abc ':

Execute B = A, the interpreter creates the variable B and points B to the string ' ABC ' that points to a:

Execute a = ' xyz ', the interpreter creates the string ' xyz ' and changes the point of a to ' XYZ ', but B does not change:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.