Python Function Parameters
In this page:
What are Parameters and Arguments?
Parameters are the placeholder names listed in a function's definition, while arguments are the actual values supplied when the function is called — the distinction matters because parameters describe the function's interface, arguments fill it in for one specific call.
Example: What are Parameters and Arguments?
def greet(name): # name is the parameter
print("Hello,", name)
greet("Alex") # "Alex" is the argument
Positional Arguments
By default, Python matches arguments to parameters by position — the first argument fills the first parameter, and so on — so the order you pass values in a call must line up with the order they were defined.
Example: Positional Arguments
def describe(animal, sound):
print(animal, "says", sound)
describe("Cat", "Meow")
Keyword Arguments
Passing name="value" explicitly by parameter name, instead of relying on position, lets you supply arguments in any order and makes a call self-documenting, since the parameter name is visible right at the call site.
Example: Keyword Arguments
def describe(animal, sound):
print(animal, "says", sound)
describe(sound="Meow", animal="Cat")
Modifying Mutable Parameters
Because lists and dictionaries are mutable, passing one into a function and modifying it inside (like calling .append()) changes the original object the caller still holds — unlike passing a number or string, which can't be altered in place.
Example: Modifying Mutable Parameters
def add_item(cart):
cart.append("apple")
cart = []
add_item(cart)
print(cart)
Positional-Only and Keyword-Only Parameters
A / in the parameter list forces everything before it to be passed positionally, and a * forces everything after it to be passed by keyword only — both are optional safeguards that make a function's calling convention explicit and harder to misuse.
Example: Positional-Only and Keyword-Only Parameters
def greet(name, /, *, greeting):
print(greeting, name)
greet("Alex", greeting="Hi")
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: