for 变量 in 序列:
循环体
1)for in - 都是关键字,固定写法
2)变量 - 自定义一个变量名
3)序列 - 容器型数据类型对应的数据
4)常见的序列:字符串,元组,列表,字典,迭代器,生成器,集合
5)循环体:重复执行的代码段
6)执行过程:变量依次从序列中取值,取一次循环体执行一次,直到取完
for i in "I Love You":
print(i)
1)range(N) 产生[0-N)的数字序列,步长默认为1
2)range(M,N) 产生[M,N)的数字序列,步长默认为1
3)range(M,N,step) 产生[M,N)的数字序列 step为指定步长的值
for i in range(10):
print(i)
print("---------------------")
a = 0
for i in range(0,100):
a+=1
print("hello")
print(a)
print("---------------------")
for i in range(10,20,2):
print(i)
print("---------------------")
for i in range(20,2,-3):
print(i)
for i in range(0,100,2):
print(i)
for i in range(100,200):
if i % 10 == 3:
print(i)
for i in range(100,1000):
if (i//100)**3+(i//10%10)**3+(i%10)**3 == i:
print("水仙花数:",i)
count = 0
for i in range(100):
if i % 2 == 0:
count+=1
print(count)
count = 0
for i in range(100):
if i % 6 == 0:
count+=1
print(count)
sum = 0
for i in range(101):
sum += i
print(sum)
sum = 0
for i in range(100,1000):
if (i//100)**3+(i//10%10)**3+(i%10)**3 == i:
sum += i
print(sum)
while 条件语句:
循环体
2.2.1while - 关键字,固定写法
2.2.2条件语句 - 任何有结果的表达式
2.2.3循环体 - 重复执行的代码段
2.3.1先判断条件,true执行循环体,执行完再次判断,
2.3.2如果为true再次执行,以此类推,直到条件不成立,循环结束
num = 0
while num<10:
print("hello")
num += 1
2.4.1for循环:循环次数确定的时候使用
2.4.2while循环:循环次数不确定的时候使用
b = input("请输入:")
while b !='0':
b = input("请输入:")
说明:只能出现在循环体中,for循环与while循环都可
如果遇到continue,当次循环结束,直接执行下一次循环
for i in range(4):
print('======')
continue
print('++++++')
for i in range(10):
if i == 5 or i == 8:
continue
print(i)
如果遇到break,整个循环结束
for i in range(10):
print(i)
break
for i in range(10):
if i == 5:
break
print(i)
while True:
value = input("请输入:")
if value == '0':
break
for循环
for 变量 in 序列:
循环体
else:
代码段
while循环
while 条件语句:
循环体
else:代码段
for i in range(4):
print(i,"====")
else:
print("正常")
for i in range(4):
print(i,"====")
if i == 2:
break
else:
print("正常")
使用for和while循环输出 0~100内所有3的倍数。
for i in range(0,100,3):
print(i)
x = 0
while x<100:
if x % 3 ==0:
print(x)
x+=1
for i in range(100,200):
if (i%10) % 3 ==0 or (i//10%10) % 3 ==0:
print(i)
x = 100
while x<200:
if (x % 10) % 3 == 0 or (x // 10 % 10) % 3 == 0:
print(x)
x+=1
count = 0
for i in range(100,200):
if i//10%10 == 5:
count+=1
print(count)
for i in range(50,150):
if i % 3 ==0 and i % 5 !=0:
print(i)
sum = 0
for i in range(50,150):
if i % 3 ==0 and i % 5 !=0:
sum+=i
print(sum)
1*2*3*4*...*10
的结果。k = 1
for i in range(1,11):
k*=i
print(k)
count = 0
for i in range(100):
if i % 10 == 2 and i % 3 == 0:
count+=1
print(count)
注意: 这儿不能使用字符串,只能用循环
b = int(input("请输入一个正整数:"))
count = 0
while b>0:
b //=10
count+=1
print("它是一个",count,"位数")
1³ + 5³ + 3³
等于 153。for i in range(100,1000):
if (i//100)**3+(i//10%10)**3+(i%10)**3 == i:
print("水仙花数:",i)
x = int(input("请输入一个数:"))
for i in range(2,x):
if x % i == 0:
print("这个数不是素数")
break
else:
print("这个数是素数")
n = int(input('请输入一个正整数:'))
n1 = 0
n2 = 1
sum = 1
for i in range(2, n+1):
sum = n1 + n2
n1 = n2
n2 = sum
print('第',n,'个数是',sum)
for i in range(1,10):
for j in range(1,i+1):
print(i,"*",j,"=",i*j,end=' ')
print("")
for i in range(34):
for j in range(51):
k = 100 - i -j
if 3*i+2*j+k/2 == 100 and i+j+k==100:
print("有大,中,小马各",i,j,k,"匹")