Positional parameters
- Python course
- Beginner
- Lesson 77/116
- 4 min
Learn to pass positional parameters.
Positional parameters
A positional parameter matches by its position in the call. Order matters:
def introduce(name, age):
print(name, age)
introduce("Ayse", 28) # Ayse 28
introduce(28, "Ayse") # 28 AyseThe second call runs but is meaningless; because 28 goes as name and "Ayse" as age. With positional calls you must respect the order.
Positional parameters are the most common form and handy for short calls.
Positional call limitations
In functions with many parameters, remembering the order gets hard. Then keyword calls are safer:
def create_user(name, age, role, active):
...
create_user("Ayse", 28, "admin", True) # which value is what?This call runs but is hard to read. To know which value goes to which parameter, you must check the function definition.
This problem is solved in the next lesson with keyword parameters.
Practice Write a positional call
Define a function called introduce with name and age positional parameters. The body does print(name, age). Call it with introduce("Ayse", 28).
Starter code
# Define the introduce functionSample solution
def introduce(name, age):
print(name, age)
introduce("Ayse", 28)Expected output
Ayse 28
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 In def f(a, b): ... called as f(1, 2), what is b?
- Raises an error
- 1
- 2 Correct answer
- None
Explanation: Positionally the second value 2 goes to b.
2 Complete the second positional parameter
def introduce(name, ____):
print(name, age)
def introduce(name, age):
print(name, age)
Explanation: The second parameter should be named age.
3 Match positional parameter concepts
| Term | Meaning |
|---|---|
order |
Matching rule |
f(1, 2) |
Call shape |
def f(a, b) |
Definition shape |
second value |
Goes to b |