Day8-总结

2019-06-13  本文已影响0人  SheeranED

一.元祖

1.什么是元组(tuple)

2.查 - 和列表一样

tuple1 = (10, 20, 30)
print(tuple1[1:])

3.相关运算 - 与列表一样

4.元组特有的操作

tuple1 = (10,)
print(type(tuple1), tuple1)
tuple2 = 123, True, '222wss'
tuple3 = (1, 2, 3)
x, y, z = tuple3
print(x, y, z)
tuple4 = ('小明', 30, 42, 43, 54, 34, 54)
name, age, *scores = tuple4  # name, *scores, age = tuple4
print(name, age)
print(scores)

二.字典

1.什么是字典(dict)

2.元素

3.查 - 获取字典的值

class1 = {'name': 'python1903', 'student_num': 23, 'address': '18-8'}
print(class1['name'])
print(class1.get('student_num'))
print(class1.get('id', '001'))  # 001
for x in class1.values():
    print(x)
print(class1.values())
for key, value in class1.items():
    print(key, value)
print(class1.items())

三.字典的相关操作

1.增(改) - 添加键值对

dog = {'name': '坤坤', 'age': 3, 'color': '黄色'}
dog['gender'] = '共'
dog['age'] = '4'

2. 删 - 删除键值对

del dog['color']
del_name = dog.pop('name')

3.相关运算

4.字典相关的方法

dict2 = dict.fromkeys('adsadsa')
dict3 = dict.fromkeys('dasdsa', 2)
print(dict3)
dog.setdefault('name', 'fss')

四.集合

1.什么是集合(set)

2.元素

ages = [12, 42, 42, 42, 21]
ages = list(set(ages))
print(ages)
set0 = set()  # 表示空集合

3.查

set2 = {1, 3, 4, 5, 7}
for item in set2:
    print(item)

4.增

set2.add(200)
print(set2)

5.删

set2.remove(200)
print(set2)

6.改 - 集合不支持改操作

7.数学集合运算

set1 = {1, 2, 3, 4, 5, 6}
set2 = {1, 2, 3, 4, 7}
print(set2 | set1)

8.判断集合的包含关系

上一篇下一篇

猜你喜欢

热点阅读