Python中IF語句的多條件檢查


編寫程式時,通常需要檢查多個條件才能確定合適的行動方案。在 Python 中,"if" 語句用於在特定條件為 True 時執行程式碼塊。但是,在許多情況下,我們需要一次檢查多個條件,這可以透過使用邏輯運算子、括號和其他工具來實現。

在本文中,我們將探討使用 Python 在 "if" 語句中檢查多個條件的幾種技術。我們將討論使用邏輯運算子 and、or 和 not,以及使用括號將條件組合在一起。我們還將介紹使用 "in" 和 "not in" 關鍵字檢查專案是否在列表或字串中,以及使用 if-elif-else 語句順序檢查多個條件。

閱讀完本文後,您應該對在 Python 的 if 語句中檢查多個條件的各種方法有紮實的理解。無論您是初學者還是經驗豐富的開發者,理解這些技術將幫助您編寫更有效率的程式碼。所以,讓我們深入探討 Python 中多條件檢查的世界吧!

Python 中的基本 If 語句

Python 中 "if" 語句的基本語法是:

if condition:
   # code to execute if condition is true

"if" 語句中的條件可以是任何計算結果為布林值(True 或 False)的表示式。如果條件為 True,則將執行 if 語句後的程式碼塊。如果條件為 False,則將跳過程式碼塊。

這是一個 Python 中簡單 if 語句的示例:

x = 5
if x > 0:
   print("x is positive")

在這個例子中,我們檢查 x 的值是否大於 0。如果條件為真,它將列印字串 "x is positive"。

x is positive

使用邏輯運算子檢查多個條件

有時我們需要同時檢查多個條件。在 Python 中,我們可以使用邏輯運算子將多個條件組合到 if 語句中。

Python 中的三個邏輯運算子是:

  • and - 如果兩個條件都為 True,則返回 True

  • or - 如果至少一個條件為 True,則返回 True

  • not - 返回條件的反義

讓我們看一些例子。

示例 1

在這個例子中,我們檢查 x 和 y 是否都大於 0。如果兩個條件都為真,我們列印字串 "Both x and y are positive"。

x = 5
y = 10
if x > 0 and y > 0:
   print("Both x and y are positive")

輸出

它將產生以下輸出:

Both x and y are positive

示例 2

在這個例子中,我們檢查 x 和 y 中至少一個是否大於 0。如果任一條件為真,我們列印字串 "At least one of x and y is positive"。

x = 5
y = 10

if x > 0 or y > 0:
   print("At least one of x and y is positive")

輸出

它將產生以下輸出:

At least one of the x and y is positive

示例 3

在這個例子中,我們檢查 x 是否不小於 0。如果條件為真,我們列印字串 "x is not negative"。

x = 5

if not x < 0:
   print("x is not negative")

輸出

它將產生以下輸出:

x is not negative

使用括號組合多個條件

我們也可以使用括號將條件組合到 "if" 語句中。當我們有需要按特定順序計算的複雜條件時,這很有用。讓我們來看一個例子。

示例

在這個例子中,我們檢查 x 和 y 是否都大於 0,或者 x 和 y 是否都小於 0。如果任一條件為真,我們列印字串 "Both x and y are positive or both x and y are negative"。

x = 5
y = 10

if (x > 0 and y > 0) or (x < 0 and y < 0):
   print("Both x and y are positive or both x and y are negative")

輸出

它將產生以下輸出:

Both x and y are positive or both x and y are negative

使用 In 關鍵字檢查專案是否在列表或字串中

在 "if" 語句中檢查多個條件的另一種方法是使用 in 關鍵字。"in" 關鍵字用於檢查專案是否在列表或字串中。

示例 1

這是一個使用 "in" 關鍵字檢查專案是否在列表中的示例。在這個例子中,我們檢查 "apple" 是否在水果列表中。如果條件為真,我們列印字串 "We have apples!"。

fruits = ["apple", "banana", "orange"]
if "apple" in fruits:
   print("We have apples!")

輸出

它將產生以下輸出:

We have apples!

