python-字典、元素、控制语句
字典的基础知识
1、字典
1.1字典的创建
str1={'name':'zhaowei','age':45,'job':'programmer'}
print(str1.get('name'))
str2=dict(name='lili',age=18)
print(str2.get('name'))
str3=dict([("name","zz"),("age",18)])
print(str3.get('age'))
#通过zip创建字典对象
str4=['name','age','job']
str5=['zhaowei',12,'teacher']
str6=dict(zip(str4,str5))
print(str6)
#通过fromkeys创建值为空的字典
str7=dict.fromkeys(['name','age','job'])
print(str7)
输出为
zhaowei
lili
18
{‘name’: ‘zhaowei’, ‘age’: 12, ‘job’: ‘teacher’}
{‘name’: None, ‘age’: None, ‘job’: None}
1.2 字典元素的访问
str8={'name':'zhaowei','age':45,'job':'programmer'}
print(str8['name'])
print(str8.get('age'))
print(str8.get('ddd','不存在'))
#通过items获取所有的键值对
str9={'name':'zhaowei','age':45,'job':'programmer'}
print(str9.items())
#列出所有的键,列出所有的值
str10={'name':'zhaowei','age':45,'job':'programmer'}
print(str10.keys())
print(str10.values())
print(len(str10))#键值对的个数
a='name'in str10#检测键是否在字典中
print(a)
输出为
zhaowei
45
不存在
dict_items([(‘name’, ‘zhaowei’), (‘age’, 45), (‘job’, ‘programmer’)])
dict_keys([‘name’, ‘age’, ‘job’])
dict_values([‘zhaowei’, 45, ‘programmer’])
3
True
1.3、字典元素的添加、修改、删除
str10['address']='上海'
print(str10)
#使用update()将新字典中的键值对全部添加到旧字典对象上,如果key有重复,则直接覆盖
str11={'name':'zhaowei','age':45,'job':'programmer'}
str12={'name':'zheng','age':46,'job':'java','address':'北京'}
str11.update(str12)
print(str11)
#字典中的删除del(),pop(),clear()
str13={'name':'zhaowei','age':45,'job':'programmer'}
del(str13['name'])
print(str13)
b=str13.pop('age')
print(b)
#popitem()随机的删除和返回该键值对
str14={'name':'zhaowei','age':45,'job':'programmer'}
print(str14.popitem())
#序列解包,可以用于元组、列表、字典
x,y,z=(20,30,40)
print(x)
[x1,y1,z1]=[11,12,13]
print(x1)
str15={'name':'zhaowei','age':45,'job':'programmer'}
a1,b1,c1=str15#序列解包用于字典
print(a1)
a2,b2,c2=str15.values()
print(a2,b2,c2)
w,s,x=str15.items()
print(w)
print(w[1])
输出为
{‘name’: ‘zhaowei’, ‘age’: 45, ‘job’: ‘programmer’, ‘address’: ‘上海’}
{‘name’: ‘zheng’, ‘age’: 46, ‘job’: ‘java’, ‘address’: ‘北京’}
{‘age’: 45, ‘job’: ‘programmer’}
45
(‘job’, ‘programmer’)
20
11
name
zhaowei 45 programmer
(‘name’, ‘zhaowei’)
zhaowei
1.4、表格数据使用字典和列表存储,并实现访问
r1={"name":"高小一","age":18,"salary":30000,"city":"北京"}
r2={"name":"高小二","age":19,"salary":20000,"city":"上海"}
r3={"name":"高小三","age":20,"salary":10000,"city":"深圳"}
tb=[r1,r2,r3]
print(tb[1].get("salary"))#获得高小二的薪资
for i in range(len(tb)):#获得所有人的薪资
print(tb[i].get("salary"))
#打印表的所有数据
for i in range(len(tb)):
print(tb[i].get("name"),tb[i].get("age"),tb[i].get("salary"),tb[i].get("city"))
输出为
20000
30000
20000
10000
高小一 18 30000 北京
高小二 19 20000 上海
高小三 20 10000 深圳
2、集合
2.1 集合的基础增删改查
e1={20,30,56}
e1.add(89)
print(e1)
e3=['a','s','c','d','f']
e4=set(e3)#set将列表、元组可以转化成
print(e4)
#集合的删除remove,并集|或者union、交集&或者intersection、差集-或者difference
输出为
{56, 89, 20, 30}
{‘s’, ‘d’, ‘a’, ‘c’, ‘f’}
5.2 控制语句
#控制语句
a=input("请输入:")
if int(a)<5:
print(a)
else:
print("a是大于5的数")
n1=input("请输入")
print("n1是小余10的数" if int(n1)<10 else "n1是大于等于10的数")
#测试多分支结构
s=int(input("请输入分数"))
grade=""
if s<60:
grade="不及格"
elif s<80:
grade = "及格"
elif s<90:
grade = "良好"
else:
grade="优秀"
print("分数是{0},等级是{1}".format(s,grade))
score=int(input("请输入分数"))
if score>100 or score<0:
score = int(input("请重新输入"))
else:
if score>=90:
grade="A"
elif score>=80:
grade="B"
elif score>=70:
grade="C"
elif score>=60:
grade="D"
else:
grade="E"
print("分数为{0},等级为{1}".format(score,grade))
score1=int(input("请输入分数"))
degree="ABCDE"
num=0
if score>100 or score<0:
score = int(input("请重新输入"))
else:
num=score1//10
if num<6: num=5
print("分数是,等级是".format(score1,degree[9-num]))
#循环结构while
num=0
while num<=10:
print(num)
num+=1
本文地址:https://blog.csdn.net/okm1314ijn/article/details/107351251
下一篇: javascript笔记之匿名函数和闭包