Python Arguments
Table of Contents
Description
Function arguments are the values passed to a function when calling it. Python supports several types of arguments:
Positional Arguments
Keyword Arguments
Default Arguments
Variable-length Arguments (*args, **kwargs)
These allow functions to be flexible, readable, and reusable in different contexts.
Prerequisites
- Understanding of basic functions
- Familiarity with variables, data types
- Knowledge of function calling conventions
Examples
Here's a simple program in Python:
✅Positional Arguments
def greet(name, age):
print(f"Hello {name}, you are {age} years old.")
greet("Basha", 20) # Positional order matters
✅ Keyword Arguments
greet(age=20, name="Basha") # Arguments passed with keywords (order doesn't matter)
✅ Default Arguments
def greet(name="Guest"):
print(f"Welcome, {name}!")
greet("Ram") # Output: Welcome, Ram!
greet() # Output: Welcome, Guest!
✅ Variable-length Arguments - *args
def total_marks(*marks):
# *args collects all positional arguments as a tuple
print("Marks received:", marks)
print("Total:", sum(marks))
total_marks(90, 85, 78, 92)
✅ Variable-length Keyword Arguments - **kwargs
def student_info(**kwargs):
# **kwargs collects all keyword arguments as a dictionary
for key, value in kwargs.items():
print(f"{key} : {value}")
student_info(name="Basha", age=20, course="AI")
Real-World Applications
APIs use keyword arguments to receive flexible input
Data pipelines process dynamic sets of features using *args
User interfaces allow optional settings using default arguments
Logging/debugging systems use **kwargs to collect contextual data
Where topic Can Be Applied
Web development: Routes with optional query parameters
Data Science: Functions processing variable datasets
Machine Learning: Customizable training functions
System utilities: Scripts taking user-defined parameters
Frameworks: Extending functionality using flexible inputs
Resources
Topic video source
A comprehensive video
Watch
Python pdf
pdf on topic
Visit
Interview Questions
What are the types of arguments supported in Python?
What is the difference between *args and **kwargs?
Can you use both positional and keyword arguments in the same function?
What happens if you pass more positional arguments than defined?
What is the order of arguments in a function definition?