日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python 程序:檢查元組中存在的項目

創(chuàng)新互聯(lián)python教程:

創(chuàng)新互聯(lián)是一家專業(yè)提供寧河企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)、成都h5網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為寧河眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。

編寫一個 Python 程序來檢查給定的項是否存在于元組中。我們使用 in 運(yùn)算符來查找元組中存在的項。

# Check Element Presnet in Tuple

numTuple = (4, 6, 8, 11, 22, 43, 58, 99, 16)
print("Tuple Items = ", numTuple)

number = int(input("Enter Tuple Item to Find = "))

result = number in numTuple

print("Does our numTuple Contains the ", number, "? ", result)

雖然上面的例子返回真或假,我們需要一個有意義的消息。因此,如果元組中存在條目,我們使用 if 語句和 in 運(yùn)算符(numTuple 中的 If 數(shù)字)來打印不同的消息。

# Check Element Presnet in Tuple

numTuple = (4, 6, 8, 11, 22, 43, 58, 99, 16)
print("Tuple Items = ", numTuple)

number = int(input("Enter Tuple Item to Find = "))

if number in numTuple:
    print(number, " is in the numTuple")
else:
    print("Sorry! We haven't found ", number, " in numTuple")
Tuple Items =  (4, 6, 8, 11, 22, 43, 58, 99, 16)
Enter Tuple Item to Find = 22
22  is in the numTuple
>>> 
Tuple Items =  (4, 6, 8, 11, 22, 43, 58, 99, 16)
Enter Tuple Item to Find = 124
Sorry! We haven't found  124  in numTuple
>>>

使用 For 循環(huán)檢查元組中是否存在項目的 Python 程序

在這個 Python 示例中,我們使用 if 語句(if val == number)對照給定的數(shù)字檢查每個元組項。如果為真,則結(jié)果為真,編譯器將從 for 循環(huán)中斷開。

# Check Element Presnet in Tuple

numTuple = (4, 6, 8, 11, 22, 43, 58, 99, 16)
print("Tuple Items = ", numTuple)

number = int(input("Enter Tuple Item to Find = "))

result = False

for val in numTuple:
    if val == number:
        result = True
        break

print("Does our numTuple Contains the ", number, "? ", result)
Tuple Items =  (4, 6, 8, 11, 22, 43, 58, 99, 16)
Enter Tuple Item to Find = 16
Does our numTuple Contains the  16 ?  True

標(biāo)題名稱:Python 程序:檢查元組中存在的項目
當(dāng)前鏈接:http://www.dlmjj.cn/article/dpcsjhd.html