Check for the Existence of Elements
00:00
In this lesson, you’ll take a look at how you can check for the existence of values using the in
keyword. That’s something I just wanted to mention briefly.
00:08
You can use the in
keyword to check whether a value is inside of a tuple. And you can do that, like, so let’s say we have a vowels
tuple that contains the vowels of the English alphabet, "a", "e", "i", "o", "u"
.
00:24
And then you could go ahead and say, is "o"
in vowels
by just typing the string "o"
in vowels
, and then Python is going to return True
because "o"
is an element in the tuple vowels
.
00:39
Then to try the opposite, if I say "x"
, the character "x"
in vowels
, then Python’s going to return False
because that’s not part of the vowels
tuple.
00:50 Now you can do this all with non-string data. So again, so there are numbers
00:59
1, 2, 3
, so a tuple that consists of the integers 1
, 2
, and 3
. And similarly by typing 1 in numbers
and Python’s going to return True
.
01:10
And if you try 100 in numbers
—
01:15
got to spell numbers
correctly, let’s try again—100 in numbers
, then Python returns False
. And this is how you can check whether a value is an element of a tuple by using the in
keyword.
Become a Member to join the conversation.