詳解Python中三元運算符的使用
條件表達(dá)式(有時稱為“三元運算符”)在所有Python操作中優(yōu)先級最低。三元運算符根據(jù)條件為真或假來計算某些東西。
它只允許在單行中測試條件,取代多行if-else,使代碼緊湊。
語法:
[on_true] if [expression] else [on_false]
expression : conditional_expression | lambda_expr
使用三元運算符的簡單方法
# Program to demonstrate conditional operator a, b = 10, 20 # Copy value of a in min if a < b else copy b min = a if a < b else b print(min)
輸出
10
說明:表達(dá)式min用于根據(jù)給定條件打印a或b。例如,如果a小于b,則輸出是a,如果a不小于b,則輸出是b。
使用元組、字典和lambda
# Python program to demonstrate ternary operator a, b = 10, 20 # Use tuple for selecting an item # (if_test_false,if_test_true)[test] # if [a<b] is true it return 1, so element with 1 index will print # else if [a<b] is false it return 0, so element with 0 index will print print( (b, a) [a < b] ) # Use Dictionary for selecting an item # if [a < b] is true then value of True key will print # else if [a<b] is false then value of False key will print print({True: a, False: b} [a < b]) # lambda is more efficient than above two methods # because in lambda we are assure that # only one expression will be evaluated unlike in # tuple and Dictionary print((lambda: b, lambda: a)[a < b]())
輸出
10
10
10
三元運算符可以寫成嵌套的if-else
# Python program to demonstrate nested ternary operator a, b = 10, 20 print ("Both a and b are equal" if a == b else "a is greater than b" if a > b else "b is greater than a")
上述方法可以寫成:
a, b = 10, 20 if a != b: if a > b: print("a is greater than b") else: print("b is greater than a") else: print("Both a and b are equal")
輸出
b is greater than a
在三元運算符中使用print函數(shù)
示例:在python中使用三元運算符查找2者較大的數(shù)
a=5 b=7 # [statement_on_True] if [condition] else [statement_on_false] print(a,"is greater") if (a>b) else print(b,"is Greater")
輸出
7 is Greater
注意事項:
首先對給定的條件求值(a < b),然后根據(jù)條件返回的布爾值返回a或b
操作符中參數(shù)的順序與其他語言(如C/C++)不同。
條件表達(dá)式在所有Python操作中具有最低的優(yōu)先級。
使用for循環(huán)
要在for循環(huán)中使用三元運算符,可以按照以下步驟操作:
使用for語句循環(huán)要計算的數(shù)據(jù)。
使用三元運算符計算數(shù)據(jù)中的每個元素。
打印每個元素的三元運算符的結(jié)果。
在下例中,要評估的數(shù)據(jù)存儲在稱為data的列表中。for語句用于循環(huán)遍歷列表中的每個元素。三元運算符用于確定每個數(shù)字是偶數(shù)還是奇數(shù)。三元運算符的結(jié)果存儲在名為result的變量中。print()語句用于打印列表中每個元素的三元運算符的結(jié)果。
# Define the data to be evaluated data = [3, 5, 2, 8, 4] # Use a for loop to evaluate each element in the data for num in data: # Use the ternary operator to determine if the number is even or odd result = 'even' if num % 2 == 0 else 'odd' # Optionally, print the result of the ternary operator for each element print(f'The number {num} is {result}.')
輸出
The number 3 is odd.
The number 5 is odd.
The number 2 is even.
The number 8 is even.
The number 4 is even.
代碼定義了一個稱為data的整數(shù)列表。然后它使用for循環(huán)遍歷列表中的每個元素。對于每個元素,它使用三元運算符來檢查它是偶數(shù)還是奇數(shù),并將結(jié)果保存到名為result的變量中。然后,代碼可選地打印每個元素的三元運算符的結(jié)果。
到此這篇關(guān)于詳解Python中三元運算符的使用的文章就介紹到這了,更多相關(guān)Python三元運算符內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談python socket函數(shù)中,send與sendall的區(qū)別與使用方法
下面小編就為大家?guī)硪黄獪\談python socket函數(shù)中,send與sendall的區(qū)別與使用方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05python中response.text 和response.content的區(qū)別詳解
這篇文章主要介紹了python中response.text 和response.content的區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05python tkinter與Mysql數(shù)據(jù)庫交互實現(xiàn)賬號登陸
本文主要介紹了python tkinter與Mysql數(shù)據(jù)庫交互實現(xiàn)賬號登陸,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01