Functions
Lesson 1 of 2
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 import*ing 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,…

## 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