Three characteristic variables Python

For each variable, python provides three methods were acquired three characteristic variables, wherein the built-in function python id (), the memory address is not the same, the id () after the result of printing is not the same, because each of the variables has its own memory address value, the id is used to reflect the position of the variable value in memory, different memory address is different id.

x = 10
print(x)  # 获取变量的变量值
print(id(x))  # 获取变量的id,可以理解成变量在内存中的地址
print(type(x))  # 获取变量的数据类型,下章会详细介绍数据类型

First, the print (a)

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
x = 10
print(x)  # 获取变量的变量值
10

Second, the values ​​are equal, the variable is determined with (ii)

name1 = 'egon'
name2 = 'nick'
print(name1 == name2)  # False
False

Third, the variable is determined whether the same id (III)

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
x = 11
y = x
z = 11
print(x == y)  # True
True
print(x is y)  # True
True
print(x is z)  # True,整数池的原因
True
x = 257
z = 257

print(x is z)  # False
False

As can be seen from the above print messages: variable id equal, equal to a certain value, pointing to the same memory address; value equal to the variable, id not necessarily equal.

In which the first print print(x is z)time of just talking about the last chapter triggered integer pool. This can be understood as a mechanism to optimize the python, the value itself is not 11, and because of our fast once again use the 11, again due to the application memory space required computer overhead, so let python x and z point to the same 11. Because the deposit is not an end, to take is the purpose of such optimization case does not affect the operation of the program.

Guess you like

Origin blog.51cto.com/14246112/2430264