总纲
Python中的所有数据类型都是对象
按简单和复杂分类
简单类型:int、float、bool、string
复杂类型:tuple、list、dict、set按可变和不可变分类
不可变对象:int、float、bool、string、tuple
可变对象:list、dict、set序列Sequence
string、tuple、list
可以通过序号访问序列中的每一项。
序列都是有序的。
相对的,dict、set都是无序的。而且dict和set中的键必须唯一。
int整型
- 运算
// 整除
% 取余
** 乘方 - 其它数据类型转换为int
int(3.4) # 3
int(1.0e4) # 10000
int(True) # 1
int(False) # 0
int('3') # 3
int('3.4') # 出错,int不能转换float字符串。
float浮点型
# 同int类型,而且可以转换float字符串。
float('3.4') # 3.4
string字符串
- 基础
str(True) # 'True' 将bool类型转换为字符串
'hello ' * 2 # 'hello hello '
- slice操作
letters = '123456'
letters[:] # '123456'
letters[3:] # '456'
letters[:4] # '1234'
letters[2:4] # '34'
letters[1:5:2] # '24'
letters[-3:] # '456'
letters[1:-2] # '234'
letters[-4:-2] # '34'
letters[-1::-1] # '654321'
letters[::-1] # '654321'
letters[-50:] # '123456'
letters[:70] # '123456'
- split、join、replace
letters = '1,2,3,4,5,6'
my_list = letters.split(',') # ['1', '2', '3', '4', '5', '6']
','.join(my_list) # '1,2,3,4,5,6'
letters.replace(',', ';') # '1;2;3;4;5;6'
letters.replace(',', ';', 2) # '1;2;3,4,5,6'
- 查找操作
letters = '1, 2, 3, 4, 5, 6, 3, 7, 3'
letters.startswith('1,') # True
letters.endswith('6') # False
letters.find('3') # 6
letters.rfind('3') # 24
letters.count('3') # 3
len(letters) # 25
letters.isalnum() # False
- 格式化
letters = 'hello, world!...'
new = letters.strip('.') # 'hello, world!'
new.capitalize() # 'Hello, world!'
title = new.title() # 'Hello, World!'
new.upper() # 'HELLO, WORLD!'
title.lower() # 'hello, world!'
title.swapcase() # 'hELLO, wORLD!'
new.center(30) # ' hello, world! '
new.ljust(30) # 'hello, world! '
new.rjust(30) # ' hello, world!'
tuple元组
- 初始化
memeda = ()
memeda = (1,)
memeda = 1,
memeda = 1, 2, 3
- 将list转换为tuple
ori = ['a', 'b', 'c']
tuple(ori) # ('a', 'b', 'c')
- 多变量同时赋值
memeda = ('a','b','c')
a, b, c = memeda
a # 'a'
b # 'b'
c # 'c'
- 交换两个变量的值
a = 'a'
b = 'b'
a, b = b, a
a # 'b'
b # 'a'
list列表
- 增
memeda = [1,2,3]
memeda.append(4) # [1,2,3,4]
memeda.insert(2, 9) # [1,2,9,3]
memeda.insert(10, 0) # [1,2,3,0]
others = [3,4]
memeda.extend(others) # [1,2,3,3,4]
memeda += others # [1,2,3,3,4]
memeda.append(others) # [1,2,3,[3,4]]
- 删
memeda = [1,2,3]
del memeda[-1] # [1,2]
memeda.pop() # [1,2]
memeda.pop(1) # [1,3]
memeda.remove(2) # [1,3]
- 改
memeda = [3,1,2,4]
sorted(memeda) # [1,2,3,4]且原memeda不变,仍为[3,1,2,4]
memeda.sort() # [1,2,3,4]且原memeda变了。
memeda.sort(reverse=True) # [4,3,2,1]
- 查
memeda = [1,2,3]
2 in memeda # True
34 in memeda # False
memeda.index(2) # 1
len(memeda) # 3
memeda = [1,2,3,4,4,4]
memeda.count(2) # 1
memeda.count(4) # 3
a = [1,2,3]
b = a
a[1] = 'hehe'
a # [1, 'hehe', 3]
b # [1, 'hehe', 3]
a = [1,2,3]
b = a.copy()
c = list(a)
d = a[:]
a[1] = 'hehe'
a # [1, 'hehe', 3]
b # [1,2,3]
c # [1,2,3]
d # [1,2,3]
dict字典
dict的键必须是不可变对象,即tuple也能做dict的键。
houses = {
(44.79, -93.14, 285): 'My House',
(38.89, -77.03, 13): 'The White House'
}
- 其它类型转换为dict
lol = [ ['a', 'b'], ['c', 'd'], ['e', 'f'] ]
dict(lol) # {'c': 'd', 'a': 'b', 'e': 'f'}
#
lot = [ ('a', 'b'), ('c', 'd'), ('e', 'f') ]
dict(lot) # {'c': 'd', 'a': 'b', 'e': 'f'}
#
los = [ 'ab', 'cd', 'ef' ]
dict(los) # {'c': 'd', 'a': 'b', 'e': 'f'}
#
tol = ( ['a', 'b'], ['c', 'd'], ['e', 'f'] )
dict(tol) # {'c': 'd', 'a': 'b', 'e': 'f'}
#
tol = ( ('a', 'b'), ('c', 'd'), ('e', 'f') )
dict(tol) # {'c': 'd', 'a': 'b', 'e': 'f'}
#
tos = ( 'ab', 'cd', 'ef' )
dict(tos) # {'c': 'd', 'a': 'b', 'e': 'f'}
- 删
kk = {'a': 1, 'b': 2, 'c': 3}
# del
del kk['b'] # {'a': 1, 'c': 3}
# clear
kk.clear() # {}
# 直接赋值为{}
kk = {} # {}
- 改
first = {'a': 1, 'b': 2}
second = {'b': 9, 'c': 3}
first.update(second) # {'c': 3, 'a': 1, 'b': 9}
- 查
kk = {'a': 1, 'b': 2, 'c': 3}
# in
'a' in kk # True
'z' in kk # False
# get
kk.get('a') # 1
kk.get('z') # None
kk.get('z', 'nothing') # nothing
# keys() values() items()
kk.keys() # dict_keys(['a','b','c'])
list(kk.keys()) # ['a','b','c']
list(kk.values()) # [1,2,3]
list(kk.items()) # [('a',1),('b',2),('c',3)]
# copy()
save_kk = kk.copy()
save_kk['hehe'] = 'memeda'
save_kk # {'hehe': 'memeda', 'a': 1, 'b': 2, 'c': 3}
kk # {'a': 1, 'b': 2, 'c': 3}
set
# 新建一个set
empty_set = set() # 由于{}符号被用来表示创建空的dict了,所以创建空的set只能用set()了
# 将其它类型转换为set
set('letters') # {'l','e','t','r','s'}
set((1,1,3,3,5,7)) # {1,3,5,7}
set([1,1,3,3,5,7]) # {1,3,5,7}
set({'a': 1, 'b': 2, 'c': 3}) # {'a', 'c', 'b'}
even_numbers = {0, 2, 4, 6, 8} {0, 2, 4, 6, 8}
odd_numbers = {1, 3, 5, 7, 9} {1, 3, 5, 7, 9}
numbers = {
num1: {1,2,3},
num2: {2,3,4},
num3: {3,4,5},
num_odd: {1,3,5,7},
num_even: {0,2,4,6,8}
}
for name, contents in numbers.items():
if '3' in contents and not ('4' in contents or '5' in contents):
print(name)
结果:num1
等同于:
for name, contents in numbers.items():
if '3' in contents and not contents & {'4', '5'}:
print(name)
各种集合运算
a = {1, 2}
b = {2, 3}
交集
a & b {2}
a.intersection(b) {2}
并集
a | b {1, 2, 3}
a.union(b) {1, 2, 3}
前一个集合中存在,后一个集合中不存在
a - b {1}
a.difference(b) {1}
存在于任何一个集合中,但没有在两个集合中同时存在。
a ^ b {1, 3}
a.symmetric_difference(b) {1, 3}
子集测试、真子集测试
a <= b False
a.issubset(b) False
a <= a True
a.issubset(a) True
a < a False
父集测试、真父集测试
a >= b False
a.issuperset(b) False
a >= a True
a.issuperset(a) True
a > a False