1. 什么是元组(tuple)
“”"
元组是容器型数据类型(序列), 将()作为容器的标志里面多个元素用逗号隔开: (元素1, 元素2, 元素3,…)
元组不可变(不支持增删改);元组是有序的(支持下标操作)
元素:任何类型的数据
“”"
t1 = (10, 20, 30)
print(t1, type(t1))
t2 = (10, ‘acb’, False, [10, 230])
print(t2)
t3 = () # 空元组
print(t3, type(t3))
2. 元组就是不可变的列表
列表中除了和增、删、改相关操作以外的操作元组都支持
例如:查、相关操作、相关方法(除了增删改相关的)、相关函数
t2 = (10, 23, 78, 9)
print(t2[-1])
print(’--------------------------’)
print(t2[1:])
print(’--------------------------’)
for x in t2:
print(x)
print(’--------------------------’)
for index in range(len(t2)):
print(index, t2[index])
print(’--------------------------’)
for index, item in enumerate(t2):
print(index, item)
print(’--------------------------’)
print((10, 23) + (100, 100, 200))
print((10, 23) * 3)
print((1, 2, 3) == (1, 2, 3))
print((1, 2, 3) == (3, 2, 1))
print((1, 1000, 3000, 70000) > (2, -100))
print(10 in (1, 20, 10))
num = 23
t_num = type(num)
if t_num == int or t_num == float or t_num == str:
print(‘是数字或者字符串’)
if t_num in (int, float, str):
print(‘是数字或者字符串’)
t2 = (10, 23, 78, 9, 10)
print(t2.count(10))
print(t2.index(23))
print(max(t2), min(t2))
print(sum(t2))
print(sorted(t2)) # [9, 10, 10, 23, 78]
print(tuple(‘abc’))
print(len(t2))
3.元组特殊或者常用的操作
1)只有一个元素的元组: (元素,)
list1 = [10]
print(list1, type(list1))
t1 = (10,)
print(t1, type(t1))
2)元组在没有歧义的情况下可以省略()。
直接用逗号将多个数据隔开,表示的也是一个元组
t2 = 10, 23, 45, 6
print(t2, type(t2))
3)同时使用多个变量获取元组的元素(列表也支持)
t3 = (‘小明’, 18, 90)
print(t3[0])
a. 让变量的个数和元组中元素个数保持一致
name, age, score = t3
print(name, age, score)
point = (100, 200)
x, y = point
print(x, y)
x, y = 100, 200
m, n, t = 10, 20, 30
t3 = (‘小明’, 18, 90)
x, y, z, t = t3 # 报错
b. 让变量的个数小于元组元素的个数,但是必须在某一个变量前加*
先让不带的变量按照位置获取元素,剩下的部分全部保存到带的变量对应的列表中
t3 = (‘小明’, 18, 170, 80, 99, 80, 76)
name, age, height, weight, *scores = t3
x, y, *z = t3
print(x, y, z) # 小明 18 [170, 80, 99, 80, 76]
x, *y, z = t3
print(x, z, y) # 小明 76 [18, 170, 80, 99, 80]
*x, y, z = t3
print(y, z, x) # 80 76 [‘小明’, 18, 170, 80, 99]
x, *y, z, t = t3
print(z) # 80
print(y) # [18, 170, 80, 99]
student = [‘张四’, ‘张三’, 30, ‘110’, 28]
print(student[0])
student = {
‘name’: ‘张四’,
‘contacts’: ‘张三’,
‘年龄’: 30,
‘tel’: ‘110’,
‘score’: 28
}
print(student[‘name’])
1. 什么是字典(dict)
“”"
1)字典是容器型数据类型(序列),将{}作为容器的标志,里面多个键值对用逗号隔开:{键1:值1, 键2:值2, 键3:值3,…}
2)字典是可变的(支持增删改);字典是无序的(不支持下标操作)
3)元素 - 键值对
键 - 必须是不可变的数据,例如:元组、数字、字符串。键是唯一的
值(才是真正想要保存的数据) - 没有要求
“”"
1)空字典
dict1 = {}
print(dict1, type(dict1))
2) 键是不可变的数据
dict2 = {‘a’: 10, 1: 20, (1, 2): 30}
print(dict2)
dict3 = {‘a’: 10, 1: 20, [1, 2]: 30}
print(dict3) # 报错!
3)键是唯一的
dict3 = {‘a’: 10, 1: 20, ‘a’: 30}
print(dict3) # {‘a’: 30, 1: 20}
字典的查操作是获取字典的值
1. 查单个
“”"
- 字典[键] - 获取字典中指定键对应的值; 当键不存在的时候会报错
- 字典.get(键) == 字典.get(键, None) - 获取字典中指定键对应的值; 当键不存在不会报错,返回None
- 字典.get(键, 默认值) - 获取字典中指定键对应的值; 当键不存在不会报错,返回默认值
“”"
dog = {‘name’: ‘小黑’, ‘breed’: ‘哈士奇’, ‘age’: 3, ‘color’: ‘黑色’}
print(dog[‘breed’])
print(dog[‘name’])
print({‘name’: ‘小黑’, ‘breed’: ‘哈士奇’, ‘age’: 3, ‘color’: ‘黑色’}[‘color’])
print(dog.get(‘name’))
print(dog[‘gender’]) # 报错
print(dog.get(‘gender’)) # None
student = {‘name’: ‘小明’}
print(student.get(‘age’, 20))
2.遍历
for 变量 in 字典:
循环体
变量取到的是键
“”"
print(’----------------------------------------------------’)
dog = {‘name’: ‘小黑’, ‘breed’: ‘哈士奇’, ‘age’: 3, ‘color’: ‘黑色’}
for x in dog:
print(x, dog[x])
用一个字典保存一个班级信息
class1 = {
‘name’: ‘Python2106’,
‘address’: ‘9教’,
‘lecturer’: {
‘name’: ‘余婷’,
‘age’: 18,
‘tel’: ‘13678192302’,
‘QQ’: ‘726550822’
},
‘head_teacher’: {
‘name’: ‘张瑞燕’,
‘tel’: ‘110’,
‘QQ’: ‘7283211’
},
‘students’: [
{
‘name’: ‘陈来’,
‘age’: 20,
‘gender’: ‘女’,
‘score’: 98,
‘contacts’: {
‘name’: ‘p2’,
‘tel’: ‘120’
}
},
{
‘name’: ‘葛奕磊’,
‘age’: 25,
‘gender’: ‘男’,
‘score’: 80,
‘contacts’: {
‘name’: ‘p1’,
‘tel’: ‘119’
}
}
]
}
-
获取班级的名字
print(class1[‘name’]) -
获取讲师的名字和年龄
lecturer = class1[‘lecturer’]
print(lecturer[‘name’], lecturer[‘age’])
print(class1[‘lecturer’][‘name’], class1[‘lecturer’][‘age’]) -
获取班主任的名字和电话
head_teacher = class1[‘head_teacher’]
print(head_teacher[‘name’], head_teacher[‘tel’]) -
获取第一个学生的姓名
print(class1[‘students’][0][‘name’]) -
获取所有学生的联系人的名字
for stu in class1[‘students’]:
print(stu[‘contacts’][‘name’]) -
计算所有学生的平均分
sum1 = 0
all_student = class1[‘students’]
for stu in all_student:
sum1 += stu[‘score’]
print(sum1 / len(all_student))
1. 增 - 添加键值对
2. 改 - 修改键对应的值
语法: 字典[键] = 值 - 当键存在的时候是修改,键不存在就是添加
goods = {
‘name’: ‘泡面’,
‘price’: 3.5
}
print(goods)
goods[‘count’] = 10
print(goods)
goods[‘price’] = 4
print(goods)
3. 删
1)del 字典[键] - 删除字典中指定键对应的键值对
goods = {‘name’: ‘泡面’, ‘price’: 4, ‘count’: 10}
del goods[‘price’]
print(goods) # {‘name’: ‘泡面’, ‘count’: 10}
2) 字典.pop(键) - 取出字典中指定键对应值
goods = {‘name’: ‘泡面’, ‘price’: 4, ‘count’: 10}
result = goods.pop(‘price’)
print(goods) # {‘name’: ‘泡面’, ‘count’: 10}
print(result) # 4