Skip to content

Commit

Permalink
Added source code for all chapters.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Matthes authored and Eric Matthes committed May 1, 2019
1 parent 87b94e7 commit 4002a3c
Show file tree
Hide file tree
Showing 420 changed files with 246,611 additions and 0 deletions.
3 changes: 3 additions & 0 deletions chapter_01/Python3.sublime-build
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cmd": ["python3", "-u", "$file"],
}
1 change: 1 addition & 0 deletions chapter_01/hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello Python world!")
2 changes: 2 additions & 0 deletions chapter_02/apostrophe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
message = "One of Python's strengths is its diverse community."
print(message)
2 changes: 2 additions & 0 deletions chapter_02/comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Say hello to everyone.
print("Hello Python people!")
5 changes: 5 additions & 0 deletions chapter_02/full_name.py
Original file line number Diff line number Diff line change
@@ -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)
5 changes: 5 additions & 0 deletions chapter_02/hello_world.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
message = "Hello Python world!"
print(message)

message = "Hello Python Crash Course world!"
print(message)
3 changes: 3 additions & 0 deletions chapter_02/name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name = "Ada Lovelace"
print(name.upper())
print(name.lower())
4 changes: 4 additions & 0 deletions chapter_03/bicycles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
message = f"My first bicycle was a {bicycles[0].title()}."

print(message)
5 changes: 5 additions & 0 deletions chapter_03/cars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cars = ['bmw', 'audi', 'toyota', 'subaru']
print(cars)

cars.reverse()
print(cars)
7 changes: 7 additions & 0 deletions chapter_03/motorcycles.py
Original file line number Diff line number Diff line change
@@ -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.")
3 changes: 3 additions & 0 deletions chapter_04/dimensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dimensions = (200, 50)
for dimension in dimensions:
print(dimension)
2 changes: 2 additions & 0 deletions chapter_04/even_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
even_numbers = list(range(2, 11, 2))
print(even_numbers)
2 changes: 2 additions & 0 deletions chapter_04/first_numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numbers = list(range(1, 6))
print(numbers)
11 changes: 11 additions & 0 deletions chapter_04/foods.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions chapter_04/magicians.py
Original file line number Diff line number Diff line change
@@ -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!")
5 changes: 5 additions & 0 deletions chapter_04/players.py
Original file line number Diff line number Diff line change
@@ -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())
2 changes: 2 additions & 0 deletions chapter_04/squares.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
squares = [value**2 for value in range(1, 11)]
print(squares)
12 changes: 12 additions & 0 deletions chapter_05/amusement_park.py
Original file line number Diff line number Diff line change
@@ -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}.")
5 changes: 5 additions & 0 deletions chapter_05/banned_users.py
Original file line number Diff line number Diff line change
@@ -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.")
7 changes: 7 additions & 0 deletions chapter_05/cars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
cars = ['audi', 'bmw', 'subaru', 'toyota']

for car in cars:
if car == 'bmw':
print(car.upper())
else:
print(car.title())
3 changes: 3 additions & 0 deletions chapter_05/magic_number.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
answer = 17
if answer != 42:
print("That is not the correct answer. Please try again!")
12 changes: 12 additions & 0 deletions chapter_05/toppings.py
Original file line number Diff line number Diff line change
@@ -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!")
7 changes: 7 additions & 0 deletions chapter_05/voting.py
Original file line number Diff line number Diff line change
@@ -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!")
17 changes: 17 additions & 0 deletions chapter_06/alien.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
alien_0 = {'x_position': 0, 'y_position': 25, 'speed': 'medium'}
print(f"Original position: {alien_0['x_position']}")

# Move the alien to the right.
# Determine how far to move the alien based on its current speed.
if alien_0['speed'] == 'slow':
x_increment = 1
elif alien_0['speed'] == 'medium':
x_increment = 2
else:
# This must be a fast alien.
x_increment = 3

# The new position is the old position plus the increment.
alien_0['x_position'] = alien_0['x_position'] + x_increment

print(f"New position: {alien_0['x_position']}")
18 changes: 18 additions & 0 deletions chapter_06/aliens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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("...")
14 changes: 14 additions & 0 deletions chapter_06/favorite_languages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}

friends = ['phil', 'sarah']
for name in favorite_languages.keys():
print(name.title())

if name in friends:
language = favorite_languages[name].title()
print(f"\t{name.title()}, I see you love {language}!")
22 changes: 22 additions & 0 deletions chapter_06/many_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
users = {
'aeinstein': {
'first': 'albert',
'last': 'einstein',
'location': 'princeton',
},

'mcurie': {
'first': 'marie',
'last': 'curie',
'location': 'paris',
},

}

for username, user_info in users.items():
print(f"\nUsername: {username}")
full_name = f"{user_info['first']} {user_info['last']}"
location = user_info['location']

print(f"\tFull name: {full_name.title()}")
print(f"\tLocation: {location.title()}")
12 changes: 12 additions & 0 deletions chapter_06/pizza.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Store information about a pizza being ordered.
pizza = {
'crust': 'thick',
'toppings': ['mushrooms', 'extra cheese'],
}

