元素可以是任何类型的数据
tuple1 = (10, 20, 30, 'abc', 10*30)
print(tuple1)
tuple2 = ('咪咪', '任旧康', '张一山', '刘亦菲')
# 获取单个元素
print(tuple2[1])
print(tuple2[-1])
for i in tuple2:
print(i)
for index, item in enumerate(tuple2):
print(index, item)
print(tuple2[::-1])
print((1, 2, 3) + ('a', 'b'))
print((1, 2, 3) * 3)
print((1, 2, 3) == (2, 3, 4))
print(100 in (111, 200, 100))
print(tuple('abc'))
nums = (10, 20, 30, 43, 2, 34, 10)
print(nums.count(10))
print(nums.index(20))
list1 = [1]
print(type(list1)) #
tuple1 = (1)
print(type(tuple1)) #
tuple2 = (1,)
print(type(tuple2)) #
tuple3 = (100, 200, 300)
tuple4 = 100, 200, 300
print(type(tuple4), tuple4)
point = (100, 200)
x, y = point
print(x, y)
stu = ('小明', '30', '男', 89, 90, 44, 100)
names, age, gender, *scores = stu
print(names, age, gender, *scores)
a, *b, c, d = stu
print(a)
print(b)
print(c)
print(d)
问题:用列表同时保存多个意义不同的数据的时候,代码的可读性很低
stu = ['小明', '20', '男', '15182639450', '13689742581', 56, 170, 89]
解决:用字典保存多个意义不同的数据
stu = {
'name': '小明',
'age': 20,
'sex': '男',
'tel': '15182639450',
'm_tel': '13689742581',
'weight': 56,
'height': 170,
'score': 80
}
print(stu['name'])
print(stu['sex'])
print(stu['score'])
字典是容器数据类型的数据,将{}作为容器的标志,里面多个元素用逗号隔开(其中与那苏必须是键值对):
{键1:值1, 键2:值2, 键3:值3, …}
字典是可变的(支持增删改);字典是无序的(不支持下标操作)
dict1 = {
10: 100,
'abc': 300
}
print(dict1)
练习:定义一个字典,保留一条狗的信息,包括名字、品种、颜色、年龄。
dict_dog = {
'姓名': '馒头',
'品种': '藏獒',
'颜色': '黑色',
'叫声': '┗|`O′|┛ 嗷~~',
'年龄': 20
}
print(dict_dog)
movies = {
'name': '战狼',
'director': '吴京',
'actor': '吴京',
'release_time': '2015',
'kind': '动作/战争/军事'
}
movies1 = {
'name': '沉默的羔羊',
'director': '乔纳森·戴米',
'actor': '莱蒂·福斯特',
'release_time': '1991',
'kind': '惊悚'
}
print(movies['name'])
print(movies1['release_time'])
print(movies1.get('score'))
print(movies1.get('color', 'yellow'))
print(movies1.get('name'))
print(movies.get('kind'))
for x in movies1:
print(x, movies1[x])
②:
for 变量1, 变量2 in 字典.items(): - 同时获取字典中的key和key对应的值
循环体
for x, y in movies.items():
print(x, y)
print('========================================')
dict_subject = {
'name': 'python',
'score': 3,
'class_hour': 20,
'direction': ['数据分析', 'web后端', '自动化运维', '爬虫', '自动化运维']
}
dict_subject['teacher'] = '老任'
print(dict_subject)
del dict_subject['class_hour']
print(dict_subject)
②:
字典.pop(key) - 取出字典中指定key对应的值
direction = dict_subject.pop('direction')
print(direction)
print({'a': 10, 'b': 20} == {'b': 20, 'a': 10}) # True
a = {'a': 10}
b = {'a': 10}
c = a
print(a == b) # True
print(a is b) # False
print(a is c) # True
dict1 = {'a': 10, 'b': 20, 'c': 30}
print(10 in dict1) # False
print('b' in dict1) #True
print(len(dict1))
② dict(数据) - 将其他数据转换成字典(数据的要求:1.序列 2.序列中的元素还是序列 3内部的序列有且只有两个元素,第一个元素不可变)
list1 = ['ab', (10, 20), ['name', '张三']]
print(dict(list1))
stu = {'name': '小明', 'age': 18, 'score': 100}
print(list(stu))
print(tuple(stu))
print(str(stu))
print(bool({}))
print(bool(stu))
stu = {'name': '小明', 'age': 18, 'score': 100}
stu2 = stu
stu2['name'] = '小花'
print(stu2)
new_dict = dict.fromkeys('abs')
print(new_dict)
stu = {'name': '小明', 'age': 18, 'score': 100}
print(stu.items())
print(stu.keys())
print(stu.values())
stu = {'name': '小明', 'age': 18, 'score': 100}
stu.setdefault('weight', 50)
print(stu)
stu = {'name': '小明', 'age': 18, 'score': 100}
stu2 = {'a': 10, 'b': 20}
stu.update(stu2)
print(stu)