Default arguments in Python

Python will enable the Python arguments to have the default values. Whereas, if the function is defined without an argument. Then the argument will have a default value. Read this article to learn more about Default arguments in Python.

What is a default value?

Python has several ways of showing syntax and the default value for function arguments. The default value will represent the function argument that has the value.

However, if no argument value can be passed during the function call. Then the default value will be provided with the help of the assignment (=) operator of the form keywordname=value

Check out the example provided below

student(firstname, lastname ='Mark', standard ='Fifth'):
 
     print(firstname, lastname, 'studies in', standard, 'Standard')

Output

John Mark studies in Fifth Standard
Jane Smith studies in Fifth Standard
Bob Johnson studies in Sixth Standard

What are the examples of Python Functions?

Example 1: Calling Function without the Keyword arguments

def student(firstname, lastname='Mark', standard='Fifth'):
    print(firstname, lastname, 'studies in', standard, 'Standard')

# Call the function with different combinations of arguments
student("John")
student("Jane", "Smith")
student("Bob", "Johnson", "Sixth")

Output

John Mark studies in Fifth Standard
Jane Smith studies in Fifth Standard
Bob Johnson studies in Sixth Standard

Example 2: Calling Function with Keyword arguments

def student(firstname, lastname='Mark', standard='Fifth'):
    print(firstname, lastname, 'studies in', standard, 'Standard')

# Call the function with keyword arguments
student(firstname="Alice", standard="Sixth")
student(firstname="Charlie", lastname="Brown", standard="Fourth")
student(firstname="Eva", lastname="Miller")

Output

Alice Mark studies in Sixth Standard
Charlie Brown studies in Fourth Standard
Eva Miller studies in Fifth Standard

Example 3: Some Invalid Function calls

def student(firstname, lastname='Mark', standard='Fifth'):
    print(firstname, lastname, 'studies in', standard, 'Standard')

# Invalid call: Missing required argument 'firstname'
# student()

# Invalid call: Providing a non-string value for 'lastname'
# student("John", 123)

# Invalid call: Unknown keyword argument 'grade'
# student(firstname="Alice", grade="A")

# Invalid call: Providing too many positional arguments
# student("Bob", "Johnson", "Sixth", "ExtraArgument")

 Default argument values in Python

In Python, when you create a function with default values, those values are set when the function is defined. However, if the default value is a list or another mutable object, it can cause unexpected behavior

Let us look into the example given below:

def add_fruit(fruit, basket=[]):
    basket.append(fruit)
    return basket

# Example calls
print(add_fruit("apple"))   # Output: ['apple']
print(add_fruit("orange"))  # Output: ['apple', 'orange']
print(add_fruit("banana"))  # Output: ['apple', 'orange', 'banana']

Output

def add_fruit(fruit, basket=None):
    if basket is None:
        basket = []
    basket.append(fruit)
    return basket

# Example calls
print(add_fruit("apple"))   # Output: ['apple']
print(add_fruit("orange"))  # Output: ['orange']
print(add_fruit("banana"))  # Output: ['banana']

Conclusion

In conclusion, a Python function is like a mini-program within your code that performs a specific task when called. It takes input values, processes them, and can return a result. Functions are handy for organizing code, making it more readable, and allowing you to reuse the same functionality in different parts of your program.

They can have default values for some or all of their parameters, making them flexible and easy to use. Understanding how to define and call functions is a fundamental skill in Python programming that helps make your code efficient and modular.

Default arguments in Python- FAQs

Q1. What are the default arguments in Python?

Ans. Default arguments are the values that are given while defining functions.

Q2. What is the default input in Python?

Ans. The input will return the string.

Q3. What are the 4 types of arguments in Python?

Ans. Default arguments, Keyword arguments (named arguments), Positional arguments and
Arbitrary arguments are the 4 types of argument in Python.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment