10 Lists
10 Lists
CS106AP Lecture 10
Roadmap B asics
in g
Programm The C
onsol Ima
e ges
Day 1!
Object-Oriented
Everyday Python
Programming
questions
1. Review
4. What’s next?
Review
Image coordinate system
y (height) x (width)
Nested for loops
image = SimpleImage(filename)
for y in range(image.height):
for x in range(image.width):
pixel = image.get_pixel(x, y)
Iterate over # Do something with pixel
the rows Iterate over the columns
x1 = 3
x2 = 4
temp = x1
x1 = x2
x2 = temp
Summary
● Use nested for range() loops to manipulate pixels when we care about
x,y
... print(i)
(start_index, end_index, step)
Advanced For Range Loops
>>> for i in range(4, 0, -1):
... print(i)
(start_index, end_index, step)
4
1
Advanced For Range Loops
>>> for i in range(0, 8, 2):
Advanced For Range Loops
>>> for i in range(0, 8, 2):
... print(i)
Advanced For Range Loops
>>> for i in range(0, 8, 2):
... print(i)
6
How can we store and
organize data in code?
Lists!
What is a list? Definition
List
A data type for storing
values in a linear collection.
What is a list? Definition
[1, 2, 3, 4, 5]
List
A data type for storing
values in a linear collection.
What is a list? Definition
[1, 2, 3, 4, 5]
List
A data type for storing
Use [ ] to values in a linear collection.
write a list
in code!
What is a list? Definition
[1, 2, 3, 4, 5]
List
A data type for storing
Lists contain values in a linear collection.
elements!
(separate with
commas)
What is a list? Definition
[1, 2, 3, 4, 5]
[]
What is a list? Definition
[1, 2, 3, 4, 5]
[]
>>> letters[0]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> letters[0]
‘a’
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> letters[0]
‘a’
>>> letters[3]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> letters[0]
‘a’
>>> letters[3]
‘d’
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> letters[0]
>>> letters[:2]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> letters[:2]
[‘a’, ‘b’]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> letters[:2]
[‘a’, ‘b’]
>>> letters[1:]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> letters[:2]
[‘a’, ‘b’]
>>> letters[1:]
>>> letters[:2]
>>> len(letters)
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> len(letters)
4
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
>>> len(letters)
>>> print(fruits)
Printing lists
>>> fruits = [‘apple’, ‘banana’, ‘mango’]
>>> print(fruits)
>>> lst
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
>>> lst
[1, 2, 3, 4, 5, 6, 7]
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
>>> lst
[1, 2, 3, 4, 5, 6, 7]
>>> lst += 8
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
>>> lst
[1, 2, 3, 4, 5, 6, 7]
>>> lst
[1, 2, 3, 4, 5, 6, 7]
>>> lst += 8
you can only use += for
concatenating other lists!
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
>>> lst.append(6)
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
>>> lst.append(6)
>>> lst.append(6)
>>> lst
append adds a single element to
the end of a list!
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]
>>> lst.append(6)
>>> lst
append adds a single element to
[1, 2, 3, 4, 5, 6]
the end of a list!
How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]
How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]
>>> last_elem
pop() removes the last element in
a list and returns it
How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]
>>> last_elem
pop() removes the last element in
5
a list and returns it
How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]
>>> last_elem
pop() removes the last element in
5
a list and returns it
>>> lst
How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]
>>> last_elem
pop() removes the last element in
5
a list and returns it
>>> lst
[1, 2, 3, 4]
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]
False
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]
False
False
you can use not with in
>>> ‘broccoli’ not in fruits
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]
False
you can use not with in
>>> ‘broccoli’ not in fruits
True
How can I loop over a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’]
How can I loop over a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’]
apple
banana
mango
Think/Pair/Share:
Write a function find_min() that
returns the minimum float in a list.
How can I loop over a list?
>>> find_min([2.3, 7.1, 10.6])
2.3
Think/Pair/Share:
# syntax reminder:
Write a function
for elem in lst: find_min()that returns
the minimum float in a list.
# do something
Making a list from a string
>>> s = ‘I am comprised of words’
Making a list from a string
>>> s = ‘I am comprised of words’
>>> s
Making a list from a string
>>> s = ‘I am comprised of words’
>>> s
‘I am comprised of words’
Making a list from a string
>>> s = ‘do,re,mi,fa,sol,la,ti’
Making a list from a string
>>> s = ‘do,re,mi,fa,sol,la,ti’
>>> lst[::-1]
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
>>> lst[::-1]
>>> lst[::-1]
>>> lst
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
>>> lst
>>> lst[::2]
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
>>> lst[::2]
Think/Pair/Share:
>>> lst[::2]