len(my_list):列表长度del list[]:删除指定下标元素my_set2[key]:获取、新增、修改my_int = 1
my_str = "123"
my_list = [1, 2, 2]
my_tuple0 = (1,) # 仅有一个元素时,末尾加逗号
my_tuple = (1, 2, 'a')
my_set0 = set() # 定义空集合
my_set = {1, 2, 'a'}
my_dict0 = {} # 定义空字典1
my_dict1 = dict() # 定义空字典2
my_dict2 = {'a': 2, 'b': 3}
my_dicts = {
'小王': {
'部门': '科技部',
'工资': 3000,
'级别': 1,
},
'小周': {
'部门': '科技部',
'工资': 3000,
'级别': 2,
}
}
for name in my_dicts:
if my_dicts[name]['级别'] == 1:
my_dicts[name]['级别'] += 1
my_dicts[name]['工资'] += 1000
print(my_dicts)
定义:内容连续、有序,可使用下标索引的数据容器;
常见序列:字符串、列表、元组
my_str = "员序程马黑"
result = my_str[2::-1]
print(result) # '程序员'
print(len(result)) # 3
print(result.index('员')) # 2
print(result.index('黑')) # 报错
两种:
return只能写一行;def test_func(compute):
result = compute(1, 2)
print(result)
def add(x, y):
return x + y
# 传入函数
test_func(add)
# 传入匿名函数
test_func(lambda x, y: x + y)