Python数据类型

数据类型

  • 整数:-1,100,0,0xff00(十六进制整数)
  • 浮点数:1.23,-9.01,1.23e9,1.23-5
  • 字符串:'12c',"ab","I'm OK",'I\'m OK'
  • 布尔值:True,False(首字母大写)
  • 空值:None

整数和浮点数在计算机内部存储的方式是不同的,整数运算永远是精确的(除法难道也是精确的),而浮点数运算则可能会有四舍五入的误差。

  • 转义字符:\n,\\,\t,\',\"
  • 用r''表示''内部的字符串默认不转义
  • 用'''...'''的格式表示多行内容
1
2
3
4
>>> print(r'''hello,\n
... world''')
hello,\n
world

and,or,not

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> True and True
True
>>> True and False
False
>>> True or True
True
>>> True or False
True
>>> not True
False
>>> not False
True
>>> not 1>2
True

数据类型转换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
>>> s='123'
>>> s
'123'
>>> int(s)
123
>>> a=123
>>> a
123
>>> str(a)
'123'
>>> bool(1)
True
>>> bool('')
False