数字
int(整型)
age = 22money = 88888888888888
long(长整型)
- python2.2起,如果整数发生溢出,python会自动将整数数据转换为long- python3以后只有int类型
# python2 64bit>>> 2**624611686018427387904>>> 2**639223372036854775808L #自动转成long#python3>>>2**1001267650600228229401496703205376
float(浮点型)
height = 1.74money = 2.3E4 # 23000.0
complex(复数)
bool 布尔
#只有两个值TrueFalse
bool运算为false的情况
- 空字典 {}
- 空列表 []
- 空元组 ()
- 空字符串 ‘’
- 数字零 0
list 列表
# 定义列表>>> names = ['zhangsan', 'lishi', 'wangwu', 'zhaoliu']# ------索引------>>> names.index('lishi')1>>> names[1]'lishi'# ------追加------->>> names.appen('tianqi')>>> names['zhangsan', 'lishi', 'wangwu', 'zhaoliu', 'tianqi']# ------插入------->>> names.insert(3, 'lishi')>>> names['zhangsan', 'lishi', 'wangwu', 'lishi', 'zhaoliu', 'tianqi']# extend>>> l1 = ['a','b','c']>>> l2 = ['c','d','e']>>> l1.extend(l2)>>> l1['a', 'b', 'c', 'c', 'd', 'e']# reverse 反转>>> ls = ['b','a','d','e','c']>>> ls.reverse()>>> ls['c','e','d','a','b']# sort 排序>>> ls.sort() #排序,ascii表排序>>> ls['a','b','c','d','e']>>> ls = [1,3,'a','b']>>> ls.sort() #error python3字符串和int不能排序# 统计某个元素个数>>> names.count('lishi')2>>>names.count('zhangsan')1# -------删除-------#pop>>> names.pop() # 删除最后一个 并返回'tianqi'>>> names.pop(1)# 删除索引为1的元素,并返回'lishi'>>> names['zhangsan', 'wangwu', 'lishi', 'zhaoliu']#remove>>> names.remove('wangwu')>>> names['zhangsan', 'lishi', 'zhaoliu']#clear 清空list>>> names.clear()>>> names[]#del>>> del(names[1])>>> names['zhangsan', 'zhaoliu']# 长度>>> len(names)2# 切片 同字符串>>> names = ['zhangsan', 'lishi', 'wangwu', 'zhaoliu']>>> names[1:3]['lishi', 'wangwu']>>> names[::2]['zhangsan', 'wangwu']
dictionary 字典
- dict是无序的
- key必须是唯一的,天生去重
# 定义字典>>> dic = {'name': '张三', 'age': 22}>>> dic{'name': '张三', 'age': 22}"""增""">>> dic['money'] = 5555>>> dic{'name': '张三', 'age': 22, 'money': 5555}"""改""">>> dic['age'] = 28>>> dic{'name': '张三', 'age': 28, 'money': 5555}>>> dic2 = {'age':30, 'nationality':'china'}# update 合并两个字典,如果元字典中已存在的key 值将被修改,原字典中key不存在则添加>>> dic.update(dic2)>>> dic{'name': '张三', 'age': 30, 'money': 5555, 'nationality': 'china'}"""查""">>> 'age' in dicTrue>>> dic['age']30>>> dic['hobby'] #获取没有的 报错Traceback (most recent call last):File "<stdin>", line 1, in <module>KeyError: 'hobby'# get>>> dic.get('money')5555>>> dic.get('hobby') #没有找到,不会报错,并可设置默认值>>> dic.get('hobby','爱好女')'爱好女'"""删除"""#pop 删除指定key的元素>>> dic.pop('money') #删除指定key 的项,并将值返回5555>>> dic{'name': '张三', 'age': 28}#popitem 随机删除>>> dic.popitem() # 随机删除('age', 28)"""其他""">>> dict = {'a':1,'b':2,'c':3,'d':4}# keys 返回list 字典所有key>>> dict.keys() # 返回所有key组成的listdict_keys(['a', 'b', 'c', 'd'])# values 返回list 字典所有value>>> dict.values() # 返回所有valuedict_values([1, 2, 3, 4])>>> dict.items() #将key 和 value 组合成元组dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4)])# 快速使用列表元素为key的字典>>> l = ['k1','k2','k3','k4']>>> d = {}>>> d2 = d.fromkeys(l, {})>>> d2{'k1': {}, 'k2': {}, 'k3': {}, 'k4': {}}
tuple 元组
不能修改的list
只有list的查询功能,不能修改、删除、添加操作
set 集合
无序 去重
关系运算
交集 intersection &
>>> s1 = {1,2,3,4,5}>>> s2 = {4,5,6,7}>>> s1.intersection(s2){4, 5}>>> s1 & s2{4, 5}
差集 difference -
>>> s1 = {1,2,3,4,5}>>> s2 = {4,5,6,7}>>> s1.difference(s2){1, 2, 3}>>> s1 - s2{1, 2, 3}>>> s2.difference(s1){6, 7}>>> s2 - s1{6, 7}
并集 union |
>>> s1 = {1,2,3,4,5}>>> s2 = {4,5,6,7}>>> s1.union(s2){1, 2, 3, 4, 5, 6, 7}>>> s1 | s2{1, 2, 3, 4, 5, 6, 7}
反向差集(对称差集) symmetric_difference ^
>>> s1 = {1,2,3,4,5}>>> s2 = {4,5,6,7}>>> s1.symmetric_difference(s2){1, 2, 3, 6, 7}>>> s1 ^ s2{1, 2, 3, 6, 7}
方法
| 方法 | 说明 |
|---|---|
| update | 合并两个集合 并赋值 |
| clear | 清空 |
| add | 增加 |
| copy | 浅拷贝 |
| difference_update | 将集合的值改为他们的差集 |
| discard | 删除 元素不存在 不会报错 |
| pop | 随机删除 |
| remove | 删除 元素不存在 会报错 |
| issubset | 判断是否是子集 < |
| issuperset | 判断是否是超集(父集) > |
| isdisjoint | 两个集合没有交集 True |
拷贝
浅拷贝

>>> l1 = [1,2,'a',[5,6,7]]>>> l2 = l1.copy()>>> l2[1] = 4>>> l1[1, 2, 'a', [5, 6, 7]]>>> l2[1, 4, 'a', [5, 6, 7]]>>> l2[3][0] = 10>>> l1[1, 2, 'a', [10, 6, 7]]>>> l2[1, 4, 'a', [10, 6, 7]]
深拷贝

>>> import copy>>> l1 = [1,2,'a',[5,6,7]]>>> l2 = copy.deepcopy(l1)>>> l2[1] = 4>>> l1[1, 2, 'a', [5, 6, 7]]>>> l2[1, 4, 'a', [5, 6, 7]]>>> l2[3][0] = 10>>> l1[1, 2, 'a', [5, 6, 7]]>>> l2[1, 4, 'a', [10, 6, 7]]
进制
- 二进制
01 - 八进制
oct() 0~7 - 十六进制
0~9ABCDEF
hex()
