Added solutions documents for ease of reference

This commit is contained in:
cheeks 2025-03-14 13:22:26 +00:00
parent 3a58770176
commit bf85d67430
1500 changed files with 296335 additions and 0 deletions

12
pcc-solutions/.gitignore vendored Normal file
View File

@ -0,0 +1,12 @@
.DS_Store
__pycache__/
*.pyc
*_env/
.venv*/
online_resources_site/site/
*.sqlite3

13
pcc-solutions/README.md Normal file
View File

@ -0,0 +1,13 @@
Python Crash Course - Third Edition
===
A Hands-On, Project-Based Introduction to Programming
---
This is a collection of resources for [Python Crash Course, Third Edition](https://nostarch.com/python-crash-course-3rd-edition), an introductory programming book from [No Starch Press](https://nostarch.com) by Eric Matthes. Click here for a [much cleaner version](https://ehmatthes.github.io/pcc_3e/) of these online resources.
If you have any questions about Python Crash Course, feel free to get in touch:
Email: ehmatthes@gmail.com
Twitter: [@ehmatthes](http://twitter.com/ehmatthes/)

View File

@ -0,0 +1 @@
print("Hello Python world!")

View File

@ -0,0 +1,2 @@
message = "One of Python's strengths is its diverse community."
print(message)

View File

@ -0,0 +1,2 @@
# Say hello to everyone.
print("Hello Python people!")

View File

@ -0,0 +1,4 @@
first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"
print(full_name)

View File

@ -0,0 +1,4 @@
first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"
print(f"Hello, {full_name.title()}!")

View File

@ -0,0 +1,5 @@
first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"
message = f"Hello, {full_name.title()}!"
print(message)

View File

@ -0,0 +1,2 @@
message = "Hello Python world!"
print(message)

View File

@ -0,0 +1,2 @@
message = "Hello Python world!"
print(message)

View File

@ -0,0 +1,5 @@
message = "Hello Python world!"
print(message)
message = "Hello Python Crash Course world!"
print(message)

View File

@ -0,0 +1,2 @@
name = "ada lovelace"
print(name.title())

View File

@ -0,0 +1,3 @@
name = "Ada Lovelace"
print(name.upper())
print(name.lower())

View File

@ -0,0 +1,4 @@
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
message = f"My first bicycle was a {bicycles[0].title()}."
print(message)

View File

@ -0,0 +1,5 @@
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
cars.reverse()
print(cars)

View File

@ -0,0 +1,7 @@
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
too_expensive = 'ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
print(f"\nA {too_expensive.title()} is too expensive for me.")

View File

@ -0,0 +1,2 @@
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)

View File

@ -0,0 +1,2 @@
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0])

View File

@ -0,0 +1,2 @@
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0].title())

View File

@ -0,0 +1,3 @@
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[1])
print(bicycles[3])

View File

@ -0,0 +1,2 @@
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[-1])

View File

@ -0,0 +1,4 @@
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
message = f"My first bicycle was a {bicycles[0].title()}."
print(message)

View File

@ -0,0 +1,3 @@
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)

View File

@ -0,0 +1,3 @@
cars = ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)
print(cars)

View File

@ -0,0 +1,10 @@
cars = ['bmw', 'audi', 'toyota', 'subaru']
print("Here is the original list:")
print(cars)
print("\nHere is the sorted list:")
print(sorted(cars))
print("\nHere is the original list again:")
print(cars)

View File

@ -0,0 +1,5 @@
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)
cars.reverse()
print(cars)

View File

@ -0,0 +1,5 @@
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles[0] = 'ducati'
print(motorcycles)

View File

@ -0,0 +1,7 @@
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
too_expensive = 'ducati'
motorcycles.remove(too_expensive)
print(motorcycles)
print(f"\nA {too_expensive.title()} is too expensive for me.")

View File

@ -0,0 +1,2 @@
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles[3])

View File

@ -0,0 +1,5 @@
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
motorcycles.append('ducati')
print(motorcycles)

View File

@ -0,0 +1,7 @@
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)

View File

@ -0,0 +1,4 @@
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.insert(0, 'ducati')
print(motorcycles)

View File

@ -0,0 +1,5 @@
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)

View File

@ -0,0 +1,5 @@
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[1]
print(motorcycles)

View File

@ -0,0 +1,6 @@
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
popped_motorcycle = motorcycles.pop()
print(motorcycles)
print(popped_motorcycle)

View File

@ -0,0 +1,4 @@
motorcycles = ['honda', 'yamaha', 'suzuki']
last_owned = motorcycles.pop()
print(f"The last motorcycle I owned was a {last_owned.title()}.")

View File

