What are Default Arguments in Python?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Default arguments in Python represent the function arguments that will be used if no arguments are passed to the function call. The default arguments are represented as argument_name = value in the function definition.

Example of Default Arguments in Python

Consider a function that accepts price and discount rates as arguments and returns the new price based on the discount rate. The default discount rate is 3%. Below is the logic with and without using default arguments

Without Default Arguments

Code:

Output:

Explanation:

We want to calculate the final price with the default discount percentage (3%) in this example. So we passed None to discount_in_percentage, and hence default percentage is used. It can be achieved another way by passing 3 to the discount_in_percentage argument.

Even though we want to use the default discount percent, we must explicitly pass None or the default percent value to the function call. This can be avoided by using the default arguments.

With Default Arguments

Code:

Output:

Explanation:

In the function definition, we defined the default discount percent as 3 using the discount_in_percentage=3 statement. We didn't pass the discount percent to the method call final_price_after_discount(1000), and hence it took the default percentage.

Different Ways to Use Default Arguments in Python

Default arguments in Python can be used with two types of arguments:

  • With Keyword Arguments
  • Without Keyword Arguments

With Keyword Arguments

Keyword arguments are arguments in Python whose values are passed to the function call in the form of argument_name=value. The position of the arguments in the function call doesn't matter for keyword arguments.

Code:

In this example, the div() method accepts numerator as the first argument and denominator as the second argument. But the values can be passed in any order in the function call (numerator first and denominator second, and vice-versa) and still get the same result.

Let's look at an example to understand it better: In Python, we can use default arguments along with keyword arguments. Consider a function intro() that accepts greeting, name, and age as arguments and prints the intro. The intro() method has a single default argument greeting.

Code:

Output:

Explanation:

In this example, the arguments are declared in the order name, age, and greeting in the function definition. But we can pass those arguments in any order during function call using keyword arguments because the values are assigned correctly based on the argument name.

Without Keyword Arguments

Positional arguments are arguments in Python whose position (order) matters in the function call. Passing those arguments in the wrong order will assign them to the wrong variables.

In this example, the arguments should be passed to the' div' method in the correct order/position. Changing the position of the argument will result in wrong values. For example, div(10, 5) returns 2, whereas if we change the position to div(5, 10), we end up getting 0.5

Let's look at an example to understand it better:

In Python, we can use default arguments along with positional arguments. Consider a function intro() that accepts greeting, name, and age as arguments and prints the intro. The intro() method has a single default argument, greeting.

While using default and non-default arguments in the same method, the non-default arguments should be declared first before the default arguments. In this example, the non-default arguments (name, age) are added first in the function declaration before the default argument (greeting)

Code:

Output:

Explanation:

In this example, we passed the values in the function call without specifying the argument name. So, the values are assigned to the arguments based on their position in the function call.

Multiple Default Arguments

In Python, we can use multiple default arguments in the same function. Consider the below method with two default arguments, greeting and age.

Code:

Output:

Explanation:

  • For Tony Stark, we passed all the arguments: name, age, and greeting.
  • For Steve Rogers, we passed only name and age. The default value is used for greeting.
  • For Bruce Banner, we passed only name. Default values are used for age and greeting.

Conclusion

  • Default arguments in Python represent the function arguments that will be used if no arguments are passed to the function call.
  • The default arguments are represented as argument_name = value in the function definition.
  • Default arguments in Python can be used with keyword and positional arguments.
  • Keyword arguments are arguments in Python whose values are passed to the function call in the form of argument_name = value.
  • Positional arguments are arguments in Python whose position (order) matters in the function call.
  • In Python, we can use multiple default arguments in the same function.