01 / 20
( )

Positional vs.
Keyword Arguments

Master the two fundamental ways to pass data into Python functions: by position and by name.

Python

What Is a Function Argument?

When you call a function, you supply values inside the parentheses. Those values are called arguments. The function's definition receives them through parameters: the variable names listed in its signature.

def greet(name, greeting): print(greeting, name) # Calling with two arguments greet("Alice", "Hello") # Output: Hello Alice

Two Ways to Pass Arguments

Positional
Matched to parameters by order. The first argument fills the first parameter, the second fills the second, and so on.
Keyword
Matched to parameters by name. You write parameter=value so order does not matter.

Why Does It Matter?

Choosing the right argument style affects readability, flexibility, and correctness. A wrong order in positional arguments silently produces bugs. Keyword arguments make intent explicit and let you skip optional parameters.

Readability
Keyword args act as inline documentation at the call site.
Safety
Named args prevent silent swaps when two params share a type.
Flexibility
Keyword args let you skip defaults and supply only what you need.

The Core Rule

def connect(host, port, timeout, use_ssl) connect("db.io", 5432, timeout=30, use_ssl=True) positional keyword

Positional Arguments in Detail

def power(base, exponent): return base ** exponent power(2, 10) # base=2, exponent=10 → 1024 power(10, 2) # base=10, exponent=2 → 100

Positional arguments are concise and work well when a function has few parameters whose purpose is obvious from context.

Keyword Arguments in Detail

def power(base, exponent): return base ** exponent # These two calls produce the same result: power(base=2, exponent=10) # → 1024 power(exponent=10, base=2) # → 1024

Positional vs. Keyword: Side by Side

Positional Keyword
Matching By order By name
Order sensitive? Yes No
Readability Shorter calls Self-documenting
Best for 1-2 obvious params Many or confusable params
Can skip defaults? No Yes

Default Values & Optional Args

def connect(host, port=5432, timeout=30, use_ssl=False): # ... # Only override what you need: connect("db.io", use_ssl=True) # host="db.io", port=5432, timeout=30, use_ssl=True

Mixing Positional and Keyword

1
Positional args first. Supply the obvious, required values by position.
2
Keyword args second. Name the optional or confusable values explicitly.
3
Never go back. Once you use a keyword arg, every subsequent argument must also be keyword.
# ✓ Valid: positional, then keyword connect("db.io", 5432, use_ssl=True) # ✕ Invalid: keyword before positional connect(host="db.io", 5432) # SyntaxError!

How to Apply: Step by Step

1
Read the function signature. Identify required params (no default) and optional params (have a default).
2
Supply required values positionally if their meaning is obvious (e.g., a single filename).
3
Use keyword syntax for any param where order could cause confusion or you want to skip defaults.
4
Verify: all positional args come first, and the total count matches what the function expects.

Example 1: Simple Function

def introduce(name, age, city): print(f"{name}, age {age}, from {city}") # All positional: introduce("Alice", 17, "Portland") # All keyword (any order): introduce(city="Portland", name="Alice", age=17) # Mixed: introduce("Alice", age=17, city="Portland")

Example 2: Skipping Defaults

def send_email(to, subject, body="", cc=None, urgent=False): # ... # Skip body and cc, only set urgent: send_email("bob@x.com", "Hi", urgent=True) # to="bob@x.com", subject="Hi", body="", cc=None, urgent=True

Example 3: Avoiding Silent Bugs

def create_user(username, email, role="viewer"): # ... # Bug: accidentally swapped username and email create_user("alice@x.com", "alice") # username="alice@x.com", email="alice" — no error, wrong data! # Fix: use keywords for clarity create_user(username="alice", email="alice@x.com")

Common Mistakes

✕ Keyword Before Positional
Writing func(a=1, 2) causes a SyntaxError. Positional args must come first.
✕ Duplicate Argument
Writing func(1, a=2) when the first positional already fills a. Python raises a TypeError.
✕ Wrong Param Name
Using a keyword that doesn't match any parameter: func(naem="x"). Typos cause unexpected TypeError.

Summary Table

Feature Positional Keyword Mixed
Syntax func(val1, val2) func(p=val) func(val, p=val)
Order Strict Flexible Pos first, kw after
Can skip defaults? No Yes Yes
Readability Low (many args) High Balanced
Error risk Silent swap Typo in name Both possible

Question 1 of 5

Given def area(length, width), which call correctly uses keyword arguments to compute area(5, 3)?

Question 2 of 5

What happens when you run greet(greeting="Hi", "Sam")?

Question 3 of 5

Given def log(msg, level="INFO", timestamp=True), which call sets timestamp to False without changing level?

Question 4 of 5

What error does power(2, base=3) produce if def power(base, exponent)?

Question 5 of 5

Which statement about positional arguments is FALSE?