@ -0,0 +1,4 @@
motorcycles = ['honda', 'yamaha', 'suzuki']
first_owned = motorcycles.pop(0)
print(f"The first motorcycle I owned was a {first_owned.title()}.")

View File

@ -0,0 +1,5 @@
motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
print(motorcycles)
motorcycles.remove('ducati')
print(motorcycles)

View File

@ -0,0 +1,9 @@
dimensions = (200, 50)
print("Original dimensions:")
for dimension in dimensions:
print(dimension)
dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)

View File

@ -0,0 +1,2 @@
even_numbers = list(range(2, 11, 2))
print(even_numbers)

View File

@ -0,0 +1,2 @@
numbers = list(range(1, 6))
print(numbers)

View File

@ -0,0 +1,11 @@
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)

View File

@ -0,0 +1,6 @@
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")
print("Thank you, everyone. That was a great magic show!")

View File

@ -0,0 +1,3 @@
dimensions = (200, 50)
print(dimensions[0])
print(dimensions[1])

View File

@ -0,0 +1,2 @@
dimensions = (200, 50)
dimensions[0] = 250

View File

@ -0,0 +1,3 @@
dimensions = (200, 50)
for dimension in dimensions:
print(dimension)

View File

@ -0,0 +1,9 @@
dimensions = (200, 50)
print("Original dimensions:")
for dimension in dimensions:
print(dimension)
dimensions = (400, 100)
print("\nModified dimensions:")
for dimension in dimensions:
print(dimension)

View File

@ -0,0 +1,2 @@
even_numbers = list(range(2, 11, 2))
print(even_numbers)

View File

@ -0,0 +1,2 @@
for value in range(1, 5):
print(value)

View File

@ -0,0 +1,2 @@
for value in range(1, 6):
print(value)

View File

@ -0,0 +1,2 @@
numbers = list(range(1, 6))
print(numbers)

View File

@ -0,0 +1,8 @@
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)

View File

@ -0,0 +1,11 @@
my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)

View File

@ -0,0 +1,13 @@
my_foods = ['pizza', 'falafel', 'carrot cake']
# This doesn't work:
friend_foods = my_foods
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)

View File

@ -0,0 +1,2 @@
message = "Hello Python world!"
print(message)

View File

@ -0,0 +1,3 @@
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)

View File

@ -0,0 +1,3 @@
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")

View File

@ -0,0 +1,4 @@
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")

View File

@ -0,0 +1,6 @@
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")
print("Thank you, everyone. That was a great magic show!")

View File

@ -0,0 +1,3 @@
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(magician)

View File

@ -0,0 +1,4 @@
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")

View File

@ -0,0 +1,6 @@
magicians = ['alice', 'david', 'carolina']
for magician in magicians:
print(f"{magician.title()}, that was a great trick!")
print(f"I can't wait to see your next trick, {magician.title()}.\n")
print("Thank you everyone, that was a great magic show!")

View File

@ -0,0 +1,3 @@
magicians = ['alice', 'david', 'carolina']
for magician in magicians
print(magician)

View File

@ -0,0 +1,2 @@
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[0:3])

View File

@ -0,0 +1,2 @@
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[1:4])

View File

@ -0,0 +1,2 @@
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[:4])

View File

@ -0,0 +1,2 @@
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[2:])

View File

@ -0,0 +1,2 @@
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print(players[-3:])

View File

@ -0,0 +1,5 @@
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print("Here are the first three players on my team:")
for player in players[:3]:
print(player.title())

View File

@ -0,0 +1,6 @@
squares = []
for value in range(1, 11):
square = value ** 2
squares.append(square)
print(squares)

View File

@ -0,0 +1,5 @@
squares = []
for value in range(1, 11):
squares.append(value**2)
print(squares)

View File

@ -0,0 +1,2 @@
squares = [value**2 for value in range(1, 11)]
print(squares)

View File

@ -0,0 +1,5 @@
players = ['charles', 'martina', 'michael', 'florence', 'eli']
print("Here are the first three players on my team:")
for player in players[:3]:
print(player.title())

View File

@ -0,0 +1,5 @@
squares = []
for value in range(1, 11):
squares.append(value**2)
print(squares)

View File

@ -0,0 +1,2 @@
squares = [value**2 for value in range(1, 11)]
print(squares)

View File

@ -0,0 +1,12 @@
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 40
elif age >= 65:
price = 20
print(f"Your admission cost is ${price}.")

View File

@ -0,0 +1,5 @@
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
print(f"{user.title()}, you can post a response if you wish.")

View File

@ -0,0 +1,7 @@
cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())

View File

@ -0,0 +1,4 @@
answer = 17
if answer != 42:
print("That is not the correct answer. Please try again!")

View File

@ -0,0 +1,7 @@
age = 12
if age < 4:
print("Your admission cost is $0.")
elif age < 18:
print("Your admission cost is $25.")
else:
print("Your admission cost is $40.")