示例 2

我們也可以使用 "in" 關鍵字檢查子字串是否在字串中。在這個例子中,我們檢查子字串 "fox" 是否在字串 "The quick brown fox jumps over the lazy dog" 中。如果條件為真,我們列印字串 "We have a fox!"。

sentence = "The quick brown fox jumps over the lazy dog"
if "fox" in sentence:
   print("We have a fox!")

輸出

它將產生以下輸出:

We have a fox!

使用 Not 關鍵字檢查專案是否不在列表或字串中

我們也可以使用 not 關鍵字檢查專案是否不在列表或字串中。這是一個例子:

示例

在這個例子中,我們檢查 "mango" 是否不在水果列表中。如果條件為真,我們列印字串 "We don't have mangoes :("。

fruits = ["apple", "banana", "orange"]
if "mango" not in fruits:
   print("We don't have mangoes :(")

輸出

它將產生以下輸出:

We don't have mangoes :(

組合 "in" 和 "not in" 關鍵字

我們也可以組合 "in" 和 "not in" 關鍵字來檢查更復雜的條件。這是一個例子:

示例

在這個例子中,我們檢查 "mango" 是否不在水果列表中,並且 "apple" 或 "orange" 是否在水果列表中。如果條件為真,我們列印字串 "We have apples or oranges, but no mangoes :("。

fruits = ["apple", "banana", "orange"]
if "mango" not in fruits and ("apple" in fruits or "orange" in fruits):
   print("We have apples or oranges, but no mangoes :(")

輸出

它將產生以下輸出:

We have apples or oranges, but no mangoes :(

使用 if-elif-else 語句檢查多個條件

當我們需要順序檢查多個條件時,可以使用 if-elif-else 語句。if-elif-else 語句的語法是:

if condition1:
   # code to execute if condition1 is true
elif condition2:
   # code to execute if condition1 is false and condition2 is true
else:
   # code to execute if both condition1 and condition2 are false

現在,讓我們來看一些例子。

示例 1

在這個例子中,我們檢查 x 是否大於 0。如果條件為真,我們列印字串 "x is positive"。如果條件為假且 x 等於 0,我們列印字串 "x is zero"。如果兩個條件都為假,我們列印字串 "x is negative"。

x = 5
if x > 0:
   print("x is positive")
elif x == 0:
   print("x is zero")
else:
   print("x is negative")

輸出

它將產生以下輸出:

"X is positive"

示例 2

在這個例子中,我們檢查 x 是否大於 0。如果條件為真,我們列印字串 "x is positive"。如果條件為假且 x 等於 0,我們列印字串 "x is zero"。如果兩個條件都為假,我們列印字串 "x is negative"。

x = -5
if x > 0:
   print("x is positive")
elif x == 0:
   print("x is zero")
else:
   print("x is negative")

輸出

它將產生以下輸出:

"X is negative"

示例 3

在這個例子中,我們檢查 x 是否大於 0。如果條件為真,我們列印字串 "x is positive"。如果條件為假且 x 等於 0,我們列印字串 "x is zero"。如果兩個條件都為假,我們列印字串 "x is negative"。

x = 0
if x > 0:
   print("x is positive")
elif x == 0:
   print("x is zero")
else:
   print("x is negative")

輸出

它將產生以下輸出:

"X is zero"

結論

總之,多條件檢查是程式設計的一個重要方面,它使我們能夠建立更復雜和通用的程式。在 Python 中,有多種方法可以在 if 語句中檢查多個條件,包括邏輯運算子、括號、in 關鍵字、not in 關鍵字和 if-elif-else 語句。

透過利用這些技術,我們可以建立更靈活的程式,能夠處理各種情況。此外,理解如何有效地檢查多個條件可以使我們的程式碼更高效,減少錯誤和漏洞的可能性。

總的來說,掌握 Python 中多條件檢查的技巧可以極大地提高我們的程式設計技能,使我們能夠建立更復雜的程式,以處理多樣化和複雜的情況。

更新於:2023年8月21日

11K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始學習
廣告