World's Best AI Learning Platform with profoundly Demanding Certification Programs
Designed by IITians, only for AI Learners.
Designed by IITians, only for AI Learners.
New to InsideAIML? Create an account
Employer? Create an account
Hint: Default parameters question it is we read about this in the functions session
Here is an example of a Python function that uses default parameters, so that it doesn't give an error when they are not passed:
def greet(name="world", greeting="Hello"): print(f"{greeting}, {name}!") greet() # "Hello, world!" greet("John") # "Hello, John!" greet("John", "Hi") # "Hi, John!"
In this example, the greet()
function takes two parameters: name
and greeting
, both of which have default values of "world" and "Hello", respectively. When the function is called without any arguments, it uses the default values for both name
and greeting
. When the function is called with one argument, it uses that argument for the name
parameter and the default value for greeting
. And when the function is called with two arguments, it uses those arguments for the name
and greeting
parameters, respectively.
Please note that the default parameter must be immutable object types like int, string, tuple.