Test Index

CBSE Class 12 Computer Science 2026 Paper Set 4 Get it on Google PlayDownload on the App Store

© examsnet.com
Question : 22 of 37
Marks: +1, -0
What is the difference between default parameters and positional parameters in Python ? Also give an example of a function header which uses both.
Solution:  
Difference:
Default parametersPositional parameters
Given a default value in the function header, e.g. rate=5.Have no value in the header; they are just names, e.g. p.
The argument for it may be left out in the call; then the default value is used.An argument must be passed for each of them in the call (else TypeError).
Must be written after all positional (non-default) parameters in the header.Get their values by position: the 1st argument goes to the 1st parameter, and so on.
Example (function header using both):
def interest(p, t, rate=5):      # p, t: positional;  rate: default
return p * t * rate / 100
print(interest(1000, 2)) # rate takes default 5 -> 100.0
print(interest(1000, 2, 10)) # rate = 10 -> 200.0
Key point: A header such as def interest(rate=5, p, t): is a SyntaxError, because a non-default parameter cannot follow a default parameter.
© examsnet.com
Advertisements
Go to Question: