1、while循环
n=0 #初始条件
while n<=5: #判断print('hello python') #要重复执行的代码print(n) #注意同级代码缩进相同n+=1 #计数器结果:
hello python
0
hello python
1
hello python
2
hello python
3
hello python
4
hello python
5
#求阶乘和
sum=0
n=1
while n<=3:i=1ret=1while i<=n:ret*=ii+=1sum+=retn+=1
print(sum)结果:9
死循环:条件始终为真。
2、for循环
for i in range(3): #生成0~2一个序列print('hello')结果:
hello
hello
hello
#求阶乘和
sum=0
ret=1
for n in range(4):ret=1for i in range(n+1):if i>0:ret=ret*isum=sum+ret
print(sum)结果:10