16.8: Chapter 9
- Page ID
- 117504
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
9.1 Modifying and iterating lists
- b. The
append()operation adds an element to the end of a list. - c. The element to be removed is passed as a parameter to the remove operation. The remove is performed on the specified list.
- c. Both
pop()andremove()can be used to remove the last element of a list.pop()automatically removes the last element, whereasremove()requires the last element be specified. - a. The length of the list is 5, so the iterative
forloop executes five times. - b.
len(my_list)is 5. A countingforloop ends at 1 less than the second value in the range function, so the final value ofiis4. - a. The
forloop steps by two each time, so index 0, 2, and 4 are printed. Due to the"end="in the print statement, all elements are printed on one line.
9.2 Sorting and reversing lists
- b. Sorting in descending order means to arrange the elements from largest to smallest. -3 is the smallest element of the list and would be the last element when the list is sorted in descending order.
- a. For strings, ascending order refers to dictionary order.
- c. In alphabetical order, "h" comes before "k". The first four characters are the same, so
"flash"comes before"flask". - b. The
sort()function must be applied on the list. The default for thesort()function is to arrange elements in ascending order. - a. The correct syntax for the
reverse()function requires the function to be applied to the list. - a.
"go"is the first element ofboard_games, so"go"would be the last element onceboard_gamesis reversed.
9.3 Common list operations
- a. The
min()function takes a list as a parameter and returns the minimum for the list. - b.
"Austin City Limits"is the first string in dictionary order. - c. The summation of the given floats is
12.4. - c.
list2refers to the same list asmy_list, so any changes made tolist2also reflect onmy_list. 18 is the summation of the values in the list. - a. 3 is the maximum value in the list
my_list. Line 3 changes the value of the first element inlist2. - b.
sum()function is not defined for string values.
9.4 Nested lists
- a. Each row of the matrix is represented as a list in the larger list-of-lists,
matA.matArepresents the complete matrix. - c. 6 is in the 2nd row and 3rd column. Using zero-indexing results in index 1 for the row and 2 for the column.
- c. The first element of the list
matAis a list[7, 4, 5]representing the first row of the matrix. - a. The outer
forloop iterates row by row. The innerforloop iterates through each element in a row. So all the numbers are printed, starting from 7 and ending in -5. - c. The outer
forloop runs for three iterations because there are three elements in the list-of-lists. The innerforloop iterates through each element of each row using the length of each row.
9.5 List comprehensions
- a. The expression tells Python what element to put in the new list.
- c.
i+2results in adding 2 to each element ofa_listto generateb_list. - b. The
forloop starts at 1 and steps by three, so it begins at 1 and ends at 13. Dividing using the//operator results in dropping the part after the decimal. - c. The list comprehension filters for negative numbers in
my_list. - b. The list comprehension selects vowels from the given string.
- a. The
forloop ranges from 0 through 20, stepping by two, but each iterated element is even, so nothing is added tonew_list.


