Python3的基本数据类型之数字

Number:数字的四个子类型
整数:int
浮点数:float(无单双精度之分)
bool 布尔类型:表示真、假
complex 复数 标题

>>> 1
1
>>> type(1)
<class 'int'>
>>> type(-1)
<class 'int'>
>>> type(1.1)
<class 'float'>
>>> type(1.11111111111111111111)
<class 'float'>
>>> 1+0.1
1.1
>>> type(1+0.1)
<class 'float'>
>>> type(1+1)
<class 'int'>
>>> type(1+1.0)
<class 'float'>
>>> 

注意/(得到float类型结果) 和// (得到整数类型的结果)的区别

>>> type(1*1)
<class 'int'>
>>> type(1*1.0)
<class 'float'>
>>> type(2/2)
<class 'float'>
>>> type(2//2)
<class 'int'>
>>> 2/2
1.0
>>> 2//2
1

但是要注意/除法自动转型成float
// 可以理解成整除,结果只保留整数部分

>>> 2/2
1.0
>>> 2//2
1
>>> 1//2
0
>>> 

bool 布尔类型表示注意True,False首字母大写

>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
>>> int(True)//因此将bool类型归为数字类型
1
>>> int(False)
0
>>> bool(1)
True
>>> bool(0)
False
>>> bool(2)//注意数字中只有0表示False其他都表示True
True
>>> bool(-1)
True
>>> bool(0)
False
>>> bool(0b01)
True
>>> bool(0b0)
False
>>> bool('abc') //其余类型中空值表示False其他表示True
True
>>> bool('')
False
>>> bool([1,2,3])
True
>>> bool([])
False
>>> bool({1,1,1})
True
>>> bool({})
False
>>> bool(None)//一个特殊的也表示False
False

复数:用j表示复数

>>> 36j
36j
发布了12 篇原创文章 · 获赞 1 · 访问量 197

猜你喜欢

转载自blog.csdn.net/qq_39338091/article/details/104869695