字典的创建方式
a = {'name':'gaoqi','age':18,'job':'progarammer'}
a
{'name': 'gaoqi', 'age': 18, 'job': 'progarammer'}
字典元素访问
a.get('name')
'gaoqi'
序列解包
a = dict(name='gaoqi',age=18)
a
{'name': 'gaoqi', 'age': 18}
b = [('name','gaoqi'),('age',18)]
b
[('name', 'gaoqi'), ('age', 18)]
k = ['a','b','c']
v = [100,200,300]
d = dict(zip(k,v))
d
{'a': 100, 'b': 200, 'c': 300}
元素添加修改删除
字典复杂表格顺序存储
r1 = {'name':'高1','age':18,'salary':2000,'city':'shanghai'}
r2 = {'name':'高2','age':19,'salary':3000,'city':'beijing'}
r3 = {'name':'高3','age':20,'salary':4000,'city':'guangzhou'}
tb = [r1,r2,r3]
print(tb[2].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'),)
pycharm的运用
双分支选择结构:
s = input('输入一个数:')
if int(s)<10:
print('数小于10')
else:
print('数大于等于10')
print('s<10' if int(s)<10 else 's>=10')
?
?
?
?
选择结构嵌套:
score = int(input('输入分:'))
grade = ''
if score<60:
grade = '不及格'
elif score<80:
grade = '及格'
elif score<90:
grade = '良好'
else:
grade = '优秀'
print('分数{0},等级{1}'.format(score,grade))
?
score = int(input('输入0-100的分:'))
grade = ''
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))
?
score = int(input('输入0-100的分:'))
degree= 'ABCDE'
num = 0
if score>100 or score<0:
score = int(input('输入错误,重输:'))
else:
num = score//10
if num<6:num=5
print('分数{0},等级{1}'.format(score,degree[9-num]))
?
while循环结构
num = 0
sum = 0
while num<=100:
sum = sum+num
num +=1
print('0-100的和',sum)
?
|