list
Functionfor
Loop with Listslen
Functionin
Operatordel
StatementYou should read Chapter 8, More About Strings, from our textbook, Starting Out with Python, before next week's class.
I have posted homework 11 here.
This is the last homework assignment.
The answers to Quiz 8 are posted here.
Let's take a look.
>>> numbers = [5, 8, 2, 9, 7, 6, 3] >>> numbers [5, 8, 2, 9, 7, 6, 3]
>>> courses = ["IT 244", "IT 341", "IT 116"] >>> courses ['IT 244', 'IT 341', 'IT 116']
float
values
>>> expenses = [37.55, 21.25, 32.00] >>> expenses [37.55, 21.25, 32.0]
>>> results = [True, False, True] >>>results [True, False, True]
>>> python_class = ["IT 116", 32, "McCormack 2-621"] >>> python_class
>>> lists = [ numbers, results, expenses] >>> lists [[5, 8, 2, 9, 7, 6, 3], [True, False, True], [37.55, 21.25, 32.0]]
list
Functionlist
list
function will take any sequence object ...list
function ...>>> name = "Glenn" >>> characters = list(name) >>> characters ['G', 'l', 'e', 'n', 'n']
range
function ...>>> numbers = range(1, 6) >>> numbers range(1, 6) >>> type(numbers) <class 'range'>
>>> zeros = [0] * 5 >>> zeros [0, 0, 0, 0, 0] >>> numbers = [1, 2, 3] * 3 >>> numbers [1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> new_list = [] >>> new_list []
for
Loop with Listsfor
loop ...>>> numbers = [1, 2, 3, 4, 5] >>>for n in numbers: ... print(n) ... 1 2 3 4 5
for
loop let's us iterate through the elements of a list ...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
while
loop
>>> while i < 5: ... print(even_numbers[i]) ... i += 1 ... 2 4 6 8 10
len
Functionlen
...>>> even_numbers = [2, 4, 6, 8, 10] >>> len(even_numbers) 5 >>> len("foo")
len
function to iterate through a list ...for
loop ...range
function
for i in range(len(even_numbers)): ... print(even_numbers[i]) ... 2 4 6 8 10
numbers = [5, 8, 2, 9, 7, 6, 3]
>>> numbers[0] = 4 >>> numbers [4, 8, 2, 9, 7, 6, 3]
for
loop ...>>> for i in range(len(numbers)): ... numbers[i] += 1 ... >>> numbers [5, 9, 3, 10, 8, 7, 4]
>>> numbers_1 = [1, 2, 3, 4, 5] >>> numbers_2 = [6, 7, 8, 9, 10] >>> numbers_1 + numbers_2 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
>>> days[1] 'Monday'
for
loop
for index in range(1, len(days) - 1): ... print(days[index]) ... Monday Tuesday Wednesday Thursday Friday
LIST_VARIABLE[FIRST_INDEX:ONE_MORE_THAN_LAST_INDEX]
>>> weekdays = days[1:6] >>> weekdays ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
>>> [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
digits[1:8] [2, 3, 4, 5, 6, 7, 8]
>>> days ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] >>> days[:4] ['Sunday', 'Monday', 'Tuesday', 'Wednesday'] >>> digits [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] >>> digits[:5] [1, 2, 3, 4, 5]
>>> days[4:] ['Thursday', 'Friday', 'Saturday'] >>> digits[5:] [6, 7, 8, 9, 0]
>>> days[:] ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] >>> digits[:] [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> daynames ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
range
function allows you to give it a 3rd argument ...LIST_VARIABLE[FIRST_INDEX:ONE_MORE_THAN_LAST_INDEX:STEP_VALUE]
>>> digits[1:10:2] [2, 4, 6, 8, 0]
digits[1::2] [2, 4, 6, 8, 0]
in
Operator>>> def item_in_list(value, list): ... for index in range(len(list)): ... if value == list[index]: ... return True ... return False ... >>> item_in_list(1, digits) True >>> item_in_list(11, digits) False
for
loop ...True
...return
statement always causes a function ...False
in
operator does the same thingVALUE in LIST
in
operator returns True
...False
>>> 1 in digits True >>> 11 in digits False
>>> l1 = [3, 4, 5, 6, 1, 3, 5] >>> l1 [3, 4, 5, 6, 1, 3, 5]
in
operator on a list ...>>> 3 in l1 True
Method | Description |
---|---|
append(item) | Adds item to the end of 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) |
remove(item) | Removes the first occurrence of item from the list. A ValueError exception is raised if item is not found in the list. |
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']
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
>>> l1 [3, 4, 5, 6, 1, 3, 5] >>> l1.index(3) 0
>>> l2 = [2, 4, 6, 8, 10] >>> l2.index(2) 0 >>> l2.index(10) 4 >>> l2.insert(0,1) >>> l2 [1, 2, 4, 6, 8, 10] >>> l2.index(2) 1 >>> l2.index(10) 5
>>> l1 = [1, 2, 3, 4, 5] >>> l1.remove(3) >>> l1 [1, 2, 4, 5]
>>> l1 [3, 5, 6, 1, 3, 5] >>> l1.remove(3) >>> l1 [5, 6, 1, 3, 5]
>>> l3 = [9, 1, 0, 2, 8, 6, 7, 4, 5, 3] >>> l3 [9, 1, 0, 2, 8, 6, 7, 4, 5, 3] >>>l3.sort() >>> l3 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> l3 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> l3.reverse() >>> l3 [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
del
Statementdel
statementdel LIST_VARIABLE[INDEX]
del
comes the list variable ...>>> l5 = [1, 2, 3, 4, 5, 6] >>> del l5[2] >>> l5 [1, 2, 4, 5, 6]
del
statement works differently ...>>> l5.remove(2) >>> l5 [1, 4, 5, 6]
del
removes by index ...min
returns the smallest value in a list
>>> l5 [1, 4, 5, 6] >>> min(l5) 1
max
returns the largest value
max(l5) 6