10.5. 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
- True
- Yes, 3.14 is an item in the list alist.
- False
- There are 7 items in the list, 3.14 is one of them.
What is printed by the following statements?
alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
print(3.14 in alist)
Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
- True
- in returns True for top level items only. 57 is in a sublist.
- False
- Yes, 57 is not a top level item in alist. It is in a sublist.
What is printed by the following statements?
alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
print(57 in alist)
You have attempted 1 of 4 activities on this page