View File

@ -0,0 +1,10 @@
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
else:
price = 40
print(f"Your admission cost is ${price}.")

View File

@ -0,0 +1,12 @@
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 40
else:
price = 20
print(f"Your admission cost is ${price}.")

View File

@ -0,0 +1,12 @@
age = 12
if age < 4:
price = 0
elif age < 18:
price = 25
elif age < 65:
price = 40
elif age >= 65:
price = 20
print(f"Your admission cost is ${price}.")

View File

@ -0,0 +1,5 @@
banned_users = ['andrew', 'carolina', 'david']
user = 'marie'
if user not in banned_users:
print(f"{user.title()}, you can post a response if you wish.")

View File

@ -0,0 +1,7 @@
cars = ['audi', 'bmw', 'subaru', 'toyota']
for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())

View File

@ -0,0 +1,4 @@
answer = 17
if answer != 42:
print("That is not the correct answer. Please try again!")

View File

@ -0,0 +1,4 @@
requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
print("Hold the anchovies!")

View File

@ -0,0 +1,10 @@
requested_toppings = ['mushrooms', 'extra cheese']
if 'mushrooms' in requested_toppings:
print("Adding mushrooms.")
if 'pepperoni' in requested_toppings:
print("Adding pepperoni.")
if 'extra cheese' in requested_toppings:
print("Adding extra cheese.")
print("\nFinished making your pizza!")

View File

@ -0,0 +1,6 @@
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
print(f"Adding {requested_topping}.")
print("\nFinished making your pizza!")

View File

@ -0,0 +1,9 @@
requested_toppings = ['mushrooms', 'green peppers', 'extra cheese']
for requested_topping in requested_toppings:
if requested_topping == 'green peppers':
print("Sorry, we are out of green peppers right now.")
else:
print(f"Adding {requested_topping}.")
print("\nFinished making your pizza!")

View File

@ -0,0 +1,8 @@
requested_toppings = []
if requested_toppings:
for requested_topping in requested_toppings:
print(f"Adding {requested_topping}.")
print("\nFinished making your pizza!")
else:
print("Are you sure you want a plain pizza?")

View File

@ -0,0 +1,12 @@
available_toppings = ['mushrooms', 'olives', 'green peppers',
'pepperoni', 'pineapple', 'extra cheese']
requested_toppings = ['mushrooms', 'french fries', 'extra cheese']
for requested_topping in requested_toppings:
if requested_topping in available_toppings:
print(f"Adding {requested_topping}.")
else:
print(f"Sorry, we don't have {requested_topping}.")
print("\nFinished making your pizza!")

View File

@ -0,0 +1,3 @@
age = 19
if age >= 18:
print("You are old enough to vote!")

View File

@ -0,0 +1,4 @@
age = 19
if age >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")

View File

@ -0,0 +1,7 @@
age = 17
if age >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
else:
print("Sorry, you are too young to vote.")
print("Please register to vote as soon as you turn 18!")

View File

@ -0,0 +1,12 @@
available_toppings = ['mushrooms', 'olives', 'green peppers',
'pepperoni', 'pineapple', 'extra cheese']
requested_toppings = ['mushrooms', 'french fries', 'extra cheese']
for requested_topping in requested_toppings:
if requested_topping in available_toppings:
print(f"Adding {requested_topping}.")
else:
print(f"Sorry, we don't have {requested_topping}.")
print("\nFinished making your pizza!")

View File

@ -0,0 +1,7 @@
age = 17
if age >= 18:
print("You are old enough to vote!")
print("Have you registered to vote yet?")
else:
print("Sorry, you are too young to vote.")
print("Please register to vote as soon as you turn 18!")

View File

@ -0,0 +1,5 @@
alien_0 = {'color': 'green', 'points': 5}
print(alien_0)
del alien_0['points']
print(alien_0)

View File

@ -0,0 +1,4 @@
alien_0 = {'color': 'green', 'speed': 'slow'}
point_value = alien_0.get('points', 'No point value assigned.')
print(point_value)

View File

@ -0,0 +1,21 @@
# Make an empty list for storing aliens.
aliens = []
# Make 30 green aliens.
for alien_number in range(30):
new_alien = {'color': 'green', 'points': 5, 'speed': 'slow'}
aliens.append(new_alien)
for alien in aliens[:3]:
if alien['color'] == 'green':
alien['color'] = 'yellow'
alien['speed'] = 'medium'
alien['points'] = 10
# Show the first 5 aliens.
for alien in aliens[:5]:
print(alien)
print("...")
# Show how many aliens have been created.
print(f"Total number of aliens: {len(aliens)}")

Some files were not shown because too many files have changed in this diff Show More