字符串(str):" " | 储存字符编码 | 不可变 | 序列 |
---|---|---|---|
列表(list): [ ] | 储存变量 | 可变 | 序列 |
元组(tuple): ( ) | 储存变量 | 不可变 | 序列 |
字典(key : value) | 储存键值对 | 可变 | 散列 |
集合(set) | 储存键 | 可变 | 散列 |
athlete=['马龙','科比','刘翔','李娜']
hobby=['乒乓球','篮球','跑步','网球']
info = {athlete[i]:hobby[i] for i in range(len(athlete))}
print(info)
info = {value:key for key,value in info.items()}
print(info)
通过for两层循环打印以下图形
for r in range(5): # 外层循环行
for c in range(6): # 内层循环列
if r % 2: # 奇数循环 -
print("-", end=" ")
else: # 双数循环 *
print("*", end=" ")
print()
two_dim = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16],
]
# 打印出1、6、11、16数字
print(two_dim[0][0])
print(two_dim[1][1])
print(two_dim[2][2])
print(two_dim[3][3])
# 循环打印首行数据,一行一个:结果为1,2,3,4
for item in two_dim[0]:
print(item)
# 循环打印末行数据,一行一个:结果为13,14,15,16
for item in two_dim[3]:
print(item)
# 循环打印首列数据:结果为1,5,9,13
for column in range(len(two_dim)):
print(two_dim[column][0])
# 循环倒序(右→左)打印第二行数据:结果为8,7,6,5
for row in range(len(two_dim[1])-1,-1,-1):
print(two_dim[1][row])
# 循环倒序(下→上)打印第三列数据:结果为15,11,7,3
for column in range(len(two_dim[2])-1,-1,-1):
print(two_dim[column][2])
hero ={
"后裔":{
"技能":["惩戒射击","多重箭矢","落日余晖","灼日之矢"],
"冷却值":["0","10","5","45"],
"消耗":["0","60","60","130"],
},
"安其拉":{
"技能":["咒术火焰","火球术","混沌火种","炙热光辉"],
"冷却值":["0","6","8","20"],
"消耗":["0","50","60","100"],
}
}
# 打印后裔技能
for skill in hero["后裔"]["技能"]:
print(skill)
# 打印安其拉冷却值
for cool in hero["安其拉"]["冷却值"]:
print(cool)
# 打印所有英雄
for roles in hero.keys():
print(roles)
# 打印所有人物的消耗属性值
for roles in hero.keys():
for value in hero[roles]["消耗"]:
print(value)
# 在后裔技能里添加“闪现”
hero["后裔"]["技能"].append("闪现")
print(hero["后裔"]["技能"])
dict_letter = {}
info = "wqrqdqwewqrrqweqeqwefds"
for item in info:
if item in dict_letter:
dict_letter[item] +=1
else:
dict_letter[item] = 1
for k,v in dict_letter.items():
print("%s出现的次数是%d"%(k,v))
dict_shop = {}
while True:
name = input("请输入商品名称:")
if name == "":
break
price = float(input("请输入商品单价:"))
dict_shop[name] = price
for k, v in dict_shop.items():
print("%s的价格是%f." % (k, v))
list_stu = []
while True:
name = input("请输入学生姓名:")
if name == "":
break
age = int(input("请输入学生年龄(整数):"))
sex = input("请输入学生性别:")
score = float(input("请输入学生成绩(数字):"))
dict_stu = {"name":name, "age":age,"sex":sex,"score":score}
list_stu.append(dict_stu)
for student in list_stu:
print("%s的年龄是%d,性别是%s,成绩是%.2f" % (student["name"], student["age"],student["sex"],student["score"]))
# 最后一个学生信息
student = list_stu[-1]
print("%s的年龄是%d,性别是%s,成绩是%.2f" % (student["name"], student["age"], student["sex"], student["score"]))