*args
Python · Unit 10: More on functions
- Full plan
- Python
- 10 exercises
With * before a parameter, the function takes any number of arguments, even none. They all arrive together in a tuple.
It's usually called *args, but you choose the name.
def show(*nums):
print(nums)
show(4, 7, 1)
show(9)
show()Prints
(4, 7, 1) (9,) ()
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. Predict the output
What does this code print?
3. Predict the output
What does this code print?
4. Complete the code
Fill in the blank so the function takes as many grades as you give it.
5. Find the case that fails
largest(*nums)should return the biggest of the numbers it gets, orNoneif it gets none. Which call breaks it?6. Complete the code
Fill in the blank to work out the area with the sizes in the list.
7. Find the bug
This program should print 65, the sum of the prices. Which line has the bug?
8. Put the lines in order
Put the function in order. It counts how many even numbers it gets, however many there are.
9. Multiple choice
What does this function do?
10. Predict the output
With 3 or more items there's a discount of 10. What does it print?
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.