Python entry (3): variable and python entry variable

Source: Internet
Author: User

Python entry (3): variable and python entry variable

In Python, the concept of variables is basically the same as that of the equation variables of junior high school algebra.

For exampley=x*x ,xIs a variable. Whenx=2The calculation result is4, Whenx=5The calculation result is25.

In computer programs, variables can be numbers or any data type.

In a Python program, a variable is represented by a variable name. The variable name must be a combination of uppercase and lowercase English letters, numbers, and underscores (_), and cannot start with a number. For example:

A = 1 variableaIs an integer.
T_007 = 't007 'variablet_007Is a string.

In Python, equal sign=It is a value assignment statement that can assign values of any data type to variables. The same variable can be assigned multiple times and can be of different types, for example:

A = 123 # a is an integer
print a
A = 'imooc '# a is a string
print a

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. Create'ABC'String;

2. CreateaAnd point it'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
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:

Runa = 'ABC', The interpreter creates the string 'abc' and variable a, and points a to 'abc ':

 

Runb = aThe interpreter creates variable B and points B to the string 'abc' pointed to by ':

 

Runa = 'XYZ', The interpreter created the string 'xyz' and changed the direction of a to 'xyz', But B has not changed:

 

So, finally print the variablebThe result is naturally'ABC'.

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.