变量的创建过程
name = "qiangzi"
>>> id(name)
19194464 #内存地址id
>>>
......
>>> name = "qiangzi"
>>> id(name)
19194464
>>> name = "daqiang"
>>> id(name)
19196576
python解释器有自动垃圾回收机制,自动隔一段时间把没有跟变量名关联的内存数回收。
变量指向关系
>>> name = "qiangzi"
>>> name1 = name
>>> id(name),id(name1)
(29483616, 29483616)
.....
>>> name = "qiangzi"
>>> name1 = name
>>> name = "强子"
>>> id(name),id(name1)
(25097712, 25092704)
>>> name,name1
('强子', 'qiangzi')
身份运算和None
>>> name = "qiangzi"
>>> age = 25
>>> name,age
('qiangzi', 25)
>>> type(name),type(age)
(<class 'str'>, <class 'int'>)
# class类别 str字符串 int整数
is is是判断两个标识符是不是引用自一个对象,如果引用一个一个对象则返回True,否则返回False
is not is not是判断两个标识符是不是引用自不同对象,如果引用不是同一对象则返回True,否则返回False。
>>> name = "qiangzi"
>>> age = 25
>>> name,age
('qiangzi', 25)
>>> type(name),type(age)
(<class 'str'>, <class 'int'>)
>>> type(name) is str
True
>>> type(age) is not int
False
None 空 Empty
三元运算
>>> a = 10
>>> b = 8
>>> if a > 15:
... c = a
... else:
... c = b
>>> print(c)
8
简化 c = 值1 if 条件A else 条件B
如果条件A成立,就去左边的 值1,则取 值2
>>> a = 10
>>> b = 8
>>> c = a if a > 15 else b
>>> c
8