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 9.4 List operations
The
+ operator concatenates lists:
Similarly, the
* operator repeats a list a given number of times:
The first example repeats four times. The second example repeats the list three times.
Activity 9.4.1.
What is printed by the following statements?
alist = [4, 2, 8, 6, 5]
alist = alist + 999
print(alist)
-
[4, 2, 8, 6, 5, 999]
-
You cannot concatenate a list with an integer.
-
Error, you cannot concatenate a list with an integer.
-
Yes, in order to perform concatenation you would need to write alist+[999]. You must have two lists.
-
[[4, 2, 8, 6, 5], 999]
-
You cannot concatenate a list with an integer. This would cause an error, not create a new list.
-
[4, 2, 8, 6, 5]
-
This will cause an error, but alist will remain unchanged.
Activity 9.4.2.
What is printed by the following statements?
alist = [1, 3, 5]
blist = [2, 4, 6]
print(alist + blist)
-
6
-
Concatenation does not add the lengths of the lists.
-
[1, 2, 3, 4, 5, 6]
-
Concatenation does not reorder the items.
-
[1, 3, 5, 2, 4, 6]
-
Yes, a new list with all the items of the first list followed by all those from the second.
-
[3, 7, 11]
-
Concatenation does not add the individual items.
Activity 9.4.3.
What is printed by the following statements?
alist = [1, 3, 5]
print(alist * 3)
-
9
-
Repetition does not multiply the lengths of the lists. It repeats the items.
-
[1, 1, 1, 3, 3, 3, 5, 5, 5]
-
Repetition does not repeat each item individually.
-
[1, 3, 5, 1, 3, 5, 1, 3, 5]
-
Yes, the items of the list are repeated 3 times, one after another.
-
[3, 9, 15]
-
Repetition does not multiply the individual items.
You have attempted
of
activities on this page.