What is a variable in Python define a string in Python

In Python, the concept of variables is basically the same as the equation variables of junior high school algebra.
For example, for the equation  y=x*x  , x is the variable. When x=2 , the calculation result is 4 , and when x=5 , the calculation result is 25 .
It's just that in a computer program, variables can be not just numbers, but arbitrary data types.
In a Python program, a variable is represented by a variable name. The variable name must be a combination of uppercase and lowercase English, numbers, and underscores (_), and cannot start with numbers.
E.g :
In Python, the equal sign = is an assignment statement , which can assign any data type to a variable. The same variable can be assigned repeatedly, and it can be a variable of different types, for example:

 The language in which the type of the variable itself is not fixed is called a dynamic language , and the corresponding one is a static language .

Static languages ​​must specify the variable type when defining a variable. If the type does not match when assigning, an error will be reported. For example, Java is a static language, and the assignment statement is as follows (// indicates a comment):
This is why dynamic languages ​​are more flexible than static languages.
If you understand x = x + 2 mathematically, it is not true anyway. In the program, the assignment statement first evaluates the expression x + 2 on the right side, gets the result 12, and then assigns it 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. Created a string of 'ABC' in memory;
  2. Creates a variable named a in memory and points it to 'ABC'.
  3. You can also 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:

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 wrongly conclude that b is the same as a and should be 'XYZ', but in fact the value of b is 'ABC', let's execute the code line by line, and you can see what happens what happened:
  • Executing a = 'ABC' , the interpreter creates the string 'ABC' and the variable a, and points a to 'ABC':

  • When b = a is executed , the interpreter creates the variable b and points b to the string 'ABC' pointed to by a:

  • When a = 'XYZ' is executed , the interpreter creates the string 'XYZ' and changes the pointer of a to 'XYZ', but b does not change:

  • Therefore, the final result of printing the variable b is naturally 'ABC' .

 

What if the string contains both ' and "?

 

At this time, it is necessary to " escape " some special characters of the string , and the Python string is escaped with \.

 to represent a string

Since ' and " will cause ambiguity, we insert a \ in front of it to indicate that this is a common character and does not represent the beginning of a string. Therefore, this string can be expressed as

 

If you haven't enjoyed it yet, please read    http://note.youdao.com/noteshare?id=0354468bbf9b4e35cd74ebecb2425b92&sub=458714F2E2A548F994430D219492188C

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325126481&siteId=291194637