Att kontrollera om ett element finns i en tuple i Python

Att ta reda på om ett visst element finns i en tuple görs med operatorn in:

pythontpl = ('a', 'b' 'c')
res = 'a' in tpl

print(res) # skriver ut True

För att kontrollera frånvaron av ett element bör operatorn not in användas:

pythontpl = ('a', 'b' 'c')
res = 'a' not in tpl

print(res) # skriver ut False
Задание 1

Given en tuple:

pythontpl = (2, 4, 6, 10)

Kontrollera om talet 8 finns i den.

Задание 2

Given en tuple:

pythontpl = ('abc', 'def')

Kontrollera om strängen 'd' finns i den.

Задание 3

Given följande kod:

pythontpl = ('1', '2', '3')
res = 1 not in tpl

print(res)

Säg vad som kommer att skrivas ut i konsolen.

Задание 4

Given följande kod:

pythontpl1 = ('ac', '3', 4, 'bd', 5)
tpl2 = (1, 2, 3)

res1 = 4 in tpl1
res2 = 2 not in tpl2
print(res1)
print(res2)

Säg vad som kommer att skrivas ut i konsolen.