Chapter Questions Flashcards
Find a quote from a famous person you admire. Print the quote and the name of its author. Your output should look something like the following, including the quotation marks:
Albert Einstein once said, “A person who never made a mistake never tried anything new.”
print(‘Albert Einstein once said, “A person who never made a mistake’)
print(‘never tried anything new.”’)
Use a variable to represent a person’s name, and include some whitespace characters at the beginning and end of the name. Make sure you use each character combination, “\t” and “\n”, at least once.
Print the name once, so the whitespace around the name is displayed. Then print the name using each of the three stripping functions, lstrip(), rstrip(), and strip().
name = “\tEric Matthes\n”
print(“Unmodified:”)
print(name)
print(“\nUsing lstrip():”)
print(name.lstrip())
print(“\nUsing rstrip():”)
print(name.rstrip())
print(“\nUsing strip():”)
print(name.strip())
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.
names = [‘ron’, ‘tyler’, ‘dani’]
print(names[0])
print(names[1])
print(names[2])
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.
names = [‘ron’, ‘tyler’, ‘dani’]
msg = f”Hello, {names[0].title()}!”
print(msg)
msg = f”Hello, {names[1].title()}!”
print(msg)
msg = f”Hello, {names[2].title()}!”
print(msg)
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.
guests = [‘guido van rossum’, ‘jack turner’, ‘lynn hill’]
name = guests[0].title()
print(f”{name}, please come to dinner.”)
name = guests[1].title()
print(f”{name}, please come to dinner.”)
name = guests[2].title()
print(f”{name}, please come to dinner.”)
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.
Start with your program from Exercise 3-4. Add a print statement at the end of your program stating the name of the guest who can’t make it.
Modify your list, replacing the name of the guest who can’t make it with the name of the new person you are inviting.
Print a second set of invitation messages, one for each person who is still in your list.
# Invite some people to dinner. guests = ['guido van rossum', 'jack turner', 'lynn hill']
name = guests[0].title()
print(f”{name}, please come to dinner.”)
name = guests[1].title()
print(f”{name}, please come to dinner.”)
name = guests[2].title()
print(f”{name}, please come to dinner.”)
name = guests[1].title()
print(f”\nSorry, {name} can’t make it to dinner.”)
# Jack can't make it! Let's invite Gary instead. del(guests[1]) guests.insert(1, 'gary snyder')
# Print the invitations again. name = guests[0].title() print(f"\n{name}, please come to dinner.")
name = guests[1].title()
print(f”{name}, please come to dinner.”)
name = guests[2].title()
print(f”{name}, please come to dinner.”)
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 Exercise 3-4 or Exercise 3-5. Add a print statement to the end of your program informing people that you found a bigger dinner table. Use insert() to add one new guest to the beginning of your list. Use insert() to add one new guest to the middle of your list. Use append() to add one new guest to the end of your list. Print a new set of invitation messages, one for each person in your list.
# Invite some people to dinner. guests = ['guido van rossum', 'jack turner', 'lynn hill']
name = guests[0].title()
print(f”{name}, please come to dinner.”)
name = guests[1].title()
print(f”{name}, please come to dinner.”)
name = guests[2].title()
print(f”{name}, please come to dinner.”)
name = guests[1].title()
print(f”\nSorry, {name} can’t make it to dinner.”)
# Jack can't make it! Let's invite Gary instead. del(guests[1]) guests.insert(1, 'gary snyder')
# Print the invitations again. name = guests[0].title() print(f"\n{name}, please come to dinner.")
name = guests[1].title()
print(f”{name}, please come to dinner.”)
name = guests[2].title()
print(f”{name}, please come to dinner.”)
# We got a bigger table, so let's add some more people to the list. print("\nWe got a bigger table!") guests.insert(0, 'frida kahlo') guests.insert(2, 'reinhold messner') guests.append('elizabeth peratrovich')
name = guests[0].title()
print(f”{name}, please come to dinner.”)
name = guests[1].title()
print(f”{name}, please come to dinner.”)
name = guests[2].title()
print(f”{name}, please come to dinner.”)
name = guests[3].title()
print(f”{name}, please come to dinner.”)
name = guests[4].title()
print(f”{name}, please come to dinner.”)
name = guests[5].title()
print(f”{name}, please come to dinner.”)
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 your program from Exercise 3-6. Add a new line that prints a message saying that you can invite only two people for dinner. Use pop() to remove guests from your 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. Print a message to each of the two people still on your list, letting them know they’re still invited. 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.
# Invite some people to dinner. guests = ['guido van rossum', 'jack turner', 'lynn hill']
name = guests[0].title()
print(f”{name}, please come to dinner.”)
name = guests[1].title()
print(f”{name}, please come to dinner.”)
name = guests[2].title()
print(f”{name}, please come to dinner.”)
name = guests[1].title()
print(f”\nSorry, {name} can’t make it to dinner.”)
# Jack can't make it! Let's invite Gary instead. del(guests[1]) guests.insert(1, 'gary snyder')
# Print the invitations again. name = guests[0].title() print(f"\n{name}, please come to dinner.")
name = guests[1].title()
print(f”{name}, please come to dinner.”)
name = guests[2].title()
print(f”{name}, please come to dinner.”)
# We got a bigger table, so let's add some more people to the list. print("\nWe got a bigger table!") guests.insert(0, 'frida kahlo') guests.insert(2, 'reinhold messner') guests.append('elizabeth peratrovich')
name = guests[0].title()
print(f”{name}, please come to dinner.”)
name = guests[1].title()
print(f”{name}, please come to dinner.”)
name = guests[2].title()
print(f”{name}, please come to dinner.”)
name = guests[3].title()
print(f”{name}, please come to dinner.”)
name = guests[4].title()
print(f”{name}, please come to dinner.”)
name = guests[5].title()
print(f”{name}, please come to dinner.”)
# Oh no, the table won't arrive on time! print("\nSorry, we can only invite two people to dinner.")
name = guests.pop()
print(f”Sorry, {name.title()} there’s no room at the table.”)
name = guests.pop()
print(f”Sorry, {name.title()} there’s no room at the table.”)
name = guests.pop()
print(f”Sorry, {name.title()} there’s no room at the table.”)
name = guests.pop()
print(f”Sorry, {name.title()} there’s no room at the table.”)
# There should be two people left. Let's invite them. name = guests[0].title() print(f"{name}, please come to dinner.")
name = guests[1].title()
print(f”{name}, please come to dinner.”)
# Empty out the list. del(guests[0]) del(guests[0])
# Prove the list is empty. print(guests)
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. Print your list in its original order. Don’t worry about printing the list neatly, just print it as a raw Python list. Use sorted() to print your list in alphabetical order without modifying the actual list. Show that your list is still in its original order by printing it. Use sorted() to print your list in reverse alphabetical order without changing the order of the original list. Show that your list is still in its original order by printing it again. Use reverse() to change the order of your list. Print the list to show that its order has changed. Use reverse() to change the order of your list again. Print the list to show it’s back to its original order. Use sort() to change your list so it’s stored in alphabetical order. Print the list to show that its order has been changed. Use sort() to change your list so it’s stored in reverse alphabetical order. Print the list to show that its order has changed.
locations = [‘himalaya’, ‘andes’, ‘tierra del fuego’, ‘labrador’, ‘guam’]
print(“Original order:”)
print(locations)
print(“\nAlphabetical:”)
print(sorted(locations))
print(“\nOriginal order:”)
print(locations)
print(“\nReverse alphabetical:”)
print(sorted(locations, reverse=True))
print(“\nOriginal order:”)
print(locations)
print(“\nReversed:”)
locations.reverse()
print(locations)
print(“\nOriginal order:”)
locations.reverse()
print(locations)
print(“\nAlphabetical”)
locations.sort()
print(locations)
print(“\nReverse alphabetical”)
locations.sort(reverse=True)
print(locations)
Think of at least three kinds of your favorite pizza. Store these pizza names in a list, and then use a for loop to print the name of each pizza.
Modify your for loop to print a sentence using the name of the pizza instead of printing just the name of the pizza. For each pizza you should have one line of output containing a simple statement like I like pepperoni pizza.
Add a line at the end of your program, outside the for loop, that states how much you like pizza. The output should consist of three or more lines about the kinds of pizza you like and then an additional sentence, such as I really love pizza!
favorite_pizzas = [‘pepperoni’, ‘hawaiian’, ‘veggie’]
# Print the names of all the pizzas. for pizza in favorite_pizzas: print(pizza)
print(“\n”)
# Print a sentence about each pizza. for pizza in favorite_pizzas: print(f"I really love {pizza} pizza!")
print(“\nI really love pizza!”)
A number raised to the third power is called a cube. For example, the cube of 2 is written as 2**3 in Python. Make a list of the first 10 cubes (that is, the cube of each integer from 1 through 10), and use a for loop to print out the value of each cube.
cubes = []
for number in range(1, 11):
cube = number**3
cubes.append(cube)
for cube in cubes:
print(cube)
use the third argument of the range function to make a list of the odd numbers from 1 to 20. use a for loop to print each number
odd_numbers = list(range(1, 30, 2))
for value in odd_numbers:
print(value)
Start with your program from Exercise 4-1 (page 60). Make a copy of the list of pizzas, and call it friend_pizzas. Then, do the following:
Add a new pizza to the original list.
Add a different pizza to the list friend_pizzas.
Prove that you have two separate lists. Print the message, My favorite pizzas are:, and then use a for loop to print the first list. Print the message, My friend’s favorite pizzas are:, and then use a for loop to print the second list. Make sure each new pizza is stored in the appropriate list.
favorite_pizzas = ['pepperoni', 'hawaiian', 'veggie'] friend_pizzas = favorite_pizzas[:]
favorite_pizzas.append(“meat lover’s”)
friend_pizzas.append(‘pesto’)
print(“My favorite pizzas are:”)
for pizza in favorite_pizzas:
print(f”- {pizza}”)
print(“\nMy friend’s favorite pizzas are:”)
for pizza in friend_pizzas:
print(f”- {pizza}”)
A buffet-style restaurant offers only five basic foods. Think of five simple foods, and store them in a tuple.
Use a for loop to print each food the restaurant offers.
Try to modify one of the items, and make sure that Python rejects the change.
The restaurant changes its menu, replacing two of the items with different foods. Add a block of code that rewrites the tuple, and then use a for loop to print each of the items on the revised menu.
menu_items = (
‘rockfish sandwich’, ‘halibut nuggets’, ‘smoked salmon chowder’,
‘salmon burger’, ‘crab cakes’,
)
print(“You can choose from the following menu items:”)
for item in menu_items:
print(f”- {item}”)
menu_items = (
‘rockfish sandwich’, ‘halibut nuggets’, ‘smoked salmon chowder’,
‘black cod tips’, ‘king crab legs’,
)
print(“\nOur menu has been updated.”)
print(“You can now choose from the following items:”)
for item in menu_items:
print(f”- {item}”)
Make a list of the numbers from one to one million, and then use min() and max() to make sure your list actually starts at one and ends at one million. Also, use the sum() function to see how quickly Python can add a million numbers.
numbers = list(range(1, 1_000_001))
print(min(numbers))
print(max(numbers))
print(sum(numbers))