List MembershipΒΆ

in and not in are boolean operators that test membership in a sequence. We used them previously with strings and they also work here.

Check your understanding

    rec-5-1: What is printed by the following statements?

    alist = [3, 67, "cat", 3.14, False]
    print("cat" in alist)
    
  • True
  • Yes, 'cat' is an item in the list alist.
  • False
  • There are 5 items in the list, 'cat' is one of them.

    rec-5-2: What is printed by the following statements?

    alist = [3, 67, "cat", 3.14, False]
    print("at" in alist)
    
  • True
  • "at" is in "cat", but it is not in alist
  • False
  • Yes, "at" is not in the top level item, alist. It is in one of the elements of alist.
Next Section - Concatenation and Repetition