0% found this document useful (0 votes)
15 views

10 Lists

Uploaded by

leonardo333555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

10 Lists

Uploaded by

leonardo333555
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 117

Lists

CS106AP Lecture 10
Roadmap B asics
in g
Programm The C
onsol Ima
e ges
Day 1!

Graphics Data structures


Midterm

Object-Oriented
Everyday Python
Programming

Life after CS106AP!


Roadmap B asics
in g
Programm The C
onsol Ima
e ges
Day 1!

Graphics Data structures


Midterm
Dic
tion Dicti Parsi FilesLists
arie ona ng:
Object-Oriented s ri St
Everyday Python 2.0 es 1.0 rings
Programming

Life after CS106AP!


How can we store and organize
Today’s data in our code?

questions
1. Review

Today’s 2. Introduction to Lists

topics 3. Advanced Lists

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

Gets the pixel at x,y


flip_horizontal()
[demo]
Summary
● Use nested for range() loops to manipulate pixels when we care about
x,y
○ Use image.get_pixel(x, y) to get the pixel at the specific
coordinates
Summary
● Use nested for range() loops to manipulate pixels when we care about
x,y

● Common pattern: Swapping two variables


○ Use a temporary (“temp”) variable to store the old value

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

● Common pattern: Swapping two variables

● SimpleImage.blank(new_width, new_height) allows us to


create a new, empty image of a specific size
○ Then we can loop over its pixels to set their RGB
Advanced For Range Loops
>>> for i in range(4, 0, -1):
Advanced For Range Loops
>>> for i in range(4, 0, -1):

(start_index, end_index, step)


Advanced For Range Loops
>>> for i in range(4, 0, -1):

... 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]

[‘a’, ‘b’, ‘b’, ‘d’] List


A data type for storing
values in a linear collection.
What is a list? Definition
[1, 2, 3, 4, 5]

[‘a’, ‘b’, ‘b’, ‘d’] List


A data type for storing
[True]
values in a linear collection.
What is a list? Definition
[1, 2, 3, 4, 5]

[‘a’, ‘b’, ‘b’, ‘d’] List


A data type for storing
[True]
values in a linear collection.

Lists can have 1


element!
What is a list? Definition
[1, 2, 3, 4, 5]

[‘a’, ‘b’, ‘b’, ‘d’] List


A data type for storing
[True]
values in a linear collection.
[1, ‘a’, 2, ‘b’, True]
What is a list? Definition
[1, 2, 3, 4, 5]

[‘a’, ‘b’, ‘b’, ‘d’] List


A data type for storing
[True]
values in a linear collection.
[1, ‘a’, 2, ‘b’, True]

Lists can contain different types!


What is a list? Definition
[1, 2, 3, 4, 5]

[‘a’, ‘b’, ‘b’, ‘d’] List


A data type for storing
[True]
values in a linear collection.
[1, ‘a’, 2, ‘b’, True]

[]
What is a list? Definition
[1, 2, 3, 4, 5]

[‘a’, ‘b’, ‘b’, ‘d’] List


A data type for storing
[True]
values in a linear collection.
[1, ‘a’, 2, ‘b’, True]

[]

compare to the empty string (‘’)!


How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]

>>> 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]

‘a’ Indexing lists is similar to indexing


>>> letters[3] strings, except with elements instead
‘d’ of characters!
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]

>>> 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:]

[‘b’, ‘c’, ‘d’]


How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]

>>> letters[:2]

[‘a’, ‘b’] Slicing is similar to strings, too!


>>> letters[1:]

[‘b’, ‘c’, ‘d’]


How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]
How to inspect a list
>>> letters = [‘a’, ‘b’, ‘c’, ‘d’]

>>> 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)

4 len() works for lists as well!


Printing lists
>>> fruits = [‘apple’, ‘banana’, ‘mango’]
Printing lists
>>> fruits = [‘apple’, ‘banana’, ‘mango’]

>>> print(fruits)
Printing lists
>>> fruits = [‘apple’, ‘banana’, ‘mango’]

>>> print(fruits)

[‘apple’, ‘banana’, ‘mango’]


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 += [6, 7]


How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]

>>> lst += [6, 7]

>>> lst
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]

>>> lst += [6, 7]

>>> lst

[1, 2, 3, 4, 5, 6, 7]
How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]

>>> lst += [6, 7]

>>> 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 += [6, 7]

>>> lst

[1, 2, 3, 4, 5, 6, 7]

>>> lst += T8ypeError


How can I change what’s in a list?
>>> lst = [1, 2, 3, 4, 5]

>>> lst += [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)

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
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 = lst.pop()


How can I remove something from a list?
>>> lst = [1, 2, 3, 4, 5]

>>> last_elem = lst.pop()

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 = lst.pop()

>>> 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 = lst.pop()

>>> 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 = lst.pop()

>>> 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 = lst.pop()

>>> 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’]

>>> ‘mango’ in fruits


How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]

>>> ‘mango’ in fruits returns True if element in list,


else False!
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]

>>> ‘mango’ in fruits returns True if element in list,


True else False!
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]

>>> ‘mango’ in fruits returns True if element in list,


True else False!
>>> ‘broccoli’ in fruits
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]

>>> ‘mango’ in fruits returns True if element in list,


True else False!
>>> ‘broccoli’ in fruits

False
How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]

>>> ‘mango’ in fruits returns True if element in list,


True else False!
>>> ‘broccoli’ in fruits

False

>>> ‘broccoli’ not in fruits


How can I check if something’s in a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’, ‘kiwi’]

>>> ‘mango’ in fruits returns True if element in list,


True else False!
>>> ‘broccoli’ in fruits

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’]

>>> ‘mango’ in fruits returns True if element in list,


True else False!
>>> ‘broccoli’ in fruits

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’]

>>> for fruit in fruits:


How can I loop over a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’]

>>> for fruit in fruits:


How can I loop over a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’]

>>> for fruit in fruits:


like a foreach loop over a string!
How can I loop over a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’]

>>> for fruit in fruits:


like a foreach loop over a string!
... print(fruit)
How can I loop over a list?
>>> fruits = [‘apple’, ‘banana’, ‘mango’]

>>> for fruit in fruits:


like a foreach loop over a string!
... print(fruit)

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’

>>> words = s.split()


Making a list from a string
>>> s = ‘I am comprised of words’

>>> words = s.split()


splits string where there are spaces
Making a list from a string
>>> s = ‘I am comprised of words’

>>> words = s.split()


splits string where there are spaces
>>> words
Making a list from a string
>>> s = ‘I am comprised of words’

>>> words = s.split()


splits string where there are spaces
>>> words

[‘I’, ‘am’, ‘comprised’, ‘of’, ‘words’]


Making a list from a string
>>> s = ‘I am comprised of words’

>>> words = s.split()


splits string where there are spaces
>>> words

[‘I’, ‘am’, ‘comprised’, ‘of’, ‘words’]

>>> s
Making a list from a string
>>> s = ‘I am comprised of words’

>>> words = s.split()


splits string where there are spaces
>>> words

[‘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’

>>> notes = s.split(‘,’)


Making a list from a string
>>> s = ‘do,re,mi,fa,sol,la,ti’

>>> notes = s.split(‘,’)


pass in a “delimiter”, which tells us
where to split the string
Making a list from a string
>>> s = ‘do,re,mi,fa,sol,la,ti’

>>> notes = s.split(‘,’)


pass in a “delimiter”, which tells us
>>> notes where to split the string
Making a list from a string
>>> s = ‘do,re,mi,fa,sol,la,ti’

>>> notes = s.split(‘,’)


pass in a “delimiter”, which tells us
>>> notes where to split the string
[‘do’, ‘re’, ‘mi’, ‘fa’, ‘sol’, ‘la’, ‘ti’]
Think/Pair/Share:
Write words_starting_with()
Inputs:
sentence (string)
char (string)
Returns:
list of words starting with character
Making a list from a string
>>> words_starting_with('I love ice cream', 'i')
Think/Pair/Share:
['I', 'ice']
Write a function
# syntax reminder: words_starting_with().

lst = s.split() Inputs:


sentence (string)
char (string)
Returns:
list of words starting with
character
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::-1]
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::-1]

[‘e’, ‘d’, ‘c’, ‘b’, ‘a’]


Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::-1]

[‘e’, ‘d’, ‘c’, ‘b’, ‘a’]


Think/Pair/Share:

What just happened? What is


going on in the [::-1]?
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::-1] (start_index, end_index, step)


[‘e’, ‘d’, ‘c’, ‘b’, ‘a’]
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::-1] (start_index, end_index, step)


[‘e’, ‘d’, ‘c’, ‘b’, ‘a’]

for i in range(4, 0, -1):


Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::-1] (start_index, end_index, step)


[‘e’, ‘d’, ‘c’, ‘b’, ‘a’]

>>> lst
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::-1] (start_index, end_index, step)


[‘e’, ‘d’, ‘c’, ‘b’, ‘a’]

>>> lst

[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]


Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::2]
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::2]

Think/Pair/Share:

What will this return?


Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::2]

[‘a’, ‘c’, ‘e’]


Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::2] (start_index, end_index, step)


[‘a’, ‘c’, ‘e’]
Advanced Slicing
>>> lst = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

>>> lst[::2] (start_index, end_index, step)


[‘a’, ‘c’, ‘e’]

for i in range(0, 6, 2):


What’s next?
Roadmap B asics
in g
Programm The C
onsol Ima
e ges
Day 1!

Graphics Data structures


Midterm
Dic
tion Dicti Pars FilesLists
arie ona ing:
Object-Oriented s ri S
Everyday Python 2.0 es 1.0 trings
Programming

Life after CS106AP!

You might also like