CS Pathfinder Logo CS Pathfinder

Unit 3 · Functions

Keyword Arguments

Keyword arguments let you pass values by name instead of by position. This makes function calls clearer, less error-prone, and more flexible. Learn how keyword arguments work, how they differ from positional arguments, and the rules for mixing both.

Introduction

So far, you have been passing arguments based on position—the first argument goes to the first parameter, the second to the second, and so on. While this works, it can become confusing when a function has many parameters.

Keyword arguments solve this problem by letting you explicitly specify which parameter each argument belongs to, using the parameter's name. This makes the code self-documenting and eliminates errors caused by wrong argument order.

Keyword arguments are one of the most elegant features of Python and are widely used in real-world code, especially when calling functions with many optional parameters.

University Definition

Keyword arguments are arguments passed to a function by explicitly specifying the parameter name along with the value, using the syntax parameter_name=value. They allow arguments to be passed in any order and make function calls more readable.

Syntax of Keyword Arguments

def greet(first_name, last_name):
    print("Hello,", first_name, last_name)

# Positional arguments
greet("John", "Doe")

# Keyword arguments (order does not matter)
greet(last_name="Doe", first_name="John")
Output:
Hello, John Doe
Hello, John Doe

Both calls produce the same result. The keyword argument version clearly shows which value goes to which parameter, regardless of order.

Positional vs Keyword Arguments

Positional Arguments

def add(a, b):
    print(a + b)

add(10, 20)
  • Matched by position (order matters).
  • Shorter syntax.
  • Can be error-prone with many parameters.

Keyword Arguments

def add(a, b):
    print(a + b)

add(b=20, a=10)
  • Matched by name (order does not matter).
  • More readable and self-documenting.
  • Less prone to ordering mistakes.

Mixing Positional and Keyword Arguments

You can mix both types in a single function call, but there is an important rule:

Rule: Positional arguments must come before keyword arguments. You cannot have a positional argument after a keyword argument.

def display_info(name, age, city):
    print(name, "is", age, "years old from", city)

# Valid: positional then keyword
display_info("Alice", 21, city="Mumbai")

# Valid: all keyword
display_info(name="Bob", age=25, city="Delhi")

# INVALID: keyword before positional
# display_info(name="Charlie", 30, "Chennai")  # SyntaxError
Output:
Alice is 21 years old from Mumbai
Bob is 25 years old from Delhi

Advantages of Keyword Arguments

Readability: The code clearly shows what each argument represents.

Order flexibility: Arguments can be passed in any order.

Fewer errors: Eliminates mistakes caused by wrong argument positions.

Works with defaults: You can skip optional parameters by name.

Real-Life Analogy

Imagine filling out a bank deposit form. The form has fields for Account Number, Amount, and Date. If you write the values in the wrong boxes, the deposit goes to the wrong account. But if the form asks you to label each value (Account Number = 12345, Amount = 5000), mistakes are much less likely—this is exactly what keyword arguments do.

Practical Example — Functions with Many Parameters

Keyword arguments are especially useful when a function has many parameters:

def create_profile(name, age, city, country, hobby):
    print(f"Name: {name}")
    print(f"Age: {age}")
    print(f"City: {city}")
    print(f"Country: {country}")
    print(f"Hobby: {hobby}")

# Using keyword arguments — order doesn't matter
create_profile(
    hobby="Reading",
    name="Priya",
    country="India",
    age=22,
    city="Pune"
)
Output:
Name: Priya
Age: 22
City: Pune
Country: India
Hobby: Reading

Common Mistakes

Mistake 1 — Positional After Keyword

def greet(name, age):
    print(name, age)

greet(name="Alice", 21)  # SyntaxError: positional argument follows keyword

Mistake 2 — Duplicate Keyword Arguments

def greet(name, age):
    print(name, age)

greet(name="Alice", name="Bob", age=21)  # TypeError: duplicate keyword

University Exam Tip

University exams frequently ask: "What are keyword arguments? How are they different from positional arguments?" Always mention that keyword arguments are matched by name, allow any order, and must come after positional arguments in the same call.

Practice Questions

Q1. What are keyword arguments?

Answer: Keyword arguments are arguments passed to a function by specifying the parameter name and value, like name="Alice". They can be passed in any order.

Q2. Is the following call valid? func(b=2, 3)

Answer: No. Positional arguments cannot follow keyword arguments. This raises a SyntaxError.

Q3. Write a function power(base, exp) and call it using keyword arguments.

def power(base, exp):
    return base ** exp

print(power(base=2, exp=10))   # 1024
print(power(exp=3, base=5))    # 125

Q4. What is the advantage of keyword arguments over positional arguments?

Answer: Keyword arguments improve readability, eliminate order-based errors, and allow skipping optional parameters.

Q5. What will be the output?

def show(a, b, c):
    print(a, b, c)

show(1, c=3, b=2)
Output: 1 2 3

Summary

  • Keyword arguments use name=value syntax.
  • They can be passed in any order.
  • Positional arguments must come before keyword arguments.
  • Keyword arguments make code more readable and less error-prone.
  • Duplicate keyword arguments are not allowed.

Python Programming Handwritten Notes

Master Python Programming with Easy Handwritten Notes – Perfect for Interviews, Placements, GATE & Exams.