马士兵Python基础版2020教程P58-P96 PPT笔记+课堂代码

目录

57 字典

59 字典的创建

60 字典元素的获取

61 字典增删改

62 获取字典试图

63 字典元素遍历

64 字典的特点

65 字典生成式

66 元组

67 元组的创建

68 为什么元组是不可变序列

69 元组的遍历

70 集合

71 集合增删

72 集合间的关系

73 集合的数据操作

74 集合生成式

75 字符串

76 字符串查找

77 字符串大小写转换

78 字符串内容对齐

79 字符串的劈分

80 字符串判断

81 字符串替换和合并

82 字符串比较

83 字符串切片

84 格式化字符串

85 字符串的编码与解码

86 函数

87 函数的参数传递

88 参数传递的内存分析

89 函数的返回值

90 函数的参数定义 - 默认值参数

91 函数的参数定义 - 个数可变的未知参数

92 参数总结

93 变量的作用域

94 递归函数

95 斐波那契数列


57 字典

59 字典的创建

scores={'张三':100,'李四':98,'王五':105}
print(scores)
print(type(scores))

student=dict(name='jack',age=20)
print(student)

#空字典
d={}
print(d)

60 字典元素的获取

scores={'张三':100,'李四':98,'王五':105}
print(scores['张三'])
print(scores.get('张三'))

#print(scores['五六七'])
#KeyError: '五六七'
print(scores.get('五六七'))
#None
print(scores.get('五六七',99))#99是在查找’五六七‘所对的value不存在时,提供的一个默认值
#99

61 字典增删改

scores={'张三':100,'李四':98,'王五':105}
print('张三' in scores)
print('张三' not in scores)

del scores['张三']#删除指定的键值对
print(scores)
scores.clear()
print(scores)
# {'李四': 98, '王五': 105}
# {}

scores['陈六']=100
print(scores)
# {'陈六': 100}

62 获取字典试图

scores={'张三':100,'李四':98,'王五':105}
print(scores)
# {'张三': 100, '李四': 98, '王五': 105}
keys=scores.keys()
print(keys)
print(type(keys))
print(list(keys))#将所有的keys组成的视图组成列表
# dict_keys(['张三', '李四', '王五'])
# <class 'dict_keys'>
# ['张三', '李四', '王五']
values=scores.values()
print(values)
print(type(values))
print(list(values))#将所有的values组成的视图组成列表
# dict_values([100, 98, 105])
# <class 'dict_values'>
# [100, 98, 105]
items=scores.items()
print(items)
print(type(items))
print(list(items))#转换之后的列表元素是由元组组成的
# dict_items([('张三', 100), ('李四', 98), ('王五', 105)])
# <class 'dict_items'>
# [('张三', 100), ('李四', 98), ('王五', 105)]

63 字典元素遍历

scores={'张三':100,'李四':98,'王五':105}
for item in scores:
    print(item,scores[item],scores.get(item))

64 字典的特点

65 字典生成式

item=['abc','def','ghi']
price=[1,2,3]
d={item:price for item,price in zip(item,price)}
print(d)
# {'abc': 1, 'def': 2, 'ghi': 3}

item=['abc','def','ghi','dsdf']
price=[1,2,3,4,5]
d={item:price for item,price in zip(item,price)}
print(d)
# {'abc': 1, 'def': 2, 'ghi': 3, 'dsdf': 4}

 两个列表不相等时,压缩打包的时候依据短的列表来生成

66 元组

不可变的序列:字符串、元组

可变序列:列表、字典

67 元组的创建

如果元组中只有一个元素,逗号不能省

t1=('python','world',23)
print(t1)
print(id(t1))
print(type(t1))
# ('python', 'world', 23)
# 1882095097664
# <class 'tuple'>
t2=tuple(('python','world',25))
print(t2)
print(id(t2))
print(type(t2))
# ('python', 'world', 25)
# 1882095097856
# <class 'tuple'>
t3='python','world',23
print(t3)
print(id(t3))
print(type(t3))
# ('python', 'world', 23)
# 1882095097664
# <class 'tuple'>
t=('python',)
print(t)
print(type(t))
# ('python',)
# <class 'tuple'>
t=('python')
print(t)
print(type(t))
# python
# <class 'str'>

#空元组创建方式
t=()
t1=tuple()

68 为什么元组是不可变序列

t=(10,[20,30],9)
print(t)
# t[1]=100 TypeError: 't
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值