最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Python中三元運算符的使用

 更新時間:2023年09月05日 08:56:23   作者:python收藏家  
條件表達(dá)式(有時稱為“三元運算符”)在所有Python操作中優(yōu)先級最低,三元運算符根據(jù)條件為真或假來計算某些東西,下面就跟隨小編一起來看看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)文章

最新評論

肃南| 荆州市| 麦盖提县| 莱州市| 大关县| 北碚区| 五台县| 秀山| 天台县| 衡阳县| 丹寨县| 和龙市| 万宁市| 云南省| 青海省| 中超| 乌海市| 克山县| 淮南市| 松滋市| 高州市| 新兴县| 吴堡县| 成安县| 安西县| 西吉县| 水城县| 清新县| 奉贤区| 兴城市| 东乡族自治县| 钟祥市| 富民县| 太原市| 临潭县| 当雄县| 义乌市| 海晏县| 济南市| 句容市| 楚雄市|