- disct = {'name':'gyq','age':18,'Gender':'boy'}
- print(disct['age'])
- disct = {'name':'gyq','age':18,'Gender':'boy'}
-
- disct['id'] = 110 #默认添加到尾部
- print(disct)
- disct = {'name':'gyq','age':18,'Gender':'boy'}
-
-
- disct['name'] = 'Tom'
- print(disct)
- disct = {'name':'gyq','age':18,'Gender':'boy'}
- del disct['name']
-
- print(disct)
- disct = {'name':'gyq','age':18,'Gender':'boy'}
-
- disct.clear()
-
- print(disct) #{}
- disct = {'name':'gyq','age':18,'Gender':'boy'}
-
- print(disct['name'])
- disct = {'name':'gyq','age':18,'Gender':'boy'}
-
- print(disct.get('age'))
- disct = {'name':'gyq','age':18,'Gender':'boy'}
-
- print(disct.get('id')) #None
-
- disct = {'name':'gyq','age':18,'Gender':'boy'}
-
- print(disct.get('id',0)) #0
- disct = {'name':'gyq','age':18,'Gender':'boy'}
-
- print(disct.keys()) #dict_keys(['name', 'age', 'Gender'])
- disct = {'name':'gyq','age':18,'Gender':'boy'}
-
- print(disct.values()) #dict_values(['gyq', 18, 'boy'])
- disct = {'name':'gyq','age':18,'Gender':'boy'}
-
- print(disct.items()) #dict_items([('name', 'gyq'), ('age', 18), ('Gender', 'boy')])
- disct = {'name':'gyq','age':18,'Gender':'boy'}
-
- for key in disct.keys():
- print(key)
- disct = {'name':'gyq','age':18,'Gender':'boy'}
-
- for values in disct.values():
- print(values)
- disct = {'name':'gyq','age':18,'Gender':'boy'}
-
- for items in disct.items():
- print(items)
- disct = {'name':'gyq','age':18,'Gender':'boy'}
-
- for key,value in disct.items():
- print(f'{key} = {value}')
- s1 = {10,20,30,40}
- print(s1) #{40, 10, 20, 30}
- s2 = {10,10,20,30,40}
- print(s2) #{40, 10, 20, 30}
- s3 = set('gyq1234')
- print(s3) #{'4', '3', '2', '1', 'y', 'q', 'g'}
- s4 = set()
- print(s4) #set()
- s1 = {10}
- s1.add(20)
-
- print(s1)
- s1 = {10}
- s1.update([100,20,30])
- print(s1)
- s1 = {10,20}
- s1.remove(10)
-
- print(s1)
- s1 = {10,20}
- s1.remove(10)
-
- print(s1)
- s1 = {10,20,30,40}
- del_num = s1.pop()
-
- print(del_num)
- s1 = {10,20,30,40}
-
- print(10 in s1 ) #True
- print(200 not in s1 ) #True
print(变量1 + 变量2)print ( 变量 * 想要复制的数量 )print(元素 in 变量名)print(元素 not in 变量名)
- for sum in range(1,6,2):
- print(sum)
- list1 = ['a','b','c','d','e']
-
- for i in enumerate(list1):
- print(i)
- list1 = [10,20,30]
-
- s1 = {10,20,30}
-
- t1 = (10,20,30)
-
- print("这是列表转元组:", tuple(list1))
-
- print("这是集合转列表:",list(s1))
-
- print("这是元组转集合:",set(t1))
- list3 =[i for i in range(10)] #第一个i作为返回值,将生成的序列存储到i中,返回给变量。
- print(list3)
- list5 = [i for i in range(10) if i % 2 == 0]
- print(list5)
- list6 = [(i,j) for i in range(1,3) for j in range(3)]
- print(list6) #[(1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
- dict1 = {i:i**2 for i in range(1,5)}
- print(dict1)
- list1 = ['name','age','gender']
- list2 = ['gyq',20,'man']
-
- dict1 = {list1[i]: list2[i] for i in range(len(list1))}
- print(dict1)
如果两个列表数据个数相同,len统计任何一个列表的长度都可以如果两个列表数据个数不同,len统计数据多的列表数据个数会报错;len统计数据少的列表数据个数不会报错。
- counts = {'MBP':268,'HP':125,'DELL':201,'Lenovo':99,'acer':99}
-
- dict1 = {key:value for key, value in counts.items() if value >= 200}
- print(dict1)
- list1 = [1,1,2]
- list1 = [1,1,2]
- set1 = {i ** 2 for i in list1}
- print(set1) #{1,4}