-
Python中的字典-Dicts
python中的字典是有序的、可以改变的、不允许重复值(PS:在python3.7之前,字典是无序的,而在python3.7及以后的版本中,字典是有序的)
字典用花括号{}括住,一个元素同时包含了键(key)和值(value),中间用冒号隔开
1test={"Bob":23,"Lily":22,"Amy":22}
2print(len(test))#结果为3
3print(type(test))#结果为<class 'dict'>访问字典Dicts中的元素
1#利用键Key访问值Value
2test={"Bob":23,"Lily":22,"Amy":22}
3print(test["Bob"])#结果为23
4print(test.get("Bob"))#结果为23
5test["Bob"]=24
6print(test["Bob"])#结果为24
7#用keys()获取字典中的所有键Key
8print(test.keys())#结果为dict_keys(['Bob', 'Lily', 'Amy'])
9#用values()获取字典中所有的值Value
10print(test.values())#结果为dict_values([24, 22, 22])
11#用items()获取字典中所有元素
12print(test.items())#结果为dict_items([('Bob', 24), ('Lily', 22), ('Amy', 22)])
13#检查某字符是字典中的键
14if "Amy" in test:
15 print("yes")#结果为yes更改字典Dicts中的元素
1#更改、添加特定键的值
2test={"Bob":23,"Lily":22,"Amy":22}
3test["Bob"]=24
4print(test["Bob"])#结果为24
5test["CC"]=25
6print(test)#结果为{'Bob': 24, 'Lily': 22, 'Amy': 22, 'CC': 25}
7#用update()更改或添加元素,注意用{}
8print(test)#结果为{'Bob': 24, 'Lily': 22, 'Amy': 22, 'CC': 25}
9test.update({"Bob":30})
10print(test)#结果为{'Bob': 30, 'Lily': 22, 'Amy': 22, 'CC': 25}
11test.update({"Penny":25})
12print(test)#结果为{'Bob': 30, 'Lily': 22, 'Amy': 22, 'CC': 25, 'Penny': 25}
13#用pop()删除特定键的元素
14test.pop("Bob")
15print(test)#结果为{'Lily': 22, 'Amy': 22, 'CC': 25, 'Penny': 25}
16#用popitem()删除最后一个元素
17test.popitem()
18print(test)#结果为{'Lily': 22, 'Amy': 22, 'CC': 25}
19#用del关键词删除特定键的元素
20del test["Amy"]
21print(test)#结果为{'Lily': 22, 'CC': 25}
22#用clear()清空字典
23test.clear()
24print(test)#结果为{}循环访问字典元素
1#循环访问字典的键
2test={"Bob":23,"Lily":22,"Amy":22}
3for i in test:
4 print(i)#结果为Bob Lily Amy
5for i in test.keys():
6 print(i)#结果为Bob Lily Amy
7
8#循环访问字典的值
9for i in test:
10 print(test[i])#结果为23 22 22
11for i in test.values():
12 print(i)#结果为23 22 22
13
14#循环访问字典的键和值
15for x,y in test.items():
16 print(x)#结果为Bob Lily Amy
17 print(y)#结果为23 22 22
18 print(x,y)#结果为Bob 23 Lily 22 Amy 22复制字典
dict2=dict1的方式为对dict1建立软链接,命名为dict2,任何在dict1中做的改变都会体现在dict2中,因此需要用copy()的方法来复制字典
1test={"Bob":23,"Lily":22,"Amy":22}
2#用copy()复制字典
3new=test.copy()
4print(new)#结果为{"Bob":23,"Lily":22,"Amy":22}
5#用dict()复制字典
6new=dict(test)
7print(new)#结果为{"Bob":23,"Lily":22,"Amy":22}嵌套字典,即字典中的值仍为字典
1myfamily = {"child1":{"name":"Emil","year":2004},"child2": {"name":"Tobias","year":2007},"child3":{"name":"Linus","year":2011}}
2print(myfamily)#结果为{'child1': {'name': 'Emil', 'year': 2004}, 'child2': {'name': 'Tobias', 'year': 2007}, 'child3': {'name': 'Linus', 'year': 2011}}
3
4child1 = {"name" : "Emil","year" : 2004}
5child2 = {"name" : "Tobias","year" : 2007}
6child3 = {"name" : "Linus","year" : 2011}
7myfamily = {"child1" : child1,"child2" : child2, "child3" : child3}
8print(myfamily)#结果为{'child1': {'name': 'Emil', 'year': 2004}, 'child2': {'name': 'Tobias', 'year': 2007}, 'child3': {'name': 'Linus', 'year': 2011}} -
Python中的条件语句If…Else
1#最简单的if
2a=2
3b=5
4if a<b:
5 print("a<b")#结果为a<b
6#if和elif,满足if则执行if语句,满足elif则执行elif语句,可同时执行
7if a>b:
8 print('a>b')
9elif a<b:
10 print('a<b')#结果为a<b
11#if和else,满足if则执行if语句,否则执行else语句
12if a>b:
13 print('a>b')
14else:
15 print('a<b')#结果为a<b
16
17#判断语句中的and和or
18a=2
19b=5
20c=10
21if a<c and b<c:
22 print("c is the max")#结果为c is the max
23
24#缩写版if
25a=2
26b=5
27if a<b: print("a<b")#结果为a<b,只有if的情况下,判断语句在前,执行语句在后
28print("a<b") if a<b else print("a>b")#结果为a<b,有if和else的清空下,判断语句在中间,执行语句在前后
29
30#pass语句
31a = 33
32b = 200
33if b > a:
34 pass#结果为空 -
Python中的While循环
当while之后的条件语句为真时,执行while语句
break可跳出循环
continue会结束本次循环,直接进入下次循环
当while后的条件语句为假时,可用else指定语句
1i=1
2while i<6:
3 print(i)
4 if i==3:
5 break
6 i=i+1
7#结果为1 2 3
8
9i=0
10while i<6:
11 i=i+1
12 if i==3:
13 continue
14 print(i)
15#结果为1 2 4 5 6
16
17#else用法
18i = 1
19while i < 6:
20 print(i)
21 i += 1
22else:
23 print("i is no longer less than 6")
24#结果为1 2 3 4 5 i is no longer less than 6 -
Python中的For循环
for循环可用来遍历字符串、列表、元组、集合、字典等
for循环中的break与continue用法与while中一致
1for i in range(6):
2 print(i)#结果为0 1 2 3 4 5 -
原文始发于微信公众号(BioInfo):W3Schools-Python基础4
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/238184.html