Q.What is the difference between default parameters and positional parameters in Python ? Also give an example of a function header which uses both.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Default parameters have pre-assigned values used when no argument is passed; positional parameters must receive an argument in the correct order. A function header like def greet(name, greeting="Hello") uses both — name is positional, greeting is default.
The core idea
When you define a function in Python, you decide which inputs are mandatory and which are optional. Positional parameters are the mandatory ones — the caller must supply a value for each, in the exact order they appear. Default parameters are optional — they already have a value baked in, so the caller can skip them if the default works.
Think of it like ordering at a restaurant. The positional parameter is the dish you must choose (e.g., "pasta"). The default parameter is like asking for extra cheese — if you say nothing, you get no extra cheese; but you can override it by saying "yes, please."
Step-by-step breakdown
-
Positional parameters
These are the parameters listed first in the function definition. They get their values from the arguments passed in the same left-to-right order. Every positional parameter must be supplied an argument — there is no fallback value.
Example:
def add(a, b): return a + bHere,
aandbare positional. Callingadd(5)would raise aTypeErrorbecausebis missing. -
Default parameters
These are assigned a default value in the function header using the
=operator. If the caller omits that argument, the default is used. If they do pass a value, it overrides the default.Example:
def power(base, exp=2): return base ** expexphas default2. Sopower(3)returns3 ** 2, which is 9, whilepower(3, 3)returns3 ** 3, which is 27. -
The key difference
- Positional: required, no default, order matters.
- Default: optional, has a default, can be omitted.
Python enforces that all positional parameters come before any default parameters in the function header. This is not just convention — it's a syntax rule. If you write
def f(a=1, b):, Python will raise aSyntaxError.
A common mistake is to think default parameters are evaluated once at definition time, not each time the function is called. This matters for mutable defaults like lists or dictionaries. For example:
def append_to(item, lst=[]):
lst.append(item)
return lst
``` …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.