# Summarize the order.
print(f"You ordered a {pizza['crust']}-crust pizza "
"with the following toppings:")

for topping in pizza['toppings']:
print("\t" + topping)
9 changes: 9 additions & 0 deletions chapter_06/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
user_0 = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}

for key, value in user_0.items():
print(f"\nKey: {key}")
print(f"Value: {value}")
10 changes: 10 additions & 0 deletions chapter_07/cities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
prompt = "\nPlease enter the name of a city you have visited:"
prompt += "\n(Enter 'quit' when you are finished.) "

while True:
city = input(prompt)

if city == 'quit':
break
else:
print(f"I'd love to go to {city.title()}!")
17 changes: 17 additions & 0 deletions chapter_07/confirmed_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Start with users that need to be verified,
# and an empty list to hold confirmed users.
unconfirmed_users = ['alice', 'brian', 'candace']
confirmed_users = []

# Verify each user until there are no more unconfirmed users.
# Move each verified user into the list of confirmed users.
while unconfirmed_users:
current_user = unconfirmed_users.pop()

print(f"Verifying user: {current_user.title()}")
confirmed_users.append(current_user)

# Display all confirmed users.
print("\nThe following users have been confirmed:")
for confirmed_user in confirmed_users:
print(confirmed_user.title())
7 changes: 7 additions & 0 deletions chapter_07/counting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
current_number = 0
while current_number < 10:
current_number += 1
if current_number % 2 == 0:
continue

print(current_number)
7 changes: 7 additions & 0 deletions chapter_07/even_or_odd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
number = input("Enter a number, and I'll tell you if it's even or odd: ")
number = int(number)

if number % 2 == 0:
print(f"\nThe number {number} is even.")
else:
print(f"\nThe number {number} is odd.")
5 changes: 5 additions & 0 deletions chapter_07/greeter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += "\nWhat is your first name? "

name = input(prompt)
print(f"\nHello, {name}!")
22 changes: 22 additions & 0 deletions chapter_07/mountain_poll.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
responses = {}
_poll.py
# Set a flag to indicate that polling is active.
polling_active = True

while polling_active:
# Prompt for the person's name and response.
name = input("\nWhat is your name? ")
response = input("Which mountain would you like to climb someday? ")

# Store the response in the dictionary.
responses[name] = response

# Find out if anyone else is going to take the poll.
repeat = input("Would you like to let another person respond? (yes/ no) ")
if repeat == 'no':
polling_active = False

# Polling is complete. Show the results.
print("\n--- Poll Results ---")
for name, response in responses.items():
print(f"{name} would like to climb {response}.")
11 changes: 11 additions & 0 deletions chapter_07/parrot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
prompt = "\nTell me something, and I will repeat it back to you:"
prompt += "\nEnter 'quit' to end the program. "

active = True
while active:
message = input(prompt)

if message == 'quit':
active = False
else:
print(message)
7 changes: 7 additions & 0 deletions chapter_07/pets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)

while 'cat' in pets:
pets.remove('cat')

print(pets)
7 changes: 7 additions & 0 deletions chapter_07/rollercoaster.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
height = input("How tall are you, in inches? ")
height = int(height)

if height >= 48:
print("\nYou're tall enough to ride!")
else:
print("\nYou'll be able to ride when you're a little older.")
13 changes: 13 additions & 0 deletions chapter_08/formatted_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
def get_formatted_name(first_name, last_name, middle_name=''):
"""Return a full name, neatly formatted."""
if middle_name:
full_name = f"{first_name} {middle_name} {last_name}"
else:
full_name = f"{first_name} {last_name}"
return full_name.title()

musician = get_formatted_name('jimi', 'hendrix')
print(musician)

musician = get_formatted_name('john', 'hooker', 'lee')
print(musician)
8 changes: 8 additions & 0 deletions chapter_08/greet_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
def greet_users(names):
"""Print a simple greeting to each user in the list."""
for name in names:
msg = f"Hello, {name.title()}!"
print(msg)

usernames = ['hannah', 'ty', 'margot']
greet_users(usernames)
20 changes: 20 additions & 0 deletions chapter_08/greeter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def get_formatted_name(first_name, last_name):
"""Return a full name, neatly formatted."""
full_name = f"{first_name} {last_name}"
return full_name.title()

# This is an infinite loop!
while True:
print("\nPlease tell me your name:")
print("(enter 'q' at any time to quit)")

f_name = input("First name: ")
if f_name == 'q':
break

l_name = input("Last name: ")
if l_name == 'q':
break

formatted_name = get_formatted_name(f_name, l_name)
print(f"\nHello, {formatted_name}!")
4 changes: 4 additions & 0 deletions chapter_08/making_pizzas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import pizza

pizza.make_pizza(16, 'pepperoni')
pizza.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')
9 changes: 9 additions & 0 deletions chapter_08/person.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def build_person(first_name, last_name, age=None):
"""Return a dictionary of information about a person."""
person = {'first': first_name, 'last': last_name}
if age:
person['age'] = age
return person

musician = build_person('jimi', 'hendrix', age=27)
print(musician)
Loading

0 comments on commit 4002a3c

Please sign in to comment.