Python数据类型之非序列数据类型
非序列数据类型即不可进行for遍历的数据类型。
- int(有符号整数),整数一般以十进制表示,但是 Python也支持八进制(0o开始)或十六进制(0x开始)来表示整数。转换方法:
- 十进制转换成二进制 bin(10)
- 十进制转换成八进制 oct(10)
- 十进制转换成十六进制 hex(10)
>>> a = 1
>>> b = -1
>>> type(a)
<class 'int'>
>>> type(b)
<class 'int'>
>>> c = 1000000000000
>>> type(c)
<class 'int'>
- float(浮点型)
>>> a = 3.1415926
>>> b = 0.12312
>>> type(a)
<class 'float'>
>>> type(b)
<class 'float'>
>>> a_float = 1.34e+3
>>> a_float
1340.0
>>> b_float = 1.34e3
>>> b_float
1340.0
>>> c_float = 1.34e-10
>>> c_float
1.34e-10
>>> c_float = 1.34e-3
>>> c_float
0.00134
- complex(复数)
>>> a_complex = 3+4j
>>> type(a_complex)
<class 'complex'>
- long(长整型),仅在Python2中有此数据类型,Python3中统一定位为整型(int)。整数的范围取决于机器是32位还是64位, 但长整数取决于虚拟内存的大小。
- bool(布尔类型),Python中数字0、空字符串、空列表、空元组为False;非空为True
>>> a = True
>>> type(a)
<class 'bool'>
>>> b = False
>>> type(b)
<class 'bool'>
>>> bool(0)
False
>>> bool(10000)
True
>>> bool("")
False
>>> bool("balabala")
True
>>> bool(())
False
>>> bool((1, 2, 3))
True
认识运算符
- 算术运算符
1). 算术运算符:+,-,*,**, /, %, //
2). 赋值运算符:=, +=, -=, /=, *=, %=
3). 关系运算符: >, >=, <, <=, !=, ==
4). 逻辑运算符:逻辑与and,逻辑或or,逻辑非not
注意:
- 关系运算符返回值一定是布尔类型
- 逻辑与and: 全真则真, 一假则假
逻辑或or: 全假则假, 一真则真
逻辑非not: 取相反 - / 和 //在python2和python3的区别有:
py2:
10 / 3 = 3 ----> int / int = int
10 / 3.0 = 3.33333333 ----> int / float = float
10 // 3 = 3 ----> 结果去掉小数
10 // 3.0 = 3.0 ----> 结果去掉小数
py3:
10 / 3 = 3.333333 ----> py3重要的不同之处