1 00:00:00,150 --> 00:00:02,940 In this video, we'll be discussing membership testing, in other words, 2 00:00:02,940 --> 00:00:06,860 whether or not an item or sequence is inside another sequence. 3 00:00:06,860 --> 00:00:10,240 Membership testing uses the operators in and not in. 4 00:00:10,240 --> 00:00:13,430 Take a look at my workspace here, you can see I have a really long string, 5 00:00:13,430 --> 00:00:15,810 really an entire paragraph from the Python documentation. 6 00:00:15,810 --> 00:00:20,480 I want to know whether this documentation excerpt contains the string tuple. 7 00:00:20,480 --> 00:00:22,826 To find that out, I can use the operator in. 8 00:00:22,826 --> 00:00:28,107 The expression, 'tuple' in docs, will evaluate to true or 9 00:00:28,107 --> 00:00:33,605 false, true if tuple appears in docs, and false if it does not. 10 00:00:33,605 --> 00:00:36,815 You'll see this expression most often used in conjunction with an if statement. 11 00:00:51,907 --> 00:00:54,292 So I'm gonna add just a little bit of code to this, and 12 00:00:54,292 --> 00:00:56,317 then we're gonna run it to see what happens. 13 00:01:11,778 --> 00:01:15,721 Okay, save and run. 14 00:01:21,920 --> 00:01:26,040 Great, this tells us that the string tuple is in fact inside this longer string docs. 15 00:01:27,280 --> 00:01:29,890 Alternatively, we can use the not in operator. 16 00:01:29,890 --> 00:01:31,840 This is the opposite of the in operator. 17 00:01:31,840 --> 00:01:35,950 Not in will return true if the tested object is not in the sequence, and 18 00:01:35,950 --> 00:01:37,850 will return false if it is. 19 00:01:37,850 --> 00:01:40,715 To demonstrate this, I'll modify our example code just a little bit. 20 00:01:42,926 --> 00:01:47,825 I'll change the in operator to not in, and then I'll change our print statements. 21 00:01:53,773 --> 00:01:56,542 Now when we run this again, we should see the same result. 22 00:01:59,889 --> 00:02:03,820 Yes, the string tuple is present inside the larger string docs. 23 00:02:03,820 --> 00:02:05,768 That's the gist of membership testing. 24 00:02:05,768 --> 00:02:07,370 I've demonstrated it here with strings, but 25 00:02:07,370 --> 00:02:09,770 these operators work for all sequences. 26 00:02:09,770 --> 00:02:13,460 See the following instruction step for code examples showing how to use in and 27 00:02:13,460 --> 00:02:15,290 not in with lists and tuples as well.