bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)
['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0])
trek
print(bicycles[0].title())
Trek
print(bicycles[1])
print(bicycles[3])
cannondale specialized
print(bicycles[-1])
specialized
message = 'My first bicycle was a ' + bicycles[0].title() + '.'
print(message)
My first bicycle was a Trek.
# 3-1. Names: Store the names of a few of your friends in a list
# called names. Print each person's name by accessing each element in the list,
# one at a time
friend_names = ['Emily', 'Ray', 'Anthony', 'Kate', 'Margo', 'Stephen', 'Reed']
print(friend_names[0])
print(friend_names[1])
print(friend_names[2])
print(friend_names[3])
print(friend_names[4])
print(friend_names[5])
print(friend_names[6])
Emily Ray Anthony Kate Margo Stephen Reed
# 3-2. Greetings: Start with the list you used in Exercise 3-1,
# but instead of just printing each person's name, print a message to them.
# The text of each message should be the same, but each message should be
# personalized with the person's name.
message = 'Hello ' + friend_names[0].title() + '! You cool.'
print(message)
Hello Emily! You cool.
# 3-3. Your own list: Think of your favorite mode of transportation,
# such as a motorcycle or a car, and make a list that stores several examples.
# Use your list to print a series of statements about these items, such as
# "I would like to own a Honda motorcycle"
very_large_vehicles = ['tank', 'jet', 'yacht', 'blimp']
message = 'I want to be in a ' + very_large_vehicles[0] + ' right now.'
print(message)
message = 'I want to be in a ' + very_large_vehicles[1] + ' right now.'
print(message)
I want to be in a tank right now. I want to be in a jet right now.
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)
['honda', 'yamaha', 'suzuki'] ['ducati', 'yamaha', 'suzuki']
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)
['honda', 'yamaha', 'suzuki'] ['honda', 'yamaha', 'suzuki', 'ducati']
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)
['honda', 'yamaha', 'suzuki']
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati')
print(motorcycles)
['ducati', 'honda', 'yamaha', 'suzuki']
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)
['honda', 'yamaha', 'suzuki'] ['yamaha', 'suzuki']
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)
['honda', 'yamaha', 'suzuki'] ['honda', 'yamaha'] suzuki
motorcycles = ['honda', 'yamaha', 'suzuki']
last_owned_motorcycle = motorcycles.pop()
print('The last motorcycle I owned was a ' + last_owned_motorcycle.title() + '.')
The last motorcycle I owned was a Suzuki.
motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned_motorcycle = motorcycles.pop(0)
print('The first motorcycle I owned was a ' + first_owned_motorcycle.title() + '.')
The first motorcycle I owned was a Honda.
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)
['honda', 'yamaha', 'suzuki', 'ducati'] ['honda', 'yamaha', 'suzuki']
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
too_expensive_motorcycle = 'ducati'
motorcycles.remove(too_expensive_motorcycle)
print(motorcycles)
print('\nA ' + too_expensive_motorcycle.title() + ' is too expensive for me.')
['honda', 'yamaha', 'suzuki', 'ducati'] ['honda', 'yamaha', 'suzuki'] A Ducati is too expensive for me.
# 3-4. Guest list: If you could invite anyone, living or deceased, to dinner, who would you invite?
# Make a list that includes at least three people you'd like to invite to dinner.
# Then use your list to print a message to each person, inviting them to dinner
dream_dinner_guests = ['Carl Sagan', 'Dolly Parton', 'Nicolai Tesla', 'David Bowie']
message = 'Dear ' + dream_dinner_guests[0].title() + ', you are cordially invited to dinner with me at Tenuki at 8pm Saturday.'
print(message)
Dear Carl Sagan, you are cordially invited to dinner with me at Tenuki at 8pm Saturday.
# 3-5. Changing guest list: You just heard that one of your guests can't make
# the dinner, so you need to send out a new set of invitations. You'll have to
# think of someone else to invite.
unavailable_guest = 'David Bowie'
message = 'Unfortunately, ' + unavailable_guest + ' will be unable to attend dinner on Saturday.'
print(message)
replacement_guest = 'Bill Murray'
dream_dinner_guests.remove(unavailable_guest)
dream_dinner_guests.append(replacement_guest)
print(dream_dinner_guests)
Unfortunately, David Bowie will be unable to attend dinner on Saturday. ['Carl Sagan', 'Dolly Parton', 'Nicolai Tesla', 'Bill Murray']
# 3-6. More guests: You just found a bigger dinner table, so now more space is available.
# Think of three more guests to invite to dinner
# Start with your program from 3-4 or 3-5.
# Add a print statement to the end informing people that you found a bigger table.
new_guests = ['Ada Lovelace', 'David Lynch', 'Tony Danza']
message = 'Hey, ' + dream_dinner_guests[0] + ', ' + dream_dinner_guests[1] + ', ' + dream_dinner_guests[2] + ', I found a bigger dinner table'
print(message)
# Use insert() to add one new guest to the beginning of you list
dream_dinner_guests.insert(0, new_guests[0])
# Use insert() to add one new guest to the middle of you list
dream_dinner_guests.insert(2, new_guests[1])
# Use append() to add one new guest to the end of you list
dream_dinner_guests.append(new_guests[2])
print(dream_dinner_guests)
Hey, Carl Sagan, Dolly Parton, Nicolai Tesla, I found a bigger dinner table ['Ada Lovelace', 'Carl Sagan', 'David Lynch', 'Dolly Parton', 'Nicolai Tesla', 'Bill Murray', 'Tony Danza']
# 3-7. Shrinking guest list: You just found out that your new dinner table
# won't arrive in time for the dinner, and you have space for only two guests.
# Start with 3-6. Add a new line that prints a message saying that you can invite only two people for dinner.
message = 'Sorry y\'all, I can only invite two people to dinner'
print(message)
Sorry y'all, I can only invite two people to dinner
# Use pop() to remove guests from you list one at a time until only two names remain in your list.
# Each time you pop a name from your list, print a message to that person letting them
# know you're sorry you can't invite them to dinner.
uninvited_guest = dream_dinner_guests.pop()
print('I am sorry to inform you, ' + uninvited_guest.title() + ', that I will be unable to accomadate you for the Saturday dinner.')
uninvited_guest = dream_dinner_guests.pop()
uninvited_guest = dream_dinner_guests.pop()
uninvited_guest = dream_dinner_guests.pop()
uninvited_guest = dream_dinner_guests.pop()
print(dream_dinner_guests)
I am sorry to inform you, Tony Danza, that I will be unable to accomadate you for the Saturday dinner. ['Ada Lovelace', 'Carl Sagan']
# use del to remove the last two names from your list, so you have an empty list.
# Print your list to make sure you actually have an empty list at the end of your program
del dream_dinner_guests[0]
del dream_dinner_guests[0]
print(dream_dinner_guests)
[]
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
['audi', 'bmw', 'subaru', 'toyota']
cars.sort(reverse=True)
print(cars)
['toyota', 'subaru', 'bmw', 'audi']
cars = ['bmw', 'audi', 'toyota', 'subaru']
print('Here is the original list:')
print(cars)
print('\nHere is the sorted list:')
print(sorted(cars))
print('Here is the original list again:')
print(cars)
Here is the original list: ['bmw', 'audi', 'toyota', 'subaru'] Here is the sorted list: ['audi', 'bmw', 'subaru', 'toyota'] Here is the original list again: ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
cars.reverse()
print(cars)
['bmw', 'audi', 'toyota', 'subaru'] ['subaru', 'toyota', 'audi', 'bmw']
len(cars)
4
# 3-8. Seeing the world: Think of at least five places in the world you'd like to visit.
# Store the locations in a list. Make sure the list is not in alphabetical order.
places_to_visit = ['la', 'thailand', 'berlin', 'japan', 'paris']
# Print your list in its original order. Don't worry about printing the list neatly. just print it as a raw Python list
print(places_to_visit)
# Use `sorted()` to print your list in alphabetical order without modifying the actual list
print(sorted(places_to_visit))
# Show that your list is still in its original order by printing it
print(places_to_visit)
# Use `sorted()` to print you list in reverse alphabetical order without changing the order of the original list
print(sorted(places_to_visit, reverse=True))
# Show that your list is still in its original order by printing it again.
print(places_to_visit)
# Use `reverse()` to change the order of your list. Print the list to show that its order has changed
places_to_visit.reverse()
print(places_to_visit)
# Use `reverse()` to change the order of your list again. Print the list to show it's back to its original order
places_to_visit.reverse()
print(places_to_visit)
# Use `sort()` to change your list so it's stored in alphabetical order.
# Print the list to show that its order has been changed.
places_to_visit.sort()
print(places_to_visit)
# Use `sort()` to change your list so it's stored in reverse alphabetical order.
# Print the list to show that its order has changed.
places_to_visit.sort(reverse=True)
print(places_to_visit)
['la', 'thailand', 'berlin', 'japan', 'paris'] ['berlin', 'japan', 'la', 'paris', 'thailand'] ['la', 'thailand', 'berlin', 'japan', 'paris'] ['thailand', 'paris', 'la', 'japan', 'berlin'] ['la', 'thailand', 'berlin', 'japan', 'paris'] ['paris', 'japan', 'berlin', 'thailand', 'la'] ['la', 'thailand', 'berlin', 'japan', 'paris'] ['berlin', 'japan', 'la', 'paris', 'thailand'] ['thailand', 'paris', 'la', 'japan', 'berlin']
# 3-9. Dinner guests: Working with one of the programs from 3-4 through 3-7, use `len()` to print a message indicating the number of people you are inviting to dinner.
dream_dinner_guests = ['Carl Sagan', 'Dolly Parton', 'Nicolai Tesla', 'David Bowie']
print('I am inviting ' + str(len(dream_dinner_guests)) + ' guests to dinner.')
I am inviting 4 guests to dinner.
# 3-10. Every function: think of something you could store in a list.
# E.g. you could make a list of mountains, rivers, countries, cities, languages or anything else you'd like.
# Write a program that creates a list containing these items and uses each function introduced
# in this chapter at least once.
coffee_shops = ['Hungry Heart', 'Bipartisan', 'The Fillmore', 'Heart']
print('Original list:')
print(coffee_shops)
print('Sorted (but unchanged) list:')
print(sorted(coffee_shops))
print('Reverse-sorted (but unchanged) list:')
print(sorted(coffee_shops, reverse=True))
print('List sorted in-place:')
print('\tBefore:')
print(coffee_shops)
print('\tAfter:')
coffee_shops.sort()
print(coffee_shops)
print('Reversed (in-place):')
coffee_shops.reverse()
print(coffee_shops)
print('... which should be the same as reversing back and then reverse-sorting in-place:')
coffee_shops.reverse()
coffee_shops.sort(reverse=True)
print(coffee_shops)
Original list: ['Hungry Heart', 'Bipartisan', 'The Fillmore', 'Heart'] Sorted (but unchanged) list: ['Bipartisan', 'Heart', 'Hungry Heart', 'The Fillmore'] Reverse-sorted (but unchanged) list: ['The Fillmore', 'Hungry Heart', 'Heart', 'Bipartisan'] List sorted in-place: Before: ['Hungry Heart', 'Bipartisan', 'The Fillmore', 'Heart'] After: ['Bipartisan', 'Heart', 'Hungry Heart', 'The Fillmore'] Reversed (in-place): ['The Fillmore', 'Hungry Heart', 'Heart', 'Bipartisan'] ... which should be the same as reversing back and then reverse-sorting in-place: ['The Fillmore', 'Hungry Heart', 'Heart', 'Bipartisan']
# generate an index-error
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles[3])
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-67-33492f266c41> in <module>() 1 # generate an index-error 2 motorcycles = ['honda', 'yamaha', 'suzuki'] ----> 3 print(motorcycles[3]) IndexError: list index out of range
# What we meant to do:
print(motorcycles[-1])
suzuki
# generate another index-error
motorcycles = []
print(motorcycles[-1])
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-69-0004907884ad> in <module>() 1 # generate another index-error 2 motorcycles = [] ----> 3 print(motorcycles[-1]) IndexError: list index out of range
# 3-11. Intentional error: Make an index error happen.
coffee_shops = ['Hungry Heart', 'Bipartisan', 'The Fillmore', 'Heart']
print(coffee_shops[4])
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-70-b53475062f55> in <module>() 1 # 3-11. Intentional error: Make an index error happen. 2 coffee_shops = ['Hungry Heart', 'Bipartisan', 'The Fillmore', 'Heart'] ----> 3 print(coffee_shops[4]) IndexError: list index out of range
# Correct the error.
print(coffee_shops[3])
print(coffee_shops[-1])
Heart Heart