Lists & Tuples
A Python list is a sequence (ordered collection) of Python objects. Because it is a sequence like a string it shares characteristics such as indexing & slicing, iteration (for) and membership (in)
as well as functions such as len().
One importance difference is that lists are mutable (strings are immutable).
Tuples are immutable lists.
Chapter 7: Lists
A list is a sequence of Python objects delimited by square brackets and
separated by commas. Any mixture of objects is allowed.
- Lists (Video I,
- Section 7.2.1: 51290
- Section 7.2.2: 51613
- Section 7.2.3: 51612, 51213
- Section 7.2.5: 51215,
51210 (hint: use functions "sum()" and "len()", not the "k" and "total" MPL suggests),
51219 (hint: use the function "max()")
- Section 7.3.1: 51202, 51208
- Section 7.3.2: 51610,
51295 (challenge: this one can be done in one line using List Comprehensions of Section 7.10 p.337)
- Section MPL Extra: 51283 (challenge: can be done in one line),
- Tuples (Video)
Tuples are immutable lists – handy if you want a list that cannot be changed. Tuples are delimited by parentheses.
- Section 7.7: 51700, 51706, 51707, 51708
- Examples using lists
- Perfect Numbers (Video I , Video II)
In number theory, a perfect number is a positive integer that is equal to the sum of its positive divisors (excluding the number itself). Here we find the perfect numbers that are less than some value.
What do these words have in common: pig, table, cab, real, yet, and ride?
Once you figure that out, what other English words have this characteristic? Let's write a program.
This link provides detail about naming and assignment, especially with respect to immutables (such as lists) and function parameters. There isn't anything here that isn't in the book, but the information is organized differently. Understanding the details right will help you program (as well as help you on exams).
How to build a list:
a_list =  # start with an empty list
for item in collection:
a_list.append(item) # append item to list
No Chapter exercises on Mimir this week.
Lab06 (do pre-lab first on D2L)