Python 3
Taking a Vacation
Hard day at work? Rough day at school? Take a load off with a programming vacation!
Before We Begin
Let's first quickly review functions in Python.
def bigger(first, second):
print max(first, second)
return True
In the example above:
We define a function called bigger that has two arguments called first and second.
Then, we print out the larger of the two arguments using the built-in function max.
Finally, the bigger function returns True.
Now try creating a function yourself!
Planning Your Trip
When planning a vacation, it's very important to know exactly how much you're going to spend.
def wages(hours):
# If I make $8.35/hour...
return 8.35 * hours
The above example is just a refresher in how functions are defined.
Let's use functions to calculate your trip's costs.
Getting There
You're going to need to take a plane ride to get to your location.
def fruit_color(fruit):
if fruit == "apple":
return "red"
elif fruit == "banana":
return "yellow"
elif fruit == "pear":
return "green"
The example above defines the function fruit_color that accepts a string as the argument fruit.
The function returns a string if it knows the color of that fruit.
Transportation
You're also going to need a rental car in order for you to get around.
def finish_game(score):
tickets = 10 * score
if score >= 10:
tickets += 50
elif score >= 7:
tickets += 20
return tickets
In the above example, we first give the player 10 tickets for every point that the player scored. Then, we check the value of score multiple times.
First, we check if score is greater than or equal to 10. If it is, we give the player 50 bonus tickets.
If score is just greater than or equal to 7, we give the player 20 bonus tickets.
At the end, we return the total number of tickets earned by the player.
Remember that an elif statement is only checked if all preceding if/elif statements fail.
Pull it Together
Great! Now that you've got your 3 main costs figured out, let's put them together in order to find the total cost of your trip.
def double(n):
return 2 * n
def triple(p):
return 3 * p
def add(a, b):
return double(a) + triple(b)
We define two simple functions, double(n) and triple(p) that return 2 times or 3 times their input. Notice that they have n and p as their arguments.
We define a third function, add(a, b) that returns the sum of the previous two functions when called with a and b, respectively.
Hey, You Never Know!
You can't expect to only spend money on the plane ride, hotel, and rental car when going on a vacation. There also needs to be room for additional costs like fancy food or souvenirs.
def hotel_cost(nights):
return nights * 140
def plane_ride_cost(city):
if city == "Charlotte":
return 183
elif city == "Tampa":
return 220
elif city == "Pittsburgh":
return 222
elif city == "Los Angeles":
return 475
def rental_car_cost(days):
cost = days * 40
if days >= 7:
cost -= 50
elif days >= 3:
cost -= 20
return cost
def trip_cost(city, days, spending_money):
return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days) + spending_money
Plan Your Trip!
Nice work! Now that you have it all together, let's take a trip.
What if we went to Los Angeles for 5 days and brought an extra 600 dollars of spending money?
def hotel_cost(nights):
return nights * 140
def plane_ride_cost(city):
if city == "Charlotte":
return 183
elif city == "Tampa":
return 220
elif city == "Pittsburgh":
return 222
elif city == "Los Angeles":
return 475
def rental_car_cost(days):
cost = days * 40
if days >= 7:
cost -= 50
elif days >= 3:
cost -= 20
return cost
def trip_cost(city, days, spending_money):
return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days) + spending_money
print trip_cost('Los Angeles', 5, 600)
Python Lists and Dictionaries
Lists and dictionaries are powerful tools you can use to store, organize, and manipulate all kinds of information.
Introduction to Lists
Lists are a datatype you can use to store a collection of different pieces of information as a sequence under a single variable name. (Datatypes you've already learned about include strings, numbers, and booleans.)
You can assign items to a list with an expression of the form
list_name = [item_1, item_2]
with the items in between brackets. A list can also be empty: empty_list = []
.
Lists are very similar to strings, but there are a few key differences.
Access by Index
You can access an individual item on the list by its index. An index is like an address that identifies the item's place in the list. The index appears directly after the list name, in between brackets, like this: list_name[index]
.
List indices begin with 0, not 1! You access the first item in a list like this: list_name[0]
. The second item in a list is at index 1: list_name[1]
. Computer scientists love to start counting from zero.
New Neighbors
A list index behaves like any other variable name! It can be used to access as well as assign values.
You saw how to access a list index like this:
zoo_animals[0]
# Gets the value "pangolin”
You can see how assignment works on line 5:
zoo_animals[2] = "hyena"
# Changes "sloth" to "hyena"