def and return
Python · Unit 4: Functions
- Full plan
- Python
- 9 exercises
A function is code with a name. def defines it, but doesn't run it:
it runs when you call it by its name and parentheses.
You can call it as many times as you want.
def greet():
print("Hello")
print("How are you?")
greet()
greet()Prints
Hello How are you? Hello How are you?
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Put the lines in order
Put the lines in order to define the function and then call it.
3. Predict the output
What does this code print?
4. Complete the code
Fill in the blanks so the function hands back the square and the program prints 26.
5. Multiple choice
What does this function do?
6. Predict the output
What does this code print?
7. Find the bug
This program should print what two 50 tickets cost. Which line has the error?
8. Find the case that fails
sign(n)should hand back "positive", "negative" or "zero". Which call makes it fail?9. Predict the output
What does this code print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.