Python学习笔记 2

第二篇

# bool型
a=True
print(a)
b=a+1
print(b)# bool型为整形的一种,true=1,False=0

# None型
c=None
print(c)

# 对象有id,type,value三个属性

# type( )用来检查值的类型
# 有返回值<class '类型'>
# python是一门强类型的语言,对象一旦创建类型不可修改
a='123'
b=123
d = type( a )
print( d )# <class 'str'>
print ( type( b ) )# <class 'int'>

# id( )
# id是由解析器生成的,在Cpython中id就是对象的内存地址
# 对象一但创建,id永远不能改变
print(id(d))

# vlaue( )就是对象中储存的具体的数据

## 变量和对象
# 对象并没有直接存储到变量中,在Python中变量更像是给对象起了一个别名
# 变量名与对象id(内存地址)相对应
# 变量中保存的对象只有在重新赋值时才会改变,变量之间相互独立,互不影响
q=10
print(id(q))
w=q
print(id(w))

q=5
print(id(q))# q对应的id改变
print(id(w))# w对应的id不会改变

q = w时:

name(变量) id(存储地址)
q 1756128496
w 1756128496

q = 5时:

name(变量) id(存储地址)
q 1756128416
w 1756128496
  			———————————————————————————————————————————————————————————————
# 类型转换 	int( ) float( ) str( ) bool( )
# 有返回值为转换后类型的值
# 类型转换是将一个类型的对象转换为其他对象
# 类型转换不是改变自身的类型,而是将对象的值转换为新的对象
t = True
print( t , type( t ))
t = int( t )
print( t , type( t ))

t=''
t = bool( t )
print( t , type( t ))

猜你喜欢

转载自blog.csdn.net/weixin_44011689/article/details/88934933