掃二維碼與項(xiàng)目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
創(chuàng)新互聯(lián)python教程:

創(chuàng)新互聯(lián)是一家專業(yè)提供寧河企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、外貿(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 程序來檢查給定的項(xiàng)是否存在于元組中。我們使用 in 運(yùn)算符來查找元組中存在的項(xiàng)。
# 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
>>>在這個 Python 示例中,我們使用 if 語句(if val == number)對照給定的數(shù)字檢查每個元組項(xiàng)。如果為真,則結(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 
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流