python第二期-day05

字典的遍历

stu_info = {"name": "小鲁班","age": 18,"height":  1.3, "addr":"王者峡谷"}
print(list(stu_info.keys()))  #获取所有键
print(list(stu_info.values()))  #获取所有值
print(list(stu_info.items()))  #获取所有键值

# zip 同时迭代两个列表
for k, v in zip(list(stu_info.keys()), list(stu_info.values())):
    print("本次的键: ", k, "本次的值", v)
print("+++++++++++++++++++++++++++++++++++++++++++++++++++")
for k, v in stu_info.items():
    print("本次的键: ", k, "本次的值", v)

ls = [('name', '小鲁班'), ('age', 18), ('height', 1.3), ('addr', '王者峡谷')]

#list 转化字典
d = dict(ls)
print(d)
# 字典变列表 .items()

在这里插入图片描述
字典推导式:

print([i for i in range(10)])
print(tuple([i for i in range(10)]))
#  key 必须具有唯一性
print({1: i for i in range(10)})
print({i: 1 for i in range(10)})
print({i: i for i in range(10)})
print({str(i): i for i in range(10)})
# 不可变的都可以作为键
print({tuple([i]): i for i in range(10)})

from random import randint
ls = ['a', 'b', 'c', 'd', 'e']
print([randint(40, 100) for i in range(10)])
grade = [randint(40, 100) for i in range(10)]
print(grade)
print([i for i in grade if i >60])
score_D = {k: randint(10, 100) for k in ls }
print(score_D)
new_dict = {}  # 空字典
for k, v in score_D.items():
    if v > 60:
        new_dict[k] = v
print(new_dict)  # 新字典

切片和字符串常用方法

切片是对序列(字符串/列表/元组)截取一部分的操作

序列名[start : end : step]
左闭右开
name = "PythonC++Java"
ls = [i for i in range(1, 11)]
print(name)
print(ls)
print(name[0:6])
print(name[:6]) # 开始是从头的可以省略0
print(name[:6]) # 开始是从头的可以省略0
print(name[9:])
print(name[:])
print(name[::])
print(name[::2])
# 步长为负数代表方向从右向左
print(name[::-1]) # 反转
print(name[::-2]) # 反转
# 正向索引从零开始, 负向索引从 -1开始
print(name[-1])
print(name[12])
print(name[-13:-7])
print(name[-1:-5:-1])
print(ls[0:10:2])
print(ls[::-1])

回文数
121 , 222, 1,

for i in range(100, 1000):
    # 123
    # if 123 == 321
    if i == int(str(i)[::-1]):
        print(i, end='\t',)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值