您的位置:首页 > 编程语言 > Python开发

Python - 基本数据类型_Number 数字、bool 布尔、complex 复数

2021-07-18 14:55 1046 查看

Number

数字,是一个大的分类,细分四小类

  • 整数:int
  • 浮点数:float
  • 布尔:bool
  • 复数:complex

 

int 的栗子

print(type(-1))
print(type(1))
print(type(-999999999999999))
print(type(9999999999999999))

// 输出结果
<class 'int'>
<class 'int'>
<class 'int'>
<class 'int'>
  • 无论正数负数都是 int
  • 即使数字再长也还是 int,不会变成像 java 的 long

 

float 的栗子

print(type(-1.0))
print(type(1.11))
print(type(-1.11111111111111))

//输出结果
<class 'float'>
<class 'float'>
<class 'float'>

即使精度再大,也还是 float,不会像 java 分单精度、双精度

 

加法

print(type(1 + 1))
print(type(1 + 1.0))
print(type(1 + 0.0))
print(type(1 + 1.11))

# 输出结果
<class 'int'>
<class 'float'>
<class 'float'>
<class 'float'>
  • int + int = int
  • int + float = float,会自动转型为浮点数
  • float + float = float

 

减法

print(type(1 - 1))
print(type(1 - 0.0))
print(type(1 - 1.1))
print(type(2.0 - 1))

# 输出结果
<class 'int'>
<class 'float'>
<class 'float'>
<class 'float'>

和加法一个道理

 

乘法

print(type(1 * 1))
print(type(1 * 1.0))
print(type(-1 * -1.0))
print(type(2.0 * 1))

# 输出结果
<class 'int'>
<class 'float'>
<class 'float'>
<class 'float'> 

和加减法一个道理

 

除法

print(type(2 / 2))
print(type(2 / 1.0))
print(type(2 // 2))
print(type(2 // 1.0))

# 输出结果
<class 'float'>
<class 'float'>
<class 'int'>
<class 'float'> 

和加减乘法稍稍不一样哦,具体看下面

 

/ 和 // 的区别

  • / 除法,自动转型成浮点数
  • // 整除,只保留整数部分
print(2 / 2)
print(2 // 2)
print(1 / 2)
print(1 // 2)

# 输出结果
1.0
1
0.5
0

 

进制数

10 进制

  • 0,1,2,3,4,5,6,7,8,9
  • 满 10 进 1 位
  • 正常写的 Number 都是 10 进制

 

2 进制

  • 0,1
  • 满 2 进 1 位
# 二进制
print(0b10)  # 2^1 + 0
print(0b11)  # 2^1 +2^0
print(0b100)  # 2^2 + 0 + 0

# 输出结果
2
3
4

  

8 进制

  • 0,1,2,3,4,5,6,7
  • 满 8 进 1 位
# 八进制
print(0o1)  # 1
print(0o11)  # 8^1 + 1
print(0o117)  # 8^2 + 8^1 + 7

# 输出结果
1
9
79

 

16 进制

  • 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
  • 满 16 进 1 位
# 十六进制
print(0x1)  # 1
print(0x19)  # 16+9
print(0x2A)  # 16*2+10
print(0x9F)  # 16*9+15

# 输出结果
1
25
42
159

 

int() 转成十进制

int 可以将数字字符串和 Number 类型的值转成整数

# 转成十进制
print(0b101)
print(0o777)
print(0xBBB)
print(int(0b101))
print(int(0o777))
print(int(0xBBB))
print(int("-123"))
print(int(1.1))
print(int(1.9))

# 输出结果
5
511
3003
5
511
3003
-123
1
1
  • 不写 int() 的话,也可以将其他进制的数自动转成十进制
  • int() 能将纯整数(不能是浮点数)的字符串转成 int 类型
  • 传入浮点数不会进行四舍五入,直接取整数部分

 

bin() 其他进制数转二进制

# 转成二进制
print(bin(10))  # 10 转成 2进制
print(bin(0o7))  # 7 转成 2进制
print(bin(0xA))  # 10 转成 2进制
print(bin(0o27))  # 8*2+7 转成 2进制
print(bin(0x22E))  # 16^2*2+16*2+14 转成 2进制

# 输出结果
0b1010
0b111
0b1010
0b10111
0b1000101110

  

oct() 其他进制转成八进制

# 转成八进制
print(oct(110))
print(oct(0b100))
print(oct(0xAAA))

# 输出结果
0o156
0o4
0o5252

  

hex() 其他进制转成十六进制

# 转成十六进制
print(hex(110))
print(hex(0b100))
print(hex(0o777))

# 输出结果
0x6e
0x4
0x1ff

 

bool

布尔类型

  • 真:True
  • 假:False 
# 打印 bool 和 type
print(True)
print(False)
print(type(True))
print(type(False))

# 输出结果
True
False
<class 'bool'>
<class 'bool'>

注意不是 true 和 false哦

 

为什么说 bool 属于 Number 的一种呢?

# 可以将它转成 int 呢?
print(int(True))
print(int(False))

# 输出结果
1
0

因为 int 能讲 bool 转成整型,True 就是 1,False 就是 0

 

那只有 1 和 0 能表示 True 和 False吗?

并不是

Number 

# 数字
print(bool(1))
print(bool(1.1))
print(bool(-1))
print(bool(0))

# 输出结果
True
True
True
False

 

字符串

# 字符串
print(bool("123"))
print(bool(""))
print(bool("  "))
print(bool("\n"))

# 输出结果
True
False
True
True

 

列表

# 列表
print(bool([1, 1]))
print(bool([]))

# 输出结果
True
False

 

元组

# 元组
print(bool((1, 1)))
print(bool(()))

# 输出结果
True
False

 

set

# set
print(bool({1, 1, 1}))
print(bool({}))

# 输出结果
True
False

  

None

# None
print(bool(None))

# 输出结果
False

  

总结

无论什么数据类型,主要是空值就会为 False,非空就是 True

 

复数

  • 36j,直接在数字后面加 j
  • 用的比较少,不写了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: