📦 What are Function Arguments in Python?
Arguments are the values passed into a function when it is called. Parameters act as variables inside the function definition to receive these values.
Python supports flexible parameter passing mechanisms including positional arguments, keyword arguments, default values, arbitrary positional arguments (*args), and arbitrary keyword arguments (**kwargs).
1. Positional Arguments
Values assigned to parameters strictly based on their positional order in the function call.
Python
def create_user_profile(username, role):
return f"User '{username}' registered with role '{role}'."
# Positional arguments match parameters in exact order
print(create_user_profile("nikita_dev", "Web Developer"))
Output:
User 'nikita_dev' registered with role 'Web Developer'.
Real-Time Example 1: Keyword Arguments
Pass arguments by explicitly naming parameters, allowing values to be supplied independently of order.
Python
def schedule_meeting(title, time, room):
return f"Meeting '{title}' scheduled in {room} at {time}."
# Passing arguments using keywords out of original order
print(schedule_meeting(room="Conference Hall B", time="10:30 AM", title="Sprint Review"))
Output:
Meeting 'Sprint Review' scheduled in Conference Hall B at 10:30 AM.
Real-Time Example 2: Default Parameter Values
Provide fallback values for parameters if no corresponding argument is passed during invocation.
Python
def connect_database(host="127.0.0.1", port=3306, user="root"):
return f"Connecting to MySQL server at {host}:{port} as user '{user}'..."
# Using default port and user values
print(connect_database("192.168.1.50"))
# Overriding default values
print(connect_database("localhost", port=5432, user="admin"))
Output:
Connecting to MySQL server at 192.168.1.50:3306 as user 'root'...
Connecting to MySQL server at localhost:5432 as user 'admin'...
Real-Time Example 3: Arbitrary Positional Arguments (*args)
Accept a variable number of positional arguments bundled into a tuple using
*args.
Python
def calculate_cart_total(*prices):
total = sum(prices)
return round(total, 2)
# Pass any number of product prices
print("Cart Total (2 items): ₹", calculate_cart_total(450, 1200))
print("Cart Total (5 items): ₹", calculate_cart_total(150, 800, 300, 1200, 50))
Output:
Cart Total (2 items): ₹ 1650
Cart Total (5 items): ₹ 2500
Real-Time Example 4: Arbitrary Keyword Arguments (**kwargs)
Accept any number of keyword parameters packaged into a dictionary using
**kwargs.
Python
def build_html_element(tag, **attributes):
attr_string = "".join(f' {key}="{value}"' for key, value in attributes.items())
return f"<{tag}{attr_string}>"
# Generate dynamic markup with variable attributes
print(build_html_element("input", type="text", name="username", placeholder="Enter username"))
print(build_html_element("img", src="banner.jpg", alt="Studio Banner", width="100%"))
Output:


Real-Time Example 5: Combining Standard, *args, and **kwargs
Enforce the correct parameter sequence: Standard arguments,
*args, and then **kwargs.
Python
def log_system_event(level, *messages, **metadata):
print(f"[{level.upper()}] Messages logged: {len(messages)}")
for msg in messages:
print(f" -> {msg}")
if metadata:
print(f" Metadata tags: {metadata}")
log_system_event(
"error",
"Database timeout occurred",
"Connection retry failed",
service="AuthService",
code=503
)
Output:
[ERROR] Messages logged: 2
-> Database timeout occurred
-> Connection retry failed
Metadata tags: {'service': 'AuthService', 'code': 503}
Real-Time Example 6: Sequence Unpacking (* operator)
Unpack list or tuple elements as individual positional arguments using the
* operator during function calls.
Python
def calculate_rectangle_area(length, width):
return length * width
# List of dimensions stored as data
dimensions = [15, 8]
# Unpack list into individual arguments using *
area = calculate_rectangle_area(*dimensions)
print(f"Calculated Rectangle Area: {area} sq. units")
Output:
Calculated Rectangle Area: 120 sq. units
Real-Time Example 7: Dictionary Unpacking (** operator)
Unpack dictionary keys and values as keyword arguments using the
** operator.
Python
def send_email_notification(recipient, subject, body):
return f"Email sent to {recipient} | Subject: '{subject}' | Body: {body}"
# Configuration dictionary
email_payload = {
"recipient": "client@infotech.com",
"subject": "Project Quotation",
"body": "Your web design package is ready for review."
}
# Unpack dictionary into keyword arguments
print(send_email_notification(**email_payload))
Output:
Email sent to client@infotech.com | Subject: 'Project Quotation' | Body: Your web design package is ready for review.
Real-Time Example 8: Keyword-Only Arguments
Enforce parameters after an asterisk
* to be passed exclusively as keyword arguments.
Python
def transfer_funds(sender_acc, receiver_acc, *, amount, currency="INR"):
return f"Transferred {amount} {currency} from {sender_acc} to {receiver_acc}."
# amount and currency MUST be passed as keyword arguments
print(transfer_funds("ACC1001", "ACC2002", amount=5000, currency="USD"))
Output:
Transferred 5000 USD from ACC1001 to ACC2002.
Real-Time Example 9: Positional-Only Arguments (Python 3.8+)
Restrict parameters before a slash
/ to be supplied strictly by position, not by keyword.
Python
def compute_percentage(part, total, /):
return round((part / total) * 100, 2)
# Correct positional call
print("Percentage Score:", compute_percentage(42, 50))
# Note: Calling compute_percentage(part=42, total=50) would raise a TypeError.
Output:
Percentage Score: 84.0
Summary of Python Arguments
- Positional & Keyword: Values mapped by exact order or explicit parameter names.
- Defaults & Unpacking: Fallback values simplify calls, while
*and**unpack lists and dictionaries dynamically. - Flexibility: Use
*argsand**kwargsto handle variable numbers of inputs robustly in reusable functions.