Python学习第七天
运算符for
for 循环 :可以遍历任何序列的项目:元组、列表、字典或指定的数据范围
str_1 =‘zhangsan’
tuple_1=(1,1.2,‘张三’,(1,2,3),[1,2,3],{1,2,3})
list_1=[1,1.2,‘张三’,(1,2,3),[1,2,3],{1,2,3}]
dict_1={‘name’:‘张三’,‘age’:18,‘money’:99.88,‘score’:[100,99,88]}
for item in str_1 :
print(‘字符串:{}’.format(item))
print()
for item in tuple_1 :
print(‘元组:{}’.format(item))
print()
for item in list_1 :
print(‘列表:{}’.format(item))
print()
for item in dict_1 :
print(‘字典:{}’.format(item))
print()
for item in dict_1.keys() :
print(‘字典key:{}’.format(item))
print()
for item in dict_1.values() :
print(‘字典values:{}’.format(item))
#题目 1-100整数和相加
print(list(range(0,100,1)))
sum = 0
for item in list(range(1,101,1)) :
sum += item
print(‘1-{}整数和相加={}’.format(item,sum))
print(‘1-100整数和相加={}’.format(sum))
for 嵌套循环
list_1=[[1,3,5,7,9],[0,2,4,6,8]]
for item in list_1:
for i in item:
print(str(i)+’ ‘, end=’’)
print()
print()
打印9*9乘法表
for i in list(range(1,10,1)) :
for n in list(range(1,i+1,1)) :
if in < 10:
print(str(i) + '’ + str(n) + ‘=’ + str(in) + ’ ‘, end=’’)
else:
print(str(i) + '’ + str(n) + ‘=’ + str(i * n) + ’ ‘, end=’’)
print()