- 1You might have considered the situation where you would like to reuse a piece of code, just with a few different values. Instead of rewriting the whole code, it’s much cleaner to define a *function…
- 2Functions are defined with three components: 1. The
*header*, which includes the def keyword, the name of the function, and any*parameters*the function requires. Here’s an example: def h… - 3After defining a function, it must be
*called*to be implemented. In the previous exercise, spam() in the last line told the program to look for the function called spam and execute the code inside… - 4Let’s take another look at the definition of the function square from the previous exercise: def square(n): Here, n is a
*parameter*of square. A parameter is a variable that is an input to a fu… - 5We’ve seen functions that can print text or do simple arithmetic, but functions can be much more powerful than that. For example, a function can call another function: def fun_one(n): return n *…
- 6Let’s create a few more functions just for good measure. def shout(phrase): if phrase == phrase.upper(): return “YOU’RE SHOUTING!” else: return “Can you speak up?” shout(“I’M INTEREST…
- 7Remember import this from the first exercise in this course? That was an example of
*import*ing a*module*. A module is a file that contains definitions—including variables and functions—that you c… - 8Did you see that? Python said: NameError: name ‘sqrt’ is not defined. Python doesn’t know what square roots are—yet. There is a Python module named math that includes a number of useful variables …
- 9Nice work! Now Python knows how to take the square root of a number. However, we only really needed the sqrt function, and it can be frustrating to have to keep typing math.sqrt(). It’s possible …
- 10Great! We’ve found a way to handpick the variables and functions we want from modules. What if we still want all of the variables and functions in a module but don’t want to have to constantly typ…
- 11Universal imports may look great on the surface, but they’re not a good idea for one very important reason: they fill your program with a
*ton*of variable and function names without the safety of … - 12Now that you understand what functions are and how to
*import modules*, let’s look at some of the functions that are built in to Python (no modules required!). You already know about some of the b… - 13The max() function takes any number of arguments and returns the largest one. (“Largest” can have odd definitions here, so it’s best to use max() on integers and floats, where the results are strai…
- 14min() then returns the smallest of a given series of arguments.
- 15The abs() function returns the
**absolute value**of the number it takes as an argument—that is, that number’s distance from 0 on an imagined number line. For instance, 3 and -3 both have the same … - 16Finally, the type() function returns the
*type*of the data it receives as an argument. If you ask Python to do the following: print type(42) print type(4.2) print type(‘spam’) Python will outpu… - 17Okay! Let’s review functions. def speak(message): return message if happy(): speak(“I’m happy!”) elif sad(): speak(“I’m sad.”) else: speak(“I don’t know what I’m feeling.”) Again, the e…
- 18Good work! Now let’s see what you remember about
*importing modules*(and, specifically, what’s available in the math module). - 19Perfect! Last but not least, let’s review the built-in functions you’ve learned about in this lesson. def is_numeric(num): return type(num) == int or type(num) == float: max(2, 3, 4) # 4 min(2,…

- 1Let’s first quickly review functions in Python. def bigger(first, second): print max(first, second) return True In the example above: 1. We define a function called bigger that has two argu…
- 2When 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 refr…
- 3You’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 == “pe…
- 4You’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 ret…
- 5Great! 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 ad… - 6You 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.
- 7Nice 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?

## What you'll create

Portfolio projects that showcase your new skills

## How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory