Published 2021-11-20

Numbers - 数字类型

Numbers 类型

python 有 4 种数字类型

Boolean

布尔值,只有 True 和 False 两个值,可以用来表示真假。

a=True
b=False

在数学运算种,布尔值的值只有 0 和 1,0 表示 False,1 表示 True。

c=4+True #5
d=False
if d==0:
    print('d is zero')
else:
    print('d is not zero')

Integer

a = 37
b = -299392993727716627377128481812241231
c = 0x7fa8      # Hexadecimal
d = 0o253       # Octal
e = 0b10001111  # Binary

Float

>>> a = 2.1 + 4.2
>>> a == 6.3
False
>>> a
6.300000000000001
>>>

比较 Comparisons

python 有如下比较运算符:

x < y      Less than
x <= y     Less than or equal
x > y      Greater than
x >= y     Greater than or equal
x == y     Equal to
x != y     Not equal to

#布尔值操作
if b >= a and b <= c:
    print('b is between a and c')

if not (b < a or b > c):
    print('b is still between a and c')

数字转换

使用类型名转换数字,如下:

a = int(3.14)
b = float(3)