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

創(chuàng)新互聯(lián)是一家專業(yè)提供寧河企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、外貿(mào)營(yíng)銷(xiā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)行中。
編寫(xiě)一個(gè) Python 程序來(lái)檢查給定的項(xiàng)是否存在于元組中。我們使用 in 運(yùn)算符來(lái)查找元組中存在的項(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)雖然上面的例子返回真或假,我們需要一個(gè)有意義的消息。因此,如果元組中存在條目,我們使用 if 語(yǔ)句和 in 運(yùn)算符(numTuple 中的 If 數(shù)字)來(lái)打印不同的消息。
# 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)檢查元組中是否存在項(xiàng)目的 Python 程序
在這個(gè) Python 示例中,我們使用 if 語(yǔ)句(if val == number)對(duì)照給定的數(shù)字檢查每個(gè)元組項(xiàng)。如果為真,則結(jié)果為真,編譯器將從 for 循環(huán)中斷開(kāi)。
# 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)頁(yè)名稱:Python 程序:檢查元組中存在的項(xiàng)目
本文地址:http://www.5511xx.com/article/dpcsjhd.html


咨詢
建站咨詢
