Functions · Lesson 12 of 25
Parameters and scope
Defaults, keyword arguments and local variables.
8 minutes2 games
Pass arguments by position or by name. Defaults apply when an argument is omitted. A variable assigned inside a function is local; prefer arguments and return values over shared global state.
Python
tax = 0.1
def total(price, qty=1):
subtotal = price * qty
return subtotal
print(total(50))
print(total(qty=3, price=50))
print(tax)| Tool | Use |
|---|---|
| qty defaults to 1 |
| Positional arguments |
| Keyword arguments |
What's the output?
def greet(name="friend"):
return f"Hi {name}"
print(greet())What will this code print?
Exercise
Define discount(price, percent=10) returning the discounted price. Print discount(200).
Expected output: 180.0
Python
Learn more from
Played 0 of 2 games
Sign in to save your progress and earn XP.