Locked lesson.
About this lesson
We'll look at the membership operators "In", and "Not In".
Exercise files
Download this lesson’s related exercise files.
Membership Operators.docx59 KB Membership Operators - Solution.docx
59.1 KB
Quick reference
Membership Operators
It's easy to test whether an item exists inside a given list, tuple, dictionary, or variable.
When to use
Use membership operators whenever you want to see if an item exists in a list, tuple, dictionary, or variable.
Instructions
The two membership operators are "In" and "Not In":
"john" in my_list #returns true if exists, false if not
"john" not in my_list #returns true if not exists, false if not
Hints & tips
- In and Not In test for membership
- 00:04 In this video, I want to look at membership operators.
- 00:07 We have lists, we have tuples, and we have if else statements.
- 00:11 Now we can sort of merge them together and look at different membership options.
- 00:15 Let's start out by creating a list and let's just call it names.
- 00:20 And let's just pass in John, Tim, and Mary like we have before,
- 00:26 so we can test to see if a certain item is in our list.
- 00:30 We can do that using membership operators and
- 00:32 the two membership operators are in and not in.
- 00:36 So we want to see if something is in this list or if something is not in this list.
- 00:41 So to do this, we need to create an if statement, and let's say,
- 00:45 if John in names,
- 00:50 print Yes, John is in our list.
- 00:56 And let's go else, print No, John is not in our list.
- 01:04 So if we save this and run it, we see yes, John is in our list.
- 01:09 And that's all we have to do instead of our comparison operator and
- 01:12 our conditional statement here, we just use the word in, lowercase in, so
- 01:16 this is case sensitive.
- 01:18 So if you look at this, we change this to lowercase john.
- 01:22 We see no, john is not in our list because we have capital John,
- 01:26 not lowercase and Python is case sensitive, most of the time.
- 01:30 So that's in, very, very easy.
- 01:32 We can go not in, and let's change this back to a capital John.
- 01:39 And let's change our logic around, so if John is not in, we'll print out No,
- 01:44 John is not in our list.
- 01:47 Otherwise, Yes, John is in our list.
- 01:52 So, okay.
- 01:53 So look at this, if John is not in the list print out, no,
- 01:56 he's not in list otherwise, he is in the list.
- 01:59 So if we save this and run it, Yes, John is in our list.
- 02:03 If we go Bobberson, is Bobberson not in our list?
- 02:08 No well, got this typed in as John.
- 02:12 So, let me change this to the name is not in our list.
- 02:16 Yes, the name, there we go.
- 02:19 Make it a little bit easier.
- 02:21 No, the name is not on our list.
- 02:22 So, Bobberson is not, so in, not in, very easy.
- 02:27 Membership operators, we're testing to see if it is a member of this list or
- 02:32 a member of this tuple, or a member of whatever.
- 02:36 So we can change this to a tuple and well, let's just keep this all the same.
- 02:40 We get this same exact thing.
- 02:41 It works for lists, it works for tuples, works for variables even,
- 02:46 so we can change this to John.
- 02:49 Save it and run it, no,
- 02:51 the name is not in our list if we change Bobberson back to John and run this.
- 02:57 Yes, the name is in our list.
- 02:58 Yes, John is in our, well, it's not a list anymore.
- 03:02 It's a variable but you get the idea.
- 03:04 So, you can test against strings,
- 03:06 you can test against numbers, all the different things, very, very easy.
- 03:10 So that's membership, those are membership operators.
- 03:12 In the next video, we're going to look at identity operators.
Lesson notes are only available for subscribers.