range (start, stop, step)
可以只有一個參數(shù):range(5), 是指[0,1,2,3,4]
可以有兩個參數(shù):
range(1,5) = [1,2,3,4]
range(-1,5)=[-1,0,1,2,3,4]
while 循環(huán)
break 是指退出循環(huán)
while True:
if guess_age==age:
print ("you are right")
break
else:
...
...
continue 是指退出當(dāng)次循環(huán)
num=1
while num<=10:
num+=1
if num%3==0:
continue
print (num)
實際輸出:
2
4
5
7
8
10
11
while ... : ... else:
num=1
while num<=10:
num+=1
if num%3==0:
continue
print (num)
else:
print("this is else statement")
輸出:
2
4
5
7
8
10
11
this is else statement
num=1
while num<=10:
num+=1
if num%3==0:
break
print (num)
輸出:
2
作業(yè)輸出九九表
while num1<=9:
num2=1
while num2<=num1:
print(num2,"*",num1,"=",num1*num2,end= " ")
num2+=1
num1+=1
print()