in
Operatordel
StatementYou can connect to Gradescope to take weekly graded quiz today during the last 15 minutes of the class.
Once you start the quiz you have 15 minutes to finish it.
I have posted a solution to homework 9 here.
Let's take a look.
I have posted homework 11 here.
It is due this coming Sunday at 11:59 PM.
This is the last homework assignment.
Are there any questions before I begin?
>>> even_numbers = [2, 4, 6, 8, 10] >>> even_numbers [2, 4, 6, 8, 10]
list
Functionlist
is a conversion function just like int
and
str
list
turns objects that are collections of things into a listlist
will work on strings
>>> name = "Glenn" >>> characters = list(name) >>> characters ['G', 'l', 'e', 'n', 'n']
for
Loop with Listsfor
loop will work with a list
>>> numbers = [1, 2, 3, 4, 5] >>>for n in numbers: ... print(n) ... 1 2 3 4 5
for
loop with strings
>>> team = "Red Sox" >>> for char in team: ... print(char) ... R e d S o x >>>
>>> new_list = [] >>> new_list []
len
Functionlen
will return the length of any sequence
>>> even_numbers = [2, 4, 6, 8, 10] >>> len(even_numbers) 5 >>> len("foo")
>>> name = "Glenn" + " " + "Hoffman" >>> name 'Glenn Hoffman'
>>> n1 = [1,2,3] >>> n2 = [4,5,6] >>> n1 + n2 [1, 2, 3, 4, 5, 6]
>>> n1 + name
Traceback (most recent call last):
File "<stdin>", line 1, in Monday
TypeError: can only concatenate list (not "str") to list
>>> n2 - n1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'list' and 'list'
>>> n1 / n2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'list' and 'list'
>>> zeros = [0] * 5 >>> zeros [0, 0, 0, 0, 0] >>> numbers = [1, 2, 3] * 3 >>> numbers [1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> "Go " * 3 'Go Go Go '
for
loop>>> even_numbers = [2, 4, 6, 8, 10] >>> count = 0 >>> for n in even_numbers: ... count += 1 ... if count == 3: ... third_element = n ... >>> third_element 6
>>> even_numbers[0] 2 >>> even_numbers[1] 4 >>> even_numbers[2] 6
IndexError
exception
>>> even_numbers[5]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
numbers = [1, 4, 6, 8]
>>> numbers[0] = 2 >>> numbers [2, 4, 6, 8]
for
loop ...>>> for i in range(len(numbers)): ... numbers[i] += 1 ... >>> numbers [3, 5, 7, 9]
>>> days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
LIST_VARIABLE[FIRST_INDEX:ONE_MORE_THAN_LAST_INDEX]
>>> weekdays = days[1:6] >>> weekdays ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
>>> digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> digits[2:9] [2, 3, 4, 5, 6, 7, 8]
>>> days ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] >>> days[:4] ['Sunday', 'Monday', 'Tuesday', 'Wednesday'] >>> digits [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> digits[:5] [0, 1, 2, 3, 4]
>>> days[4:] ['Thursday', 'Friday', 'Saturday'] >>> digits[5:] [5, 6, 7, 8, 9]
>>> days[:] ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] >>> digits[:] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> daynames = days[:] >>> daynames ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
range
function
LIST_VARIABLE[FIRST_INDEX:ONE_MORE_THAN_LAST_INDEX:STEP_VALUE]
range
function
>>> digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> digits[0:9:2] [0, 2, 4, 6, 8]
>>> digits[0::2] [0, 2, 4, 6, 8]
>>> >>> digits[::2] [0, 2, 4, 6, 8]
in
Operatorin
is an operator that works on objects ...
VALUE in LIST
in
operator returns True
if the
LIST contains VALUE
False
>>> digits [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] >>> 1 in digits True >>> 11 in digits False
>>> l = [1, 2, 3, 3, 3, 4, 5, 6]
in
will tell you that value is in a list
>>> 3 in l True
Method | Description |
---|---|
append(item) | Adds item to the end of the list |
pop(index) | Removes the element at a given index from the list and returns the value. If called with no argument, it returns the last element and deletes it from the list. |
index(item) | Returns the index of the first element whose value is equal to item. A ValueError exception is raised if item is not found in the list. |
insert(index, item) | Inserts item into the list at the specified index. When an item is inserted into a list, the list is expanded in size to accommodate the new item. The item that was previously at the specified index, and all the items after it, are shifted by one position toward the end of the list. |
sort() | Sorts the items in the list so they appear in ascending order (from the lowest value to the highest value) |
reverse() | Reverses the order of the items in the list |
>>> teams = [] >>> teams []
>>> teams.append("Red Sox") >>> teams ['Red Sox']
>>> teams.append("Orioles") >>> teams.append("Blue Jays") >>> teams.append("Rays") >>> teams.append("Yankees")
>>> teams ['Red Sox', 'Orioles', 'Blue Jays', 'Rays', 'Yankees']
>>> numbs = [1, 2, 3, 4, 5] >>> n = numbs.pop(0) >>> n 1 >>> numbs [2, 3, 4, 5]
>>> n = numbs.pop() >>> n 5 >>> numbs [2, 3, 4]
in
operator will tell us if a list contains a value>>> "Red Sox" in teams True
>>> teams.index("Red Sox") 0 >>> teams.index("Blue Jays") 2
>>> teams.index("Mets")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: 'Mets' is not in list
>>> >>> l = [1, 2, 3, 3, 5] >>> l.index(3) 2
>>> l1 = [1, 2, 3, 4, 5] >>> l1.insert(2, 7) >>> l1 [1, 2, 7, 3, 4, 5]
>>> l2 = [9, 1, 0, 2, 8, 6, 7, 4, 5, 3] >>> l2 [9, 1, 0, 2, 8, 6, 7, 4, 5, 3] >>>l2.sort() >>> l2 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l3 = [5, 4, 3, 2, 1] >>> l3.reverse() >>> l3 [1, 2, 3, 4, 5] >>> l3.reverse() >>> l3 [5, 4, 3, 2, 1]
>>> l4 = [2, 5, 9, 1, 8, 6, 3, 7, 4] >>> l4.sort() >>> l4 [1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l4.reverse() >>> l4 [9, 8, 7, 6, 5, 4, 3, 2, 1]
del
Statementdel
statement
del LIST_VARIABLE[INDEX]
del
comes the list variable ...>>> l5 = [0, 1, 2, 3, 4, 5] >>> del l5[2] >>> l5 [0, 1, 3, 4, 5]
min
and max
...
min
returns the smallest value in a list
>>> l6 [1, 4, 5, 6] >>> min(l6) 1
max
returns the largest
max(l6) 6