1. 1
    You 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…
  2. 2
    Functions 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…
  3. 3
    After 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…
  4. 4
    Let’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…
  5. 5
    We’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 *…
  6. 6
    Let’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…
  7. 7
    Remember import this from the first exercise in this course? That was an example of importing a module. A module is a file that contains definitions—including variables and functions—that you c…
  8. 8
    Did 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 …
  9. 9
    Nice 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 …
  10. 10
    Great! 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…
  11. 11
    Universal 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 …
  12. 12
    Now 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…
  13. 13
    The 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…
  14. 14
    min() then returns the smallest of a given series of arguments.
  15. 15
    The 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 …
  16. 16
    Finally, 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…
  17. 17
    Okay! 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…
  18. 18
    Good work! Now let’s see what you remember about importing modules (and, specifically, what’s available in the math module).
  19. 19
    Perfect! 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,…
  1. 1
    Let’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…
  2. 2
    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 refr…
  3. 3
    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 == “pe…
  4. 4
    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 ret…
  5. 5
    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 ad…
  6. 6
    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.
  7. 7
    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?

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

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

Pro Logo