程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

The nature of Python variables is very detailed

編輯:Python

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 .

The rules of variables

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.

The nature of variables

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 :

  • identity : A unique identifier used to identify an object . By calling functions id You can get it . This identity value can be understood as the memory address of the object .
  • type : Used to indicate the types of objects that can be stored . Specific types limit what the object can hold 、 What can be done 、 The rules to follow . To view the type of an object, you can call the function type().
  • value : Object .

Define multiple variables

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 :

  • numbers: Numbers .
  • string: character string .
  • tuple: Tuples .
  • sets: aggregate .
  • dictionaries: Dictionaries .

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 .

Help functions for variable types

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 :

  • dir: Used to query all attributes of a class or type , Such as dir(list).
  • help: Used to query class or type specific description documents , Such as help(int).

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 .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved