python中 while, if, for 語句有什么區別嗎?比如在進行range函數循環時
while是Python中的循環語句,if屬于條件選擇語句,而for in屬于迭代語句。
while 用于重復執行操作,重復的條件不一定是特定的次數,只要條件滿足即可:
# 打印1-5的平方
x = 1
while x print(x ** 2)
x = x + 1if 與 循環迭代無關,在默認順序執行代碼的情況下,滿意某個條件要以執行分支語句:
score = 66
if score >= 60:
print('及格')
for in用于遍歷一個可迭代對象(如列表,元組,range等)的每一個元素,將迭代出的每個元素放入臨時變量備用:
# 遍歷1-5的序列元素,并打印平方
for x in range(1,6):
print(x ** 2)
python中if和for語句的使用
while是Python中的循環語句,if屬于條件選擇語句,而for in屬于迭代語句。
while 用于重復執行操作,重復的條件不一定是特定的次數,只要條件滿足即可:12345# 打印1-5的平方x = 1while x <= 5: print(x ** 2) x = x + 1if 與 循環迭代無關,在默認順序執行代碼的情況下,滿意某個條件要以執行分支語句:123score = 66if score >= 60: print('及格')for in用于遍歷一個可迭代對象(如列表,元組,range等)的每一個元素,將迭代出的每個元素放入臨時變量備用:123# 遍歷1-5的序列元素,并打印平方for x in range(1,6): print(x ** 2)。
python循環語句
Python While循環語句 Python 編程中 while 語句用于循環執行程序,即在某條件下,循環執行某段程序,以處理需要重復處理的相同任務。
其基本形式為:while 判斷條件: 執行語句…… 執行語句可以是單個語句或語句塊。判斷條件可以是任何表達式,任何非零、或非空(null)的值均為true。
當判斷條件假false時,循環結束。實例:#!/usr/bin/python count = 0 while (count print 'The count is:', count count = count + 1 print "Good bye!" 以上代碼執行輸出結果:The count is: 0 The count is: 1 The count is: 2 The count is: 3 The count is: 4 The count is: 5 The count is: 6 The count is: 7 The count is: 8 Good bye!while 語句時還有另外兩個重要的命令 continue,break 來跳過循環,continue 用于跳過該次循環,break 則是用于退出循環,此外"判斷條件"還可以是個常值,表示循環必定成立,具體用法如下:# continue 和 break 用法 i = 1 while i i += 1 if i%2 > 0: # 非雙數時跳過輸出 continue print i # 輸出雙數2、4、6、8、10 i = 1 while 1: # 循環條件為1必定成立 print i # 輸出1~10 i += 1 if i > 10: # 當i大于10時跳出循環 break 無限循環 如果條件判斷語句永遠為 true,循環將會無限的執行下去,如下實例:#coding=utf-8#!/usr/bin/python var = 1 while var == 1 : # 該條件永遠為true,循環將無限執行下去 num = raw_input("Enter a number :") print "You entered: ", num print "Good bye!" 以上實例輸出結果:Enter a number :20 You entered: 20 Enter a number :29 You entered: 29 Enter a number :3 You entered: 3 Enter a number between :Traceback (most recent call last): File "*", line 5, in num = raw_input("Enter a number :") KeyboardInterrupt 注意:以上的無限循環你可以使用 CTRL+C 來中斷循環。
循環使用 else 語句 在 python 中,for … else 表示這樣的意思,for 中的語句和普通的沒有區別,else 中的語句會在循環正常執行完(即 for 不是通過 break 跳出而中斷的)的情況下執行,while … else 也是一樣。#!/usr/bin/python count = 0 while count print count, " is less than 5" count = count + 1 else: print count, " is not less than 5" 以上實例輸出結果為:0 is less than 51 is less than 52 is less than 53 is less than 54 is less than 55 is not less than 5 簡單語句組 類似if語句的語法,如果你的while循環體中只有一條語句,你可以將該語句與while寫在同一行中, 如下所示:#!/usr/bin/python flag = 1 while (flag): print 'Given flag is really true!' print "Good bye!" 注意:以上的無限循環你可以使用 CTRL+C 來中斷循環。
如何熟練掌握python的if、while和for以及與它們相關的break和continue
這是個好問題。真的很棒的問題。加油,好好學習。你會發現很多的樂趣。
編程語言通常是由表達式,語句,程序,函數,類,庫等組成的。if,while,for都是語句。有人說流程,其實這三句話都是在講流程,就是分支語句。就是一個流程里如何建立分支的。
if是標準的分支。當你需要判斷的時候就用
if a==b:
print "a is same as b."
for和while是循環語句,要說程序中最強大的語句是什么,當然就是循環啦。因為可以枯燥的去重復,同時又可以變化某些動作。我最喜歡這個語句。
for i in range(1,5):
print "next is %d"%i
while用處比較多。通常我們用來做文件讀寫
fp=open("*")
while True:
line=*ne()
if not line:break
print "we got %s"%*()
這里還使用了break, break是跳出循環的意思,continue是忽略下面的語句,回到循環開始位置。都是常用的語句。
要想學會語言就要用。什么是用?當然是活學活用。實際使用。先要找到一個你想做的題材。或者是別人的例子,然后自己試著去寫。多練習,多背。
祝學習順利。
轉載請注明出處華閱文章網 » python循環語句if