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