Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
      You have completed Python Sequences!
      
    
You have completed Python Sequences!
Preview
    
      
  Learn how to determine whether your Python sequence contains a particular value or other sequence.
This video doesn't have any notes.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
                      In this video, we'll be discussing
membership testing, in other words,
                      0:00
                    
                    
                      whether or not an item or
sequence is inside another sequence.
                      0:02
                    
                    
                      Membership testing uses
the operators in and not in.
                      0:06
                    
                    
                      Take a look at my workspace here,
you can see I have a really long string,
                      0:10
                    
                    
                      really an entire paragraph
from the Python documentation.
                      0:13
                    
                    
                      I want to know whether this documentation
excerpt contains the string tuple.
                      0:15
                    
                    
                      To find that out,
I can use the operator in.
                      0:20
                    
                    
                      The expression, 'tuple' in docs,
will evaluate to true or
                      0:22
                    
                    
                      false, true if tuple appears in docs,
and false if it does not.
                      0:28
                    
                    
                      You'll see this expression most often
used in conjunction with an if statement.
                      0:33
                    
                    
                      So I'm gonna add just a little
bit of code to this, and
                      0:51
                    
                    
                      then we're gonna run it
to see what happens.
                      0:54
                    
                    
                      Okay, save and run.
                      1:11
                    
                    
                      Great, this tells us that the string tuple
is in fact inside this longer string docs.
                      1:21
                    
                    
                      Alternatively, we can
use the not in operator.
                      1:27
                    
                    
                      This is the opposite of the in operator.
                      1:29
                    
                    
                      Not in will return true if the tested
object is not in the sequence, and
                      1:31
                    
                    
                      will return false if it is.
                      1:35
                    
                    
                      To demonstrate this, I'll modify
our example code just a little bit.
                      1:37
                    
                    
                      I'll change the in operator to not in, and
then I'll change our print statements.
                      1:42
                    
                    
                      Now when we run this again,
we should see the same result.
                      1:53
                    
                    
                      Yes, the string tuple is present
inside the larger string docs.
                      1:59
                    
                    
                      That's the gist of membership testing.
                      2:03
                    
                    
                      I've demonstrated it here with strings,
but
                      2:05
                    
                    
                      these operators work for all sequences.
                      2:07
                    
                    
                      See the following instruction step for
code examples showing how to use in and
                      2:09
                    
                    
                      not in with lists and tuples as well.
                      2:13
                    
              
        You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up