Python标准数据类型-数字

Python标准数据类型

Python 定义了一些标准类型,用于存储各种类型的数据。

  • Numbers(数字)
  • String(字符串)
  • List(列表)
  • Tuple(元组)
  • Dictionary(字典)
  • set(集合)
  • None
  • 布尔

Numbers(数字)

  • 数字数据类型用于存储数值
  • 他们是不可变的数据类型,改变数字值会重新分配新的内存
  • 使用del语句可删除对象的引用
code:
a = 2
print(id(a))
a = 3
print(id(a))
del a
print(id(a))

result:
8791391298224
8791391298256
Traceback (most recent call last):
  File "G:/python_practice/day2.py", line 56, in <module>
    print(id(a))
NameError: name 'a' is not defined

数字类型

  • int(有符号整型)
  • float(浮点型), 科学计数法是float类型
  • complex(复数), a + bj 或者 complex(a, b)
code:
a, b, c = 1, 3.14, 1 + 2j
print(a, b, c)
print(type(a), type(b), type(c))

result:
1 3.14 (1+2j)
<class 'int'> <class 'float'> <class 'complex'>
  • 指数法表示为float
code:
a, b, c, d = 1000, 1e3, 0x54, complex(1, 5)
print(a, b, c, d)
print(type(a), type(b), type(c), type(d))

result:
1000 1000.0 84 (1+5j)
<class 'int'> <class 'float'> <class 'int'> <class 'complex'>
  • 各进制表示
code:
a, b, c, d = 10, 0O10, 0x10, 0b10
print(a, b, c, d)
print(type(a), type(b), type(c), type(d))

result:
10 8 16 2
<class 'int'> <class 'int'> <class 'int'> <class 'int'>
  • 复数的实部和虚部为float类型
code:
a = 3 + 5j
print(a.real, a.imag)
print(type(a.real), type(a.imag))

result:
3.0 5.0
<class 'float'> <class 'float'>
  • 复数的常用函数 a.real, a.imag, a.conjugate

备注:部分内容参考www.runoob.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值