Python基础入门练习——Task05字典、集合、序列

Python基础入门练习——Task05字典、集合、序列

字典练习题

dic = {
    'python': 95,
    'java': 99,
    'c': 100
    }
len(dic) #字典长度为3
3
dic['java'] = 98
dic['php'] = 90
del dic['c']
list(dic.values())
[95, 98, 90]
list(dic.keys())
['python', 'java', 'php']
list(dic.items())
[('python', 95), ('java', 98), ('php', 90)]
'javascript' in dic
False
sum(dic.values())
283
min(dic.values())
90
max(dic.values())
98
dict1 = {'php':97}
dic.update(dict1)
dic
{'python': 95, 'java': 98, 'php': 97}

集合练习题

z = [a]
tuple(z)
({'apple'},)
b = set()
b.add('x')
b.add('y')
b.add('z')
b
{'x', 'y', 'z'}
set(['A','B','A','B'])
{'A', 'B'}
c = {6,7,8}
d = {7,8,9}
c&d
{7, 8}
c|d
{6, 7, 8, 9}
set.difference(c|d)
{6, 7, 8, 9}
d-c
{9}
c-d
{6}
c^d 
{6, 9}
e = {'A','B','C'}
f = {'C','B','D'}
len(e&f)
2

序列练习题

a = [1, 2, 3]
b = [4, 5, 6]
c = [9, 6, 7, 8]
max(a)
3
min(a)
1
c.sort()
c
[6, 7, 8, 9]

sort 是应用在 list 上的方法,属于列表的成员方法,sorted 可以对所有可迭代的对象进行排序操作。

result = []
for i in range(1,101):
    result.append(i)
sum(result)
5050
list1 = [2,3,4,5]
for i in list1:
    print(i*3)
6
9
12
15
g = ['x','y','z']
h = [1,2,3]
zipped = zip(g,h)
print(list(zipped))
[('x', 1), ('y', 2), ('z', 3)]
©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页