Skip to main content\(
\newcommand{\lt}{<}
\newcommand{\gt}{>}
\newcommand{\amp}{&}
\definecolor{fillinmathshade}{gray}{0.9}
\newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}}
\)
Section 5.7 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.
Remember that
in
and
not in
only checks for membership in the top level of a list.
Checkpoint 5.7.1.
What is printed by the following statements?
alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
print(3.14 in alist)
- Yes, 3.14 is an item in the list alist.
- There are 7 items in the list, 3.14 is one of them.
Checkpoint 5.7.2.
What is printed by the following statements?
alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
print(57 in alist)
- in returns True for top level items only. 57 is in a sublist.
- Yes, 57 is not a top level item in alist. It is in a sublist.
You have attempted
of
activities on this page.