DAY4
学习内容
1.python基础第四节 2.数学理论学习
课堂练习
'''
# 字典{} 本质上是一个散列表即稀疏数组,存储时先计算键的散列值,取右侧三位进行比较,若为空则存入,否则依次再取三位,数组在接近2/3时会进行扩容
a = {'name':'jeffery','age':18,'job':'programmer'}
b = dict(name='tom',age=20,job='nurse')
c = dict([('name','rimi'),('age',32)])
k = ['name','age','job']
d = ['jet',26,'teacher']
e = zip(k,d)
print(dict(e))
print(e)
f = dict.fromkeys(['name','age','job'])
print(f)
print(a['name'])
print(a.get('age','不存在'))
print(a.get('num','不存在'))
print(a.keys())
print(a.values())
print(len(a))
a['name'] = 'zhang' # 没有的加上,有的覆盖,也可用update()方法
# 删除:del(),clear()删除所有键值对,pop()删除指定键值对并返回值,popitem()可随机删除和返回键值对;
x,y,z = a.values() # 序列解包
print(x)
'''
'''
r1 = {"name":"高小一","age":18,"salary":10000,"city":"上海"}
r2 = {"name":"高小二","age":22,"salary":20000,"city":"成都"}
r3 = {"name":"高小三","age":32,"salary":40000,"city":"北京"}
info = [r1,r2,r3]
for i in range(len(info)):
print(info[i].get("name"),info[i].get("age"),info[i].get("salary"),info[i].get("city"))
# 集合:无序可变,元素不能重复,本质上是基于字典,只是只有字典中的“键”
a = {10,20,30,40}
a.add(70) # 使用add()方法添加元素
b = ['a','b','c','a']
c = set(b) # 使用set()方法将列表转为集合
print(c)
a.remove(20) # remove()函数删除指定元素,clear()清空整个集合
# 集合相关操作
d = {1,3,'sxt'}
e = {'he','sxt',3,8}
print(d|e) # 并集
print(d&e) # 交集
print(d-e) # 差集
print(d.union(e)) # 并集
print(d.intersection(e)) # 交集
print(d.difference(e)) # 差集
a = ""
b = []
c = "False"
if a:
print("not none")
if b:
print("not none")
if not c:
print("this is False")
s = input("请输入一个数字:")
if int(s) > 10:
print("s大于10")
else:
print("s小于或等于10")
# 三元运算符:条件为真时的值 if (条件表达式) else 条件为小时的值
num = input("请输入一个数字:")
print(num if int(num)<=10 else "wrong number")
score = int(input("请输入你的分数:"))
grade = ''
if score<60:
grade = "不及格"
elif score<80:
grade = "及格"
elif score<90:
grade = "良好"
else:
grade = "优秀"
print("你的分数是{score},等级是{grade}".format(score=score,grade=grade))
score = int(input("请输入一个0-100的数:"))
grade = ''
if score<0 or score>100:
print("输入错误,请重新输入一个0-100的数字!")
else:
if score<60:
grade = "E"
elif score<70:
grade = "D"
elif score<80:
grade = "C"
elif score<90:
grade = "B"
else:
grade = "A"
print("分数为:{0},等级为:{1}".format(score,grade))
degree = "ABCDE"
num = 0
if score>100 or score<0:
print("输入错误,请重新输入一个0-100的数字!")
else:
num = score//10
if num < 6:
num = 5
print("分数为:{0},等级为:{1}".format(score,degree[9-num]))
'''
# 循环结构
# WHILE循环
num = 0
while num <= 10:
print(num, end='\t')
num+=1
num1 = 0
sum_all = 0
while num1<=100:
sum_all = sum_all + num1
num1 += 1
print(sum_all)
|