Variables for Python

Source: Internet
Author: User

Python variables

In Python, the concept of variables is basically the same as the equation variables of the middle school algebra. For example, for an equation y=x*x  , it x is a variable. When, the result of the x=2 calculation is 4 , when x=5 , the computed result is 25 .

Just in a computer program, a variable can be not only a number, but also any data type. In a python program, a variable is represented by a variable name, and the variable name must be a combination of uppercase and lowercase English, numeric, and underscore (_), and cannot start with a number, and cannot use system keywords, such as: Pythonfor example: a = 1, variable a is an integer. t_007 = ' T007 ', the variable t_007 is a string.

In Python, the equals sign = is an assignment statement that assigns 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 AA = ' 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 = 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, 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 in memory ‘ABC‘ ;

2. Create a variable named in memory a and point to it ‘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 = aa = ' 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:

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

So, the b result of the last print variable is naturally ‘ABC‘ .

Variables for Python

Related Article

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.