Python – Keyword Arguments

In Python, when you use a function, you can especially give information using “keywords” or “named arguments.” It’s like giving labels to the information you provide. Think of it as putting names on boxes to organize things better. In the function, these labels are like the names of the boxes. Let’s look into this article to learn more about Python Keyword Arguments

Hence, when you call the function, you can use these labels to say which information goes where. It’s a way to make your code clearer and to make sure each part of the function gets the right information.

What is Python Keyword Argument?

Keyword arguments in Python allow you to pass values to a function by explicitly specifying the parameter names. Each keyword argument consists of a parameter name, followed by the assignment operator = and the corresponding value.

However, this approach provides clarity in function calls, as it makes it evident which value is assigned to each parameter. The analogy to dictionaries is pertinent, as both involve associating values with specific keys (or keywords). This feature contributes to code readability and flexibility in function usage. The keyword Arguments will send arguments with the key=value syntax.

What is the Example of the Python Keyword Argument?

def print_person_info(name, age, city="Unknown", country="Unknown"):
    """
    A function that prints information about a person.
    """
    print(f"Name: {name}")
    print(f"Age: {age}")
    print(f"City: {city}")
    print(f"Country: {country}")
    print("\n")

# Using keyword arguments
print_person_info(name="John", age=25, city="New York", country="USA")
print_person_info(name="Alice", age=30, country="Canada")
print_person_info(name="Bob", age=22, city="London")

# Using a mix of positional and keyword arguments
print_person_info("Eva", 28, country="Germany")

Output

Name: John
Age: 25
City: New York
Country: USA

Name: Alice
Age: 30
City: Unknown
Country: Canada

Name: Bob
Age: 22
City: London
Country: Unknown

Name: Eva
Age: 28
City: Unknown
Country: Germany

Example 2: Using def division keyword

def division(dividend, divisor=1):
    """
    A function that performs division.
    """
    result = dividend / divisor
    return result

# Example calls
result1 = division(10, 2)
result2 = division(20)
result3 = division(30, 3)

# Displaying results
print("Result 1:", result1)  # Output: Result 1: 5.0
print("Result 2:", result2)  # Output: Result 2: 20.0
print("Result 3:", result3)  # Output: Result 3: 10.0

Output

Result 1: 5.0
Result 2: 20.0
Result 3: 10.0

Conclusion

In Python, keyword arguments provide a way to make functions more flexible and readable. When defining a function, we can assign default values to some parameters, turning them into keyword arguments. This means that when we call the function, we have the option to explicitly specify values for certain parameters by using their names.

Python – Keyword Arguments- FAQs

Q1. What are keyword arguments in Python?

Ans. The values that are passed into a function will be identifiable by specific parameter names.

Q2. What are keywords in Python?

Ans. Python keywords are reserved words that consist of specific meanings and purposes. It can’t be used for anything but those specific purposes.

Q3. How many keywords for Python?

Ans.36

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