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()

python第二期-day05
字典推导式:

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',)
上一篇:Java-day05-方法


下一篇:零基础入门学习Java课堂笔记 ——day05