Python 入門教程/迴圈、條件語句
(在我們開始學習與使用者互動之前,這是最後一課。迫不及待吧?)
想象一下,你需要一個程式執行某個操作 20 次。你會怎麼做?你可以複製貼上程式碼 20 次,然後得到一個幾乎無法閱讀的程式。或者,你可以告訴計算機在 A 點和 B 點之間重複一段程式碼,直到需要停止的時候。這樣的東西叫做迴圈。
以下是迴圈型別(稱為)的示例whileloop
- 程式碼示例 1 - Thewhileloop
a = 0
while a < 10:
a = a + 1 #alternatively a += 1
print(a)
這個程式是如何工作的?讓我們用英語解釋一下(這被稱為 虛擬碼)
- 程式碼示例 2 - 樸素語言whileloop
'a' now equals 0
As long as 'a' is less than 10, do the following:
Make 'a' one larger than what it already is.
print on-screen what 'a' is now worth.
它做了什麼?讓我們看看計算機在中會“想”什麼whileloop
- 程式碼示例 3 -while迴圈過程
#JUST GLANCE OVER THIS QUICKLY #(It looks fancy, but is really simple.) Is 'a' less than 10? YES (it's 0) Make 'a' one larger (now 1) print on-screen what 'a' is (1) Is 'a' less than 10? YES (it's 1) Make 'a' one larger (now 2) print on-screen what 'a' is (2) Is 'a' less than 10? YES (it's 2) Make 'a' one larger (now 3) print on-screen what 'a' is (3) Is 'a' less than 10? YES (it's 3) Make 'a' one larger (now 4) print on-screen what 'a' is (4) Is 'a' less than 10? YES (it's 4) Make 'a' one larger (now 5) print on-screen what 'a' is (5) Is 'a' less than 10? YES (it's 5) Make 'a' one larger (now 6) print on-screen what 'a' is (6) Is 'a' less than 10? YES (it's 6) Make 'a' one larger (now 7) print on-screen what 'a' is (7) Is 'a' less than 10? YES (are you still here?) Make 'a' one larger (now 8) print on-screen what 'a' is (8) Is 'a' less than 10? YES (it's 8) Make 'a' one larger (now 9) print on-screen what 'a' is (9) Is 'a' less than 10? YES (it's 9) Make 'a' one larger (now 10) print on-screen what 'a' is (10) Is 'a' less than 10? NO (it's 10, therefore isn't less than 10) Don't do the loop There's no code left to do, so the program ends.
簡而言之,當你編寫時,嘗試用這種方式思考while迴圈。這是編寫迴圈的方式
- 程式碼示例 4 -while迴圈形式
while {condition that the loop continues}:
{what to do in the loop}
{have it indented, usually four spaces}
{the code here is not looped}
{because it isn't indented}
一個例子
- 程式碼示例 5 -while迴圈示例
#EXAMPLE
#Type this in and see what it does.
x = 10
while x != 0:
print(x)
x = x - 1
print("Wow, we've counted x down, and now it equals", x)
print ("And now the loop has ended.")
記住,要編寫程式,你需要開啟 IDLE,點選“檔案 > 新視窗”,在新視窗中輸入程式,然後按 F5 執行。
你在標記為 {迴圈繼續的條件} 的區域中輸入什麼?答案是布林表示式。
什麼?對於非數學人員來說,這是一個被遺忘的概念。別擔心,布林表示式僅僅意味著一個可以用 TRUE 或 FALSE 回答的問題。例如,如果你想說你的年齡與你旁邊的人相同,你會輸入
我的年齡 == 旁邊的人的年齡
這個語句將為 TRUE。如果你比對面的人年輕,你會說
我的年齡 < 對面的人的年齡
這個語句將為 TRUE。然而,如果你說以下內容,而對面的人比你年輕
我的年齡 < 對面的人的年齡
這個語句將為 FALSE - 事實上,情況正好相反。這就是迴圈的思維方式 - 如果表示式為真,則繼續迴圈。如果表示式為假,則不迴圈。有了這個,讓我們看看布林表示式中涉及的運算子(表示操作的符號)
| 表示式 | 函式 |
|---|---|
| < | 小於 |
| <= | 小於或等於 |
| > | 大於 |
| >= | 大於或等於 |
| != | 不等於 |
| <> | 不等於 (備選,!= 更常用) |
| == | 等於 |
不要混淆 '=' 和 '==' - '=' 運算子使左邊等於右邊。'==' 運算子表示左邊是否與右邊相同,並返回 true 或 false。
好的!我們(希望)已經涵蓋了 'while' 迴圈。現在讓我們看一下不同的東西 - 條件語句。
條件語句是指僅在滿足特定條件時才執行的一段程式碼。這類似於你剛剛編寫的 'while' 迴圈,它僅在 x 不等於 0 時執行。然而,條件語句只執行一次。任何程式語言中最常見的條件語句是 'if' 語句。以下是它的工作原理
- 程式碼示例 6 - if 語句和示例
'''
if {conditions to be met}:
{do this}
{and this}
{and this}
{but this happens regardless}
{because it isn't indented}
'''
#EXAMPLE 1
y = 1
if y == 1:
print ("y still equals 1, I was just checking")
#EXAMPLE 2
print ("We will show the even numbers up to 20")
n = 1
while n <= 20:
n = n + 1
if n % 2 == 0:
print(n)
print("there, done.")
示例 2 看起來很棘手。但我們所做的只是在每次 'while' 迴圈執行時執行一個 'if' 語句。記住,% 只是除法後的餘數 - 只檢查如果數字除以 2 沒有餘數 - 表明它是偶數。如果它是偶數,它將列印 'n' 的值。
有很多方法可以使用 'if' 語句來處理布林表示式結果為 FALSE 的情況。它們是 'else' 和 'elif'。
'else' 只是告訴計算機如果 'if' 的條件不滿足該怎麼做。例如,閱讀以下內容
- 程式碼示例 7 - else 語句
a = 1
if a > 5:
print("a is greater than 5")
else:
print("a is less than 5")
'a' 不大於 5,因此執行 'else' 下面的內容。
'elif' 只是 'else if' 的簡寫。當 'if' 語句不滿足時,'elif' 將執行它下面的內容,如果條件滿足。例如
- 程式碼示例 8 - elif 語句
z = 4
if z > 70:
print("Something is very wrong")
elif z < 7:
print("This is normal")
'if' 語句,以及 'else' 和 'elif' 遵循以下形式
- 程式碼示例 9 - 完整的 if 語法
if {conditions}:
{run this code}
elif {conditions}:
{run this code}
elif {conditions}:
{run this code}
else:
{run this code}
#You can have as many or as few elif statements as you need
#anywhere from zero to the sky.
#You can have at most one else statement
#and only after all other ifs and elifs.
需要記住最重要的一點是,你必須在每行包含 'if'、'elif'、'else' 或 'while' 的行末新增冒號 (:)。我忘記了這一點,因此導致許多人在這節課上感到困惑(抱歉 ;;))。
另外一點是,要執行的程式碼(如果條件滿足),必須縮排。這意味著,如果你想用 'while' 迴圈迴圈接下來的五行,你必須在接下來的五行的開頭放置固定數量的空格。這是任何語言中良好的程式設計實踐,但 Python 要求你這樣做。以下是一個關於這兩個點的示例
- 程式碼示例 10 - 縮排
a = 10
while a > 0:
print(a)
if a > 5:
print("Big number!")
elif a % 2 != 0:
print("This is an odd number")
print("It isn't greater than five, either")
else:
print("this number isn't greater than 5")
print("nor is it odd")
print("feeling special?")
a = a - 1
print("we just made 'a' one less than what it was!")
print("and unless a is not greater than 0, we'll do the loop again.")
print("well, it seems as if 'a' is now no bigger than 0!")
print("the loop is now over, and without further ado, so is this program!")
請注意那裡的三個縮排級別
第一級的每一行都從零個空格開始。它是主程式,始終執行。第二級的每一行都從四個空格開始。當第一級存在 'if' 或迴圈時,之後第二級的所有內容都會被迴圈/'if',直到新行再次從第一級開始。第三級的每一行都從八個空格開始。當第二級存在 'if' 或迴圈時,之後第三級的所有內容都會被迴圈/'if',直到新行再次從第二級開始。這樣無限迴圈,直到編寫程式的人大腦爆炸,無法理解他/她所寫的內容。還有一種迴圈叫做 'for' 迴圈,但我們將在學習列表之後,在後面的課程中學習它。