in Python, the concept of variables is basically the same as the equation variables of the middle school algebra.
For example, for equation y=x*x , x is the variable. When x=2 , the calculation result is 4, when x=5 , the result is.
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 begin with a number
For example:
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:
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):
This is why dynamic languages are more flexible than static languages.
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:
- A string of ' ABC ' was created in memory;
- A variable named A is created in memory and points 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:
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:
- So, the result of printing variable B at last is ' ABC ' naturally.
What if the string contains both ' and contains '?
At this point, you need to " escape " some special characters of the string, and the python string is escaped with \.
to represent a string
because ' and ' can cause ambiguity, so we insert a \ in front of it to indicate that this is an ordinary character and does not represent the beginning of the string, so the string can be represented as
If the little buddy hasn't seen it, see http://note.youdao.com/noteshare?id=0354468bbf9b4e35cd74ebecb2425b92&sub=. 458714f2e2a548f994430d219492188c
What is the variable python definition string in python