新聞中心
這里有您想知道的互聯(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è)、政府機構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進行中。
編寫一個 Python 程序來檢查給定的項是否存在于元組中。我們使用 in 運算符來查找元組中存在的項。
# 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 運算符(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 網(wǎng)站標(biāo)題:Python 程序:檢查元組中存在的項目
當(dāng)前URL:http://fisionsoft.com.cn/article/dpcsjhd.html


咨詢
建站咨詢
