int整数

整数 包含 正整数、负整数、二进制、八进制、十进制、十六进制、浮点数

1,100,-8080,0

0xff00,0xa5b4c3d2。对于很大的数,例如10000000000,很难数清楚0的个数。Python允许在数字中间以_分隔,因此,写成10_000_000_000和10000000000是完全一样的

浮点数也就是小数

浮点数可以用数学写法,如1.23,3.14,-9.01,等等。但是对于很大或很小的浮点数,就必须用科学计数法表示,把10用e替代,1.23x109就是1.23e9,或者12.3e8,0.000012可以写成1.2e-5,等等。

str字符串

布尔值

and 并且、or 或者、not运算是非运算,它是一个单目运算符,把True变成False,False变成True

  1. >>> True and True
  2. True
  3. >>> True and False
  4. False
  5. >>> False and False
  6. False
  7. >>> 5 > 3 and 3 > 1
  8. True
  1. >>> True or True
  2. True
  3. >>> True or False
  4. True
  5. >>> False or False
  6. False
  7. >>> 5 > 3 or 1 > 3
  8. True
  1. >>> not True
  2. False
  3. >>> not False
  4. True
  5. >>> not 1 > 2
  6. True

布尔值经常用在条件判断中,比如

  1. if age >= 18:
  2. print('adult')
  3. else:
  4. print('teenager')

list列表

  1. lists = ['Michael', 'Bob', 'Tracy']
  2. lists = ['python', 'java', ['asp', 'php'], 'scheme']
  3. print(len(lists))
  4. print(lists[0],lists[1],lists[-1])
  5. print(lists[0:-1]) # 等同于 print(lists)
  6. lists.append('Adam') # 向后添加数据
  7. lists.insert(1, 'Jack') # 添加到指定的位置
  8. lists[1] = 'Sarah' # 添加到指定的位置
  9. lists.pop(1) # 默认删除最后一个
  10. # 嵌套列表
  11. a = ['a', 'b', 'c']
  12. b = [1, 2, 3]
  13. dicts = [a, b]
  14. # 列表生成器
  15. [x * x for x in range(1, 11)]
  16. [x * x for x in range(1, 11) if x % 2 == 0]
  17. [m + n for m in 'ABC' for n in 'XYZ']

dict字典

dict是键:值对数据,和list比较,dict有以下几个特点:

  • 查找和插入的速度极快,不会随着key的增加而变慢;
  • 需要占用大量的内存,内存浪费多。

不允许同一个键出现两次。创建时如果同一个键被赋值两次,后一个值会被记住

  1. dicts = {'Michael': 95, 'Bob': 75, 'Tracy': 85}
  2. dicts['Adam'] = 67 #赋值
  3. dicts['cola'] = 67 #添加新键值对
  4. dicts['Adam'] = [67,68] #覆盖之前的数据为[67,68]
  5. print(dicts['Adam'])
  6. dicts.pop('Adam') #删除

字典只允许一个数据,很多时候要for循环让键对应多个值,通过以下方式实现

  1. dicts = {}
  2. for x in mycol.find():
  3. dicts.setdefault("hello","123")

set集合


瓦雀