Readers who have studied mathematical equations know : When using equations to solve practical problems , It's often set up to x To represent a number in the application problem . It is similar in writing code , Will define a x( Or any other name ) To refer to the meaning of a number , This x Is to be a variable . in application , Variables can refer to inputs , It can also refer to the result . Once programmed , The computer will put the specific number referred to by the variable into the memory , And do the calculations .
Variables are the most basic terms in programming languages , Used of variable data stored in a computer —— Such as integers 、 decimal 、 Characters or a piece of memory called . Such as :
Val ="hello"
The code above is executed , There will be a variable in the system called Val, It points to a string stored in memory “hello” Where it is , As shown in the figure below :
chart :Python Variable
After defining variables , Variables can be operated and calculated by writing specific logic , So as to realize the desired function .
In practice , The program needs to handle different types of variables , So in Python Inside , Variables are divided into various types . such as , Integer variables can only represent integers , Floating point variables can only represent numbers with decimal points , Character variables can only represent characters .
To write Python In the process of coding , Defining variables and assigning variables must be done in the same step .Python Internally, the variable will be created according to the type of the assigned variable .
for example : Defining integer variables a be equal to 5, It can be written. a=5. At this time Python A variable of type integer will be created in memory a, And let this a The value of is 5. In the following code, we use a It is equivalent to using the numerical value 5.
Python The language can automatically create variables of corresponding types according to the assigned statements , How does it work ?
Python An object model is used internally , This object model is used to store variables and their corresponding data . stay Python In language , Variables of any type are translated into an object , This is the nature of variables .
Python The internal object model consists of 3 Part of it is made up of : identity 、 Type and value , The specific meaning is as follows :
Actually writing code , You can define multiple variables in one statement , Separate them with commas . for example , Assign values to multiple variables simultaneously in one statement , as follows :
var1,var2,var3=value1,value2,value3
At this time , The system will value1、value2、value3 The value type of , Defining the var1、var2、var3 Three variables , And assign them . for example :
var1,var2,var3 = 2,5,3 # Definition var1 by 2,var2 by 5,var3 by 3
print(" The pointer :",id(var1)," type :",type(var1)," value :",var1) # Output variables var1、var2、var3 The pointer to 、 type 、 value
print(" The pointer :",id(var2)," type :",type(var2)," value :",var2)
print(" The pointer :",id(var3)," type :",type(var3)," value :",var3)
After this program runs , The output result is :
The pointer :1650393616 type :<class 'int'> value :2
The pointer :1650393712 type :<class 'int'> value :5
The pointer :1650393648 type :<class 'int'> value :3
Different Python There are slight differences in the classification of types in versions , Here we use Python 3 Mainly . stay Python 3 in , There are six types :
among ,numbers A type is a collection of numeric types , It can be subdivided into int( integer )、float( floating-point )、bool( Boolean type )、complex( The plural ) Other types . At the same time, each type has its own characteristics and rules .
Python The supported variable types are defined as classes , Any kind of (numbers、string、list…) It's all a class . Each class has its own methods and properties , Specify the operation of this type of value through the methods and properties of the class .
Python There are two built-in help functions in :
When you don't know what type a variable belongs to , You can use the above functions to view , This will be very helpful to you .