Python-while循环、for循环、range对象

while循环结构

#while循环
#打印1-10

num=0
while num<=10:
   
print(num)
    num +=
1
#计数1-100数字的累加和
num2 = 0
sum_all=0
while num2<=100:
    sum_all = sum_all + num2
    num2 +=
1
print(sum_all)

 

for循环和可迭代对象遍历

range对象

#for循环
for x in (10,20,30):
   
print(x*3)

for y in "abcdefg":
   
print(y)

d={
"name":"spy","age":18,"job":"test"}
for z in d:
   
print(z)

for z in d.keys():
   
print(z)

for z in d.values():
   
print(z)

for z in d.items():
   
print(z)

#range对象
for x in range(5): #打印01234
   
print(x)

sum_all=
0
sum_odd=0#奇数和
sum_even=0#偶数和
for x in range(101): #列表1-100依次赋值x
   
sum_all += x
   
if x%2 ==1:
        sum_odd+=x
   
else:
        sum_even+=x

print(sum_all,sum_odd,sum_even)

 

 

你可能感兴趣的:(Python)