What is variable in python

What is a variable in Python

In Python, the concept of variables is basically the same as the equation variables of junior middle algebra.

For example, for an equation  y=x*x , it xis a variable. When x=2, the calculation result is 4, when x=5, the calculation result is 25.

Just in a computer program, variables can be not only numbers, but also arbitrary data types.

In Python programs, variables are represented by a variable name. The variable name must be a combination of uppercase and lowercase English, numbers, and underscores (_), and cannot begin with a number , such as:

a = 1

The variable ais an integer.

T_007 = 'T007'

The variable t_007is a string.

In Python, the equal sign =is an assignment statement that assigns any data type to a variable. The same variable can be assigned repeatedly, and can be different types of variables, for example:

a = 123 # a is an integer
Print a
a = 'imooc' # a becomes a string
Print a

A language in which the variable itself is not fixed is called a dynamic language, and corresponds to a static language.

Static languages must specify the variable type when defining a variable. If the type does not match when assigned, an error will be reported. For example, Java is a static language, and the assignment statement is as follows (// indicates a comment):

Int a = 123; // a is an integer type variable
a = "mooc"; // Error: Cannot assign a string to an integer variable

This is the reason why dynamic languages are more flexible than static languages.

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 doesn’t work anyway. In the program, the assignment statement first evaluates the expression x + 2 on the right, and gets the result 12 and assigns it to the variable x. Since the value before x is 10, the value of x becomes 12 after reassignment.

Finally, it is important to understand the representation of variables in computer memory. When we wrote: The a = 'ABC'Python interpreter did two things:

1. Created a 'ABC'string in memory ;

2. Create a avariable named in memory and point it to it 'ABC'.

It is also possible to assign a variable a to another variable b. This operation actually points the variable b to the data pointed to by the variable a, such as the following code:

a = 'ABC'
b = a
a = 'XYZ'
Print b

The last line prints out whether the content of variable b is ‘ABC’ or ‘XYZ’? If you understand it in a mathematical sense, you will mistakenly say that b and a are the same, and should be ‘XYZ’, but the value of b is actually ‘ABC’. Let us execute the code line by line, you can see exactly what happened. What happened:

Execution a = 'ABC', the interpreter creates the string ‘ABC’ and the variable a, and points a to ‘ABC’:

Execution b = a, the interpreter creates the variable b and points b to the string ‘ABC’ pointed to by a:

Execution a = 'XYZ', the interpreter creates the string ‘XYZ’ and changes the pointing of a to ‘XYZ’, but b does not change:

So, bthe result of the last print variable is natural 'ABC'.

Leave a Reply

Your email address will not be published. Required fields are marked *