條件判斷是編程中必不可少的一部分,它讓程序可以根據不同的條件執行不同的代碼塊。在Python中,主要使用if
、elif
和else
語句來實現條件判斷。
基本語法
在Python中,條件判斷的基本語法如下:
if condition:# 當condition為True時,執行這段代碼pass
elif another_condition:# 當condition為False且another_condition為True時,執行這段代碼pass
else:# 當上述所有條件都為False時,執行這段代碼pass
if
語句
if
語句用來測試一個條件。如果條件為True
,則執行相應的代碼塊;否則,跳過該代碼塊。例如:
age = 18
if age >= 18:print("You are an adult.")
在這個例子中,如果變量age
的值大于或等于18,則會輸出"You are an adult."。
elif
語句
elif
是else if
的縮寫,用于測試另外一個條件。elif
只能在if
語句之后使用,可以有多個elif
條件。例如:
age = 15
if age >= 18:print("You are an adult.")
elif age >= 13:print("You are a teenager.")
else:print("You are a child.")
在這個例子中,程序會先檢查age >= 18
是否為True
,如果是則輸出"You are an adult.",如果不是則檢查age >= 13
是否為True
,如果是則輸出"You are a teenager.",否則輸出"You are a child."。
else
語句
else
語句在所有條件都不滿足時執行相應的代碼塊。一個if
語句塊中最多只能有一個else
語句,并且它必須放在所有if
和elif
語句的最后。例如:
age = 10
if age >= 18:print("You are an adult.")
elif age >= 13:print("You are a teenager.")
else:print("You are a child.")
在這個例子中,由于age
小于13,所有條件都不滿足,所以程序會執行else
語句,輸出"You are a child."。
嵌套條件判斷
有時我們需要在一個條件判斷中再進行一個條件判斷,這種情況稱為嵌套條件判斷。例如:
age = 20
if age >= 18:print("You are an adult.")if age >= 65:print("You are a senior.")else:print("You are not a senior yet.")
else:print("You are not an adult.")
在這個例子中,如果age
大于或等于18,程序會進一步檢查是否大于或等于65。如果是,則輸出"You are a senior.",否則輸出"You are not a senior yet."。如果age
小于18,程序直接輸出"You are not an adult."。
使用布爾操作符
在條件判斷中,我們可以使用布爾操作符來組合多個條件。常用的布爾操作符有and
、or
和not
。
and
操作符
and
操作符用于連接兩個條件,只有當兩個條件都為True
時,整個條件才為True
。例如:
age = 20
has_id = Trueif age >= 18 and has_id:print("You can enter the club.")
else:print("You cannot enter the club.")
在這個例子中,只有當age
大于或等于18且has_id
為True
時,程序才會輸出"You can enter the club."。
or
操作符
or
操作符用于連接兩個條件,只要其中一個條件為True
,整個條件就為True
。例如:
age = 20
has_id = Falseif age >= 18 or has_id:print("You can enter the club.")
else:print("You cannot enter the club.")
在這個例子中,只要age
大于或等于18,或者has_id
為True
,程序就會輸出"You can enter the club."。
not
操作符
not
操作符用于取反,即將True
變為False
,將False
變為True
。例如:
is_raining = Falseif not is_raining:print("You don't need an umbrella.")
else:print("You need an umbrella.")
在這個例子中,由于is_raining
為False
,not is_raining
為True
,所以程序會輸出"You don't need an umbrella."。
多重條件判斷
在實際應用中,我們常常需要檢查多個條件。這可以通過多個elif
語句來實現。例如:
score = 85if score >= 90:print("Grade: A")
elif score >= 80:print("Grade: B")
elif score >= 70:print("Grade: C")
elif score >= 60:print("Grade: D")
else:print("Grade: F")
在這個例子中,程序會依次檢查每個條件,直到找到一個滿足的條件,然后執行相應的代碼塊。如果score
大于或等于90,輸出"Grade: A";如果大于或等于80但小于90,輸出"Grade: B",以此類推。
條件表達式
Python還提供了一種簡潔的條件表達式(也稱為三元運算符),它允許在一行代碼中實現簡單的條件判斷。其語法如下:
value_if_true if condition else value_if_false
例如:
age = 20
status = "adult" if age >= 18 else "minor"
print(status)
在這個例子中,如果age
大于或等于18,status
的值為"adult";否則,status
的值為"minor"。
實戰案例
下面通過幾個實際案例來展示如何在編程中使用條件判斷。
案例一:判斷用戶輸入的數字是奇數還是偶數
number = int(input("Enter a number: "))if number % 2 == 0:print("The number is even.")
else:print("The number is odd.")
在這個例子中,程序首先讀取用戶輸入的數字,并將其轉換為整數。然后通過取模運算number % 2
來判斷該數字是奇數還是偶數。如果結果為0,說明是偶數,否則是奇數。
案例二:根據用戶輸入的分數判斷成績等級
score = int(input("Enter your score: "))if score >= 90:grade = "A"
elif score >= 80:grade = "B"
elif score >= 70:grade = "C"
elif score >= 60:grade = "D"
else:grade = "F"print("Your grade is:", grade)
在這個例子中,程序根據用戶輸入的分數,判斷并輸出對應的成績等級。
案例三:根據用戶輸入的年份判斷是否為閏年
year = int(input("Enter a year: "))if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):print(year, "is a leap year.")
else:print(year, "is not a leap year.")
在這個例子中,程序根據閏年的判斷規則,判斷并輸出用戶輸入的年份是否為閏年。
總的來說,條件判斷是編程中的基本構建塊,它使程序能夠根據不同的條件執行不同的代碼。掌握if
、elif
和else
語句,以及布爾操作符的使用,對于編寫復雜的程序至關重要。