All Courses

Create a function which will have default parameters and doesn’t give error when we don’t pass the parameters

By Manerushi149@gmail.com, 3 months ago
  • Bookmark
0

Create a function which will have default parameters and doesn’t give error when we don’t pass the parameters

Python
Functions
Default parameters
2 Answers
0
Hitendradixit18@gmail.com

Hint: Default parameters question it is we read about this in the functions session

0
Goutamp777

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.

Your Answer

Webinars

Live Masterclass on : "How Machine Get Trained in Machine Learning?"

Mar 30th (7:00 PM) 516 Registered
More webinars

Related Discussions

Running random forest algorithm with one variable

View More