Loops and Lists#

# remember lists? Let's make a list of breakfast items

breakfast = ["bread", "butter", "bec", "banana"]
# syntax for a loop includes two lines
# the first line identifies the variable name (for each item)
# and the name of the list (or string) that we want to loop over
# the second line instructs the loop what to do to each item

for item in breakfast:
    print(item)
bread
butter
bec
banana
# we can also loop through strings

for letter in "Hello world!":
    print(letter)
H
e
l
l
o
 
w
o
r
l
d
!
# it doesn't matter what we name the variable, as long as we
# are consistent in calling the same variable in the body 
# of the loop

sentence = ["this", "is", "a", "sentence"]

for letter in sentence:
    print(letter)
this
is
a
sentence
# print out the phrase: [item] is my favorite breakfast!

for item in breakfast:
    print(f"{item} is my favorite breakfast!") 
    
bread is my favorite breakfast!
butter is my favorite breakfast!
bec is my favorite breakfast!
banana is my favorite breakfast!

Group challenge:#

# Write some code to print out the square of each of
# these numbers:

# you want the output to print as a string, like so:
# "The square of 2 is 4."
# "The square of 3 is 9."

prime_numbers = [2, 3, 5, 7, 11]

for item in prime_numbers:
    print(f"The sqaure of {item} is {item*item}")

# Hint: Remember that the square of a number is that number 
# times itself. Remember how to use "f-strings".
The sqaure of 2 is 4
The sqaure of 3 is 9
The sqaure of 5 is 25
The sqaure of 7 is 49
The sqaure of 11 is 121

Lists#

movies = ["Avatar", "Nope", "Up", "The Cat in the Hat", "The Incredibles"]
len(movies)
5
len(breakfast)
4
# for reference, these are functions we already have been using:

print()
len()
type()
# this is a method, not a function: breakfast.upper()

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[10], line 4
      1 # for reference, these are functions we already have been using:
      3 print()
----> 4 len()
      5 type()

TypeError: len() takes exactly one argument (0 given)
movies = ["Avatar", "Nope", "Up", "The Cat in the Hat", "The Incredibles"]
movies_length = len(movies)
print(movies_length)
# this does the same thing as the code above, but less readable (imo)
print(len(movies))
# list indexing
movies = ["Avatar", "Nope", "Up", "The Cat in the Hat", "The Incredibles"]

movies[0]
'Avatar'
movies[1]
'Nope'
movies[3]
'The Cat in the Hat'
movies[-1]
'The Incredibles'
movies[-3]
'Up'
movies[-5]
# common error with list indexing is going too far, out of the range of the list.

movies[-6]
movies = ["Avatar", "Nope", "Up", "The Cat in the Hat", "The Incredibles"]

movies[1:3]
['Nope', 'Up']
# in a list slice, the first term is inclusive
# and the second term is exclusive

movies[2:5]
['Up', 'The Cat in the Hat', 'The Incredibles']
# this also works with strings

"hello-world"[2:6]
'llo-'
# the colon takes everything to the left or the right, depending on where the colon is placed
movies[:3]
movies[3:]

Group challenge#

Create a new list of books, with at least 5 books in your list. Make sure the total number of books in the list is an odd number. How do you print out the book in the middle of the list?

What about the three books in the middle? Remember that the first value in a slice is inclusive, and the final value is exclusive.

# first create a list
books = ["harry potter", "jane eyre", "percy jackson", "social credit", "hands-on python"]
# grabs the third item (which is set to #2 on the list)
books[2]
# takes a slice of the middle three items, from 1 to 3 (doesn't include 4). 
books[1:4]

List methods#

# methods are attached to objects

'hello'.upper()
'HELLO'
# functions take data that is passed into them. 

type('hello')
str
# python won't recognize "upper" as a function, only as a method.

upper('hello')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-11-bc4efc3cc90a> in <module>
----> 1 upper('hello')

NameError: name 'upper' is not defined
movies.sort()
movies
['Avatar', 'Nope', 'The Cat in the Hat', 'The Incredibles', 'Up']
movies.append('Ant Man 3')
movies
['Avatar', 'Nope', 'The Cat in the Hat', 'The Incredibles', 'Up', 'Ant Man 3']
movies.sort()
movies
['Ant Man 3', 'Avatar', 'Nope', 'The Cat in the Hat', 'The Incredibles', 'Up']
movies.pop()
'Up'
movies
['Ant Man 3', 'Avatar', 'Nope', 'The Cat in the Hat', 'The Incredibles']
'hello'.pop()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-23-60c039e1ab30> in <module>
----> 1 'hello'.pop()

AttributeError: 'str' object has no attribute 'pop'
tv_shows = ['The last of us', 'The simpsons', 'Modern family', 'Spongebob', 'South Park']
movies.extend(tv_shows)
movies
['Ant Man 3',
 'Avatar',
 'Nope',
 'The Cat in the Hat',
 'The Incredibles',
 'The last of us',
 'The simpsons',
 'Modern family',
 'Spongebob',
 'South Park']
movies.reverse()
movies
['South Park',
 'Spongebob',
 'Modern family',
 'The simpsons',
 'The last of us',
 'The Incredibles',
 'The Cat in the Hat',
 'Nope',
 'Avatar',
 'Ant Man 3']
movies.remove('Spongebob')
movies
['South Park',
 'Modern family',
 'The simpsons',
 'The last of us',
 'The Incredibles',
 'The Cat in the Hat',
 'Nope',
 'Avatar',
 'Ant Man 3']
# professor to work on this later
for item in movies:
    if item in tv_shows:
        movies.remove(item)
tv_shows
['The last of us', 'The simpsons', 'Modern family', 'Spongebob', 'South Park']
movies
['Modern family',
 'The last of us',
 'The Incredibles',
 'The Cat in the Hat',
 'Nope',
 'Avatar',
 'Ant Man 3']