2.11: Lists
- Page ID
- 206278
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \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{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)hypothes.is tag: s20iostpy11ualr
Download Assignment: S20py11
Learning Objectives
Students will be able to:
Content:
- Define a list
- Identify elements of a list
- Explain the purpose of positive and negative indexes in a list.
- Explain how to access individual elements of a list
- Explain how the following functions are used with lists: append(), insert(), remove(), count(), index()
- Explain how to replace an item
Process:
- Write code that prints a list
- Write code that edits a list – add, remove, and insert items
Prior Knowledge
- Python concepts from all previous activities
Further reading
Model 1: Lists
A sequence is an object that stores multiple data items in a contiguous manner. Two types of sequences are strings and lists. Lists are special variables that store multiple values. Each value stored in a list is called an element or item. The following are examples of lists.
Examples of Lists in Python |
|
The location of an element or item in a list is its index number. Index numbers begin at 0. A list having 5 elements will have index values from 0 to 4. The syntax for accessing the elements of a list is the same as for accessing the characters of a string---the bracket operator. The expression inside the brackets specifies the index.
Python Program 11.1 | Output |
|
d <class 'str'> |
Critical Thinking Questions
- Examine the sample lists in the Model.
- How many elements does the list named digits contain?
- What type(class) of data is stored in each list (String, integer, float)?
- digits list:
- compoundClass list:
- elementalData list:
- How would you define a list?
- Why is the output for program 1 “d”, when the third element in the list appears to be “c”?
- What is the value stored in elementalData[2]?
2. Enter the following lines of code into your Thonny editor window.
Python Program 11.2 |
|
- How would you change the code so that the output is “alkyl halides”? compoundClass[5]. What is printed? Why?
- Edit the print statement in the programs so that it prints compoundClass[5]. What is printed? Why?
- Edit the print statement in the programs so that it prints compoundClass[-1]. What is printed?
- Change -1 to -2 in “c.” above. What is printed?
- Explain how the positive and negative indexes locate the desired elements.
- What is printed with the following print statement:
print(compoundClass)
How is the information displayed?
3. Enter and execute the following code:
Python Program 11.3 |
|
- What is the output for the second line of code: print(gradebook)?
- Examine the following code. It contains a FOR loop but does not use the range() function. In previous FOR loops the values resulting from the range() function were stored in x during each iteration of the loop
for x in gradebook:
print(x, end = " ")
print()
- What is being stored in x for each interation of the loop in the following code?
- What is the output of this code?
- What are the similarities and differences between the output for “a.” and “b.”?
- Add each of the following print statements to the code above. What is the output for each statement? Explain the output.
print((gradebook[1]+gradebook[2])/2)
print(gradebook[0]+gradebook[3])
print(gradebook[2]+gradebook[3])
4. Enter and execute the following code:
Python Program 14.4 |
|
a. Explain what the following line of code does:
elements.append(‘nitrogen’)
b. Write a line of code that would add the element oxygen to the list.
5. Enter and execute the following code:
Python Program 11.5 |
|
- Explain what the following line of code does:
elements.insert(1,‘lithium’)
- Write a line of code that would place the element: hydrogen at the beginning of the list.
6. Enter and execute the following code:
Python Program 11.6 |
|
- Explain what the following line of code does:
del elements[2]
- Write a line of code that would delete the last element in the list.
7. Enter and execute the following code:
Python Program 11.7 |
|
- Explain what the following line of code does:
elements.remove('lithium')
- Write a line of code that would delete ‘boron’ from the list.
- Edit the code to determine what happens if the same element appears in the list twice and use the remove()function to remove the word. Does it remove both instances of the word
- Write a line of code that attempts to remove the element: neon. What happens when the code is executed
8. Enter and execute the following code:
Python Program 11.8 |
|
- Explain what the following line of code does:
elements[1]='helium'
- Write the line of code that would replace ‘element7’ with ‘nitrogen’ in the above list.
- Explain what the following line of code does:
elements[-3]= 'new_element'
- Explain what happens when the following line of code is added to the program: elements[12]= 'magnesium'
9. Enter and execute the following code:
Python Program 11.9 |
|
a. What is the output of this program
b. Explain the line of code:
numY = ballot.count(‘y’)
What does the count() function do? How many arguments does it have?
c. Change the following line of code in the program:
numY = ballot.count('Y')
How does this change the output? Is the count function case sensitive?
d. Add the following lines of code to the program:
print(ballot.index('y'))
print(ballot.index('n'))
print(ballot.index('x'))
-- What do the new lines lines print?
-- What does the index() function do?
-- What happens if you replace ‘y’ or ‘n’ with ‘x’? Why does this happen?