Python中while語句的一般形式:
while 判斷條件:
語句
同樣需要注意冒號和縮進,另外在Python中沒有do…while循環
下面的實例計算1到100總和
##calc.py n = 100 sum = 0 counter = 1 while counter <= n: sum = sum + counter counter += 1 print("total from 1 to 100 : %d",sum)
運行結果:
robot@ubuntu:~/wangqinghe/python/20190826$ python3.5 calc.py
total from 1 to 100 : %d 5050
while循環中使用else語句
在while…else在條件語句為false時執行els語句塊
#while.py count = 0 while count < 5: print(count," < 5") count = count + 1 else : print(count ," >= 5")
運行結果:
robot@ubuntu:~/wangqinghe/python/20190826$ python3.5 while.py
0 < 5
1 < 5
2 < 5
3 < 5
4 < 5
5 >= 5
for循環:
Python for循環可以遍歷任何序列的項目,如一個列表或一個字符串
for循環的 一般格式如下
for <variable> in <sequence>: <statement> else: <statement>
實例:
break語句用於跳出當前循環體:
##break.py sites = ["Baidu","Google","Runoob","Taobao"] for site in sites: if site == "Runoob": print("cainiao!") break print("loop data " + site) else: print("Having no loop data!") print("loop end!")
運行結果:
robot@ubuntu:~/wangqinghe/python/20190826$ python3.5 break.py
loop data Baidu
loop data Google
cainiao!
loop end!
range()函數
如果你需要遍歷數字序列,可以使用內置的range()函數,它會生成數列,例如:
也可以使range以指定數字開始並指定不同的增量,(甚至可以是負數,有時這也叫步長)
負數:
也可以結合range()和len()函數以遍歷一個序列的索引:
還可以使用range()函數來創建一個列表:
break和continue語句及循環中的else子句
break語句可以跳出for和while循環體,如果你從for或while循環終止,任何對應的循環else塊將不執行:
#else.py for letter in 'Runoob': if letter == 'b': break; print('the current letter : ',letter) print("the next example") var = 10 while var > 0: print('the current variable : ',var) var = var - 1 if var == 5: break; print("GOOF bye!")
運行結果:
robot@ubuntu:~/wangqinghe/python/20190826$ python3 else.py
the current letter : R
the current letter : u
the current letter : n
the current letter : o
the current letter : o
the next example
the current variable : 10
the current variable : 9
the current variable : 8
the current variable : 7
the current variable : 6
GOOF bye!
continue語句被用來Python跳出當前循環塊的剩余語句,然后繼續下一輪循環。
循環語句可以有else子句,它在窮盡列表(for循環)或條件變為false(以while循環)導致循環終止時被執行,但循環被break終止時不執行。
下列是查詢質數的循環例子:
##prime.py for n in range(2,10): for x in range(2,n): if n % x == 0: print(n," == ",x, '*', n//x ) break else: print(n," is prime")
運行結果:
robot@ubuntu:~/wangqinghe/python/20190826$ python3 prime.py
2 is prime
3 is prime
4 == 2 * 2
5 is prime
6 == 2 * 3
7 is prime
8 == 2 * 4
9 == 3 * 3
pass語句
Python pass是空語句,是為了保持程序結構的完整性。
pass不做任何事情,一般用作占位語句:
#pass.py for letter in 'Runoob': if letter == 'o': pass print('execute pass block') print('the current letter : ',letter) print("Good bye!")
運行結果:
robot@ubuntu:~/wangqinghe/python/20190826$ python3 pass.py
the current letter : R
the current letter : u
the current letter : n
execute pass block
the current letter : o
execute pass block
the current letter : o
the current letter : b
Good bye!
pass只是為了防止語法的錯誤
pass就是一條空語句,在代碼段中或定義函數時,如果沒有內容,或者就先不做任何處理,直接跳過,就可以先使用pass
十進制轉換:
#translate.py while True: number = input('please input a integer(enter Q exit ):') if number in ['q','Q']: break elif not number.isdigit(): print("input error,please continue input : ") continue else: number = int(number) print("decimal --> hexadecimal: %d -> 0x%x"%(number,number)) print("decimal --> octonary: %d -> 0x%o"%(number,number)) print("decimal --> binary: %d -> "%number,bin(number))
運行結果:
robot@ubuntu:~/wangqinghe/python/20190826$ python3 translate.py
please input a integer(enter Q exit ):9
decimal --> hexadecimal: 9 -> 0x9
decimal --> octonary: 9 -> 0x11
decimal --> binary: 9 -> 0b1001
please input a integer(enter Q exit ):18
decimal --> hexadecimal: 18 -> 0x12
decimal --> octonary: 18 -> 0x22
decimal --> binary: 18 -> 0b10010
please input a integer(enter Q exit ):q