Python if
資訊之芽 梁安哲 2022/03/13
- 課程回顧
- 比較運算子
- 邏輯運算子
- 基本語法
- 縮排
- 花俏語法
變數
| someInteger = 10 |
| someString = "A for apple, B for ball." |
| someBool = False |
| someFloat = 8.7 |
輸入
| colossalTitan = input("城牆裡有幾隻超大型巨人?") |
| print("早上好馬萊,現在我有" + colossalTitan + "隻超大型巨人") |
比較運算子 Comparison Operators
之前的運算子
| print(1 + 3 * 3) |
| print("1" + "3" * 3) |
| print(10 / 3) |
| print(10 // 3) |
| print(2 ** 5) |
| print(1 + 3 * 3) |
| print("1" + "3" * 3) |
| print(10 / 3) |
| print(10 // 3) |
| print(2 ** 5) |
如果我們想要做這樣的事情
艾蓮·葉卡:
梟他,將這個稱作"尤彌爾的詛咒"
十三年是始祖尤彌爾從力量覺醒到死去之間的時長
沒人擁有超越始祖尤彌爾的力量
只要接近這個時辰軀體就會衰弱…作為容器的使命就算完成了。
- 始祖尤彌爾的力量大於所有人的力量
- 繼承巨人之力的人不會活超過13年
要怎麼讓電腦聽懂呢
| powerYmir = 9999 |
| powerEren = 100 |
| statement1 = powerYmir > powerYmir |
| yearYmir = 13 |
| yearEren = 13 |
| statement2 = yearEren <= yearYmir |
比較運算子 continued
Operator |
Name |
Example |
== |
Equal |
x == y |
!= |
Not equal |
x != y |
> |
Greater than |
x > y |
< |
Less than |
x < y |
>= |
Greater than or equal to |
x >= y |
<= |
Less than or equal to |
x <= y |
非常重要的重點
| a, b, c, d = 1, 2, 3, 4 |
| firstBool, SecondBool = a = b, c == d |
| print(firstBool) |
| print(SecondBool) |
= != == (重要)
| a, b, c, d = 1, 2, 3, 4 |
| firstBool = a = b |
| SecondBool = c == d |
| print(firstBool) |
| print(SecondBool) |
如果我們還想要做這樣的事情
阿爾敏·亞魯雷特:
再次分成兩組搜索牆面,尤其是大門上方部位
無倫艾蓮何時出現賭塞牆壁都能做出對應的位置
任何時候都能看清並把握狀況的位置…
並且在時機到來前都能安全藏身的位置
要怎麼讓電腦聽懂呢
| respondAnytime = True |
| niceView = True |
| safe = True |
| optimalLocation = respondAnytime and niceView \ |
| and safe |
邏輯運算子 continued
Operator |
Name |
Example |
and |
Returns True if both statements are true |
x < 5 and x < 10 |
or |
Returns True if one of the statements is true |
x < 5 or x < 4 |
not |
Reverse the result, returns False if the result is true |
not(x < 5 and x < 10) |
善用括號
| print(not True and False or True or False) |
| print(not((True and False) or (True or False))) |
進擊的巨人again
米卡莎·阿卡曼:
如果你加入憲兵團,那麼我也會去
而要是你加入駐紮兵團,我也會照做
艾蓮,如果你不在我的身邊,一定會早死的。
艾蓮加入調查兵團->米卡莎加入調查兵團
艾蓮加入憲兵團->米卡莎加入憲兵團
艾蓮加入紮駐兵團->米卡莎加入紮駐兵團
(注意一個人一次只能加入一個兵團)
一樣我們想要讓電腦聽懂
| erenJoinSurveyCorps = True |
| erenJoinGarrison = False |
| erenJoinMilitaryPoliceBrigade = False |
| |
| if erenJoinSurveyCorps: |
| print("米卡莎加入調查兵團") |
| if erenJoinMilitaryPoliceBrigade: |
| print("米卡莎加入憲兵團") |
| if erenJoinGarrison: |
| print("米卡莎加入紮駐兵團") |
不合理的敘述
| erenJoinSurveyCorps = True |
| erenJoinGarrison = True |
| erenJoinMilitaryPoliceBrigade = True |
| |
| if erenJoinSurveyCorps: |
| print("米卡莎加入調查兵團") |
| if erenJoinMilitaryPoliceBrigade: |
| print("米卡莎加入憲兵團") |
| if erenJoinGarrison: |
| print("米卡莎加入紮駐兵團") |
改成合理的敘述
| erenJoinSurveyCorps = True |
| erenJoinGarrison = True |
| erenJoinMilitaryPoliceBrigade = True |
| |
| if erenJoinSurveyCorps: |
| print("米卡莎加入調查兵團") |
| elif erenJoinMilitaryPoliceBrigade: |
| print("米卡莎加入憲兵團") |
| else: |
| print("米卡莎加入紮駐兵團") |
if elif else 三兄弟
| income = int(input()) |
| tax = 0 |
| if income < 0: |
| tax = 0 |
| elif income < 100: |
| tax = income * 0.1 |
| elif income < 200: |
| tax = income * 0.2 |
| elif income < 300: |
| tax = income * 0.3 |
| else: |
| tax = income * 0.4 |
| print(tax) |
| computer = 10 |
| user = int(input()) |
| |
| if user > 0: |
| if user > computer: |
| print("you win") |
| else: |
| print("you lose") |
| computer = 10 |
| user = int(input()) |
| |
| if user > 0: |
| if user > computer: |
| print("you win") |
| else: |
| print("you lose") |
縮排是啥?
- 縮排=程式碼開頭的空白。
- Python使用縮排表示一個區塊的程式。
錯誤範例
| a, b = 3, 5 |
| if a > b: |
| print("a is greater than b") |
| print("python ended") |
正確範例
| a, b = 3, 5 |
| if a > b: |
| print("a is greater than b") |
| print("python ended") |
縮排是啥? continued
- 空白的個數為自訂,原則上為四個空白。
- 同一個區塊內的空白數量需一致。
- 區塊內不可空白(可用pass規避)
錯誤範例 again
| if 5 > 2: |
| print("Five is greater than two!") |
| print("Five is greater than two!") |
正確範例 again
| if 5 > 2: |
| print("Five is greater than two!") |
| print("Five is greater than two!") |
Nested if
| a, b, c = 1, 2, 3 |
| if c > b: |
| if c > a: |
| print("c is the biggest number") |
代表我們可以這樣?

要善用邏輯運算子
| a, b, c = 1, 2, 3 |
| if c > b and c > a: |
| print("c is the biggest number") |
使用條件句的邏輯整理 (重要)
- 開頭一定會有if,其他都是非必要的。
- 上一個條件不符合時,繼續判斷下一個條件,使用elif
- 所有條件都不符合時做某些事,使用else
- if及else只會出現一次,但是可以有複數個elif
- if一定會出現在第一個,else一定會出現在最後一個(但是不一定有else)
冗長的程式碼
| condition = False |
| a = 0 |
| if condition: |
| a = 1 |
| else: |
| a = -1 |
三元運算子 Ternary Operator
| condition = False |
| a = 1 if condition else -1 |
Python if 資訊之芽 梁安哲 2022/03/13
{"metaMigratedAt":"2023-06-16T20:30:31.488Z","metaMigratedFrom":"Content","title":"Python if","breaks":true,"contributors":"[{\"id\":\"9714f580-8aea-4592-8613-b6e594740378\",\"add\":8904,\"del\":2571}]"}