【0805 | Day 8】Python进阶(二)
一、列表类型内置方法(list)
-
用途:多个爱好、多个武器、多种化妆品
-
定义:[ ]内可以有多个任意类型的值,逗号分隔元素
# my_boy_friend = list(['jason','tank','sean']) my_boy_friend = ['jason','tank','sean'] print(f'{my_boy_friend}') #['jason','tank','sean'] 1 = list('hello nick') print(f'{1}') #['h','e','l','l','o',' ','n','i','c','k']
-
常用操作+内置方法:优先掌握、需要掌握、其他操作
优先掌握:
-
按索引取值(正向取值+反向取值),既可存也可取
-
切片
-
长度len
-
成员运算in和not in
-
追加append
-
删除del
-
循环
1. 按索引取值(正向取值+反向取值),既可存也可取
name_list = ['nick','jason','tank','sean'] name_list[0] = 'nick young' # name_list[1000] = 'tank sb' #报错 print(f'name_list[0]: {name_list[0]}') # name_list[0] = nick young
2.切片
name_list = ['nick','jason','tank','sean'] print(f'{name_list[0:3:3]}') #注意顾头不顾尾 print(f'{name_list[0:3:-1]}') print(f'{name_list[0:3:2]}') #['nick'] #[] #['nick','tank']
3.长度
name_list = ['nick','jason','sean','egon'] print(f'len(name_list): {len(name_list)}') #4
4.成员运算in和not in
name_list = ['nick','jason','tank','sean'] print(f"{'simon' in name_list}") print(f"{'jason' in name_list}") print(f"{'maya' not in name_list}") #False #True #True
5.追加值
name_list = ['nick','jason','tank','sean'] name_list.append('simon') print(f'name_list:{name_list}') #name_list:['nick','jason','tank','sean','simon']
6.删除
name_list = ['nick','jason','tank','sean'] del name_list[2] print(f"{name_list}") # ['nick','jason','sean']
7.循环
name_list = ['nick','jason','tank','sean'] for name in name_list: print(name) #nick #jason #tank #sean
需要掌握:
-
insert
-
pop
-
remove
-
count
-
index
-
clear
-
copy
-
expend
-
reverse
-
sort
1.insert( )
name_list = ['nick','jason','tank','sean'] name_list.insert(1,'handsome') print(f"name_list:{name_list}") #name_list:['nick','handsome','jason','tank','sean']
2.pop( )
name_list = ['nick','jason','tank','sean'] print(f"{name.list.pop(1)}") print(f"{name_list}") #jason #['nick','tank','sean']
3.remove( )
name_list = ['nick','jason','tank','sean'] print(f"{name_list.remove('nick')}") print(f"{name_list}") #None #['jason','tank','sean']
4.count( )
name_list = ['nick','jason','tank','sean'] print(f"{name_list.count('nick')}") #1
5.index( )
name_list = ['nick','jason','tank','sean'] print(f"{name_list.index('nick')}") #0
6.clear( )
name_list = ['nick','jason','tank','sean'] name_list.clear() print(f"{name.list}") #[]
7.copy( )
name_list = ['nick','jason','tank','sean'] print(f"{name_list.copy()}") #['nick','jason','tank','sean']
8.extend( )
name_list = ['nick','jason','tank','sean'] name_list2 = ['nick young'] name_list.extend(name_list2) print(f"{name_list}") #['nick','jason','tank','sean','nick young']
9.reverse( )
name_list = ['nick','jason','tank','sean'] name_list.reverse() print(f"{name_list}") #['sean','tank','jason','nick']
10.sort( ) 注意:sort列表的元素必须是同一类型的
name_list = ['nick','jason','tank','sean'] name_list.sort() print(f"{name_list}") name_list.sort(reverse=True) print(f"{name_list}") #['jason','nick','sean','tank'] #['tank','sean','nick','jason']
-
存一个值 or 多个值: 多个值
-
有序 or 无序:有序
-
可变 or 不可变:可变数据类型
hobby_list = ['read','run','girl'] print(f'first:{id(hobby_list)}') hobby_list[2] = '' print(f'second:{id(hobby_list)') #first:587887567432 #second:587887567432
元组类型内置方法
一、元组类型内置方法(tuple)
-
用途:多个装备、多个化妆品、多门课程
-
定义:在()内可以有多个任意类型的值,逗号分隔元素
my_boy_friend = ('jason','tank','sean') print(f'{my_boy_friend}') #('jason','tank','sean') name_str = ('egon') #(只是普通包含的意思) name_tuple = ('egon',) print(f'type(name_str):{type(name_str)}') print(f'type(name_tuple):{type(name_tuple)}') #type(name_str): <class 'str'> #type(name_tuple): <class 'tuple'>
-
常用操作+内置方法:优先掌握、需要掌握和了解
优先掌握:
-
索引取值
-
切片(顾头不顾尾,步长)
-
长度len
-
成员运算in和not in
-
循环
-
count
-
index
1.索引取值
name_tuple = ('nick', 'jason', 'tank', 'sean') # name_tuple[0] = 'nick handsom' # 报错 print(f"name_tuple[0]: {name_tuple[0]}") #name_tuple[0]: nick
2.切片
name_tuple = ('nick', 'jason', 'tank', 'sean') print(f"name_tuple[1:3:2]: {name_tuple[1:3:2]}") name_tuple[1:3:2]: ('jason',)
3.长度
name_tuple = ('nick','jason','tank','sean') print(f'{len(name_tuple)}') #4
4.成员运算
name_tuple = ('nick','jason','tank','sean') print(f'{"nick“ in name_tuple}") #True
5.循环
name_tuple = ('nick', 'jason', 'tank', 'sean') for name in name_tuple: print(name) #nick #jason #tank #sean
6.count( )
name_tuple = ('nick', 'jason', 'tank', 'sean') print(f"{name_tuple.count('nick')}") #1
7.index( )
name_tuple = ('nick', 'jason', 'tank', 'sean') print(f"{name_tuple.index('nick')}") #0
-
存一个值 or 存多个值 : 多个值
-
有序 or 无序:有序
-
可变 or 不可变:不可变
二、元组和列表的区别
列表可变的原因是:索引所对应的值的内存地址是可以改变的
元组不可变得原因是:索引所对应的值的内存地址是不可以改变的,或者反过来说,只要索引对应值的内存地址没有改变,那么元组是始终没有改变的。
注意:id(list[ ])不同,可变;而id(tuple[ ])相同,不可变。
字典类型内置方法
一、字典类型内置方法
-
用途:存多个值,以key:value的形式
-
定义:逗号隔开,以key:value的形式,value可以是任意类型数据,key通常是字符串类型,但是key必须为不可变类型
-
常用操作+内置方法
优先掌握:
-
按key存取值:可存可取
-
长度len
-
成员运算in和not in
-
删除del
-
键keys()、值values()、键值对items()
-
循环
1.按key存取值:可存可取
dic = {'a': 1, 'b': 2} print(f"first dic['a']: {dic['a']}") dic['a'] = 3 print(f"second dic['a']: {dic['a']}") #1 #3
2.长度len
dic = {'a':1,'b':2} print(f'{len(dic)}') #2
3.成员运算
dic = {'a': 1, 'b': 2} print(f"'a' in dic: {'a' in dic}") print(f"1 in dic: {1 in dic}") #True #False
4.删除
dic = {'a': 1, 'b': 2} del dic['a'] print(f"{dic.get('a')}") #None #指定元素删除 dic = {'a': 1, 'b': 2} dic.pop('a') print(f"{dic.pop('b')}") print(f"{dic.get('a')}") #2 #None #键值对删除并返回 dic = {'a': 1, 'b': 2} print(f"{dic.popitem()}") #{'b',2}
5.keys/values/items
dic = {'a': 1, 'b': 2} print(f"dic.keys(): {dic.keys()}") print(f"dic.values(): {dic.values()}") print(f"dic.items(): {dic.items()}") #dict_keys(['a', 'b']) #dict_values([1, 2]) #dict_items([('a', 1), ('b', 2)])
6.循环
dic = {'a': 1, 'b': 2, 'c': 3, 'd': 4} for k, v in dic.items(): # items可以换成keys()\values() print(k, v) #a 1 #b 2 #c 3 #d 4
需要掌握:
-
get
-
update
-
fromkeys
-
setdefault
1.get( )
dic = {'a': 1, 'b': 2} print(f"{dic.get('a')}") print(f"{dic.get('c')}") #1 #None
2.update( )
dic1 = {'a': 1, 'b': 2} dic2 = {'c': 3} dic1.update(dic2) print(f"dic1: {dic1}") #dic1: {'a': 1, 'b': 2, 'c': 3}
3.fromkeys
dic = dict.fromkeys(['name', 'age', 'sex'], None) print(f"{dic}") #dic: {'name': None, 'age': None, 'sex': None}
4.setdefault
dic = {'a': 1, 'b': 2} print(f"{dic.setdefault('a',3)}") print(f"{dic}") print(f"{dic.setdefault('c',3)}") print(f"{dic}") #1 #{'a': 1, 'b': 2} #3 #{'a': 1, 'b': 2, 'c': 3}
-
存一个值 or 多个值:多个值
-
有序 or 无序:无序
-
可变 or 不可变:可变
集合类型内置方法
一、集合类型内置方法
1.用途:用于关系运算的集合体,由于集合内的元素无序且集合元素不可重复,因此集合可以去重,但是去重后的集合会打乱原来元素的顺序
2.定义:{}内用逗号分隔开多个元素,每个元素必须是不可变类型
s = {1, 2, 1, 'a'} print(f'{s}') #{1,2,'a'} s = {1, 2, 1, 'a', 'c'} for i in s: print(i) #1 #2 #a #c s = set('hello') print(f"{s}") {'e','o','h','l'}
-
常用操作+内置方法:优先掌握,需要掌握,了解
优先掌握:
-
长度len
-
成员运算in和not in
-
|并集、union
-
&交集、intersection
-
-差集、difference
-
^对称差集、symmetric_difference
-
==
-
父集:>、>= 、issuperset
-
子集:<、<= 、issubset
1.长度
s = {1, 2, 'a'} print(f'{len(s)}') #3
2.成员运算in or not in
s = {1, 2, 'a'} print(f'{1 in s}') #True
3. 并集(|)
pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} print(f"{pythoners|linuxers}") #{'egon', 'tank', 'kevin', 'jason', 'nick', 'sean'}
4.交集(&)
pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} print(f"{pythoners&linuxers}") #{'nick'}
5.差集(-)
pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} print(f"{pythoners-linuxers}") #{'tank', 'jason', 'sean'}
6.对称差集(^)
pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} print(f"{pythoners^linuxers}") #{'egon', 'tank', 'kevin', 'jason', 'sean'}
7.==
pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} javers = {'nick', 'egon', 'kevin'} print(f"{pythoners==linuxers}") print(f"{javers==linuxers}") #False #True
8.父集(>、>=)
pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} javaers = {'jason', 'nick'} print(f"{pythoners>linuxers}") print(f"{pythoners>=linuxers}") print(f"{pythoners>=javaers}") print(f"{pythoners.issuperset(javaers)}") # False # False # True # True
9.子集(<、<=)
pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} javaers = {'jason', 'nick'} print(f"{pythoners<linuxers}") print(f"{pythoners<=linuxers}") print(f"{javaers.issubset(javaers)}") # False # False # True
需要掌握:
-
add
-
remove
-
difference_update
-
discard
-
isdisjoint
1.add( )
s = {1, 2, 'a'} s.add(3) print(s) #{1, 2, 3, 'a'}
2.remove( )
s = {1, 2, 'a'} s.remove(1) print(s) #{2, 'a'}
3.difference_update( )
pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} pythoners.difference_update(linuxers) print(f"{pythoners}") #{'tank', 'jason', 'sean'}
4.discard( )
s = {1, 2, 'a'} s.discard(3) # s.remove(3) # 报错 print(s) #{1, 2, 'a'}
5.isdisjoint( )
pythoners = {'jason', 'nick', 'tank', 'sean'} linuxers = {'nick', 'egon', 'kevin'} pythoners.isdisjoint(linuxers) print(f"{pythoners.isdisjoint(linuxers)}") # False
-
存一个值 or 多个值:多个值,值不可变
-
有序 or 无序:无序
-