- Home
- /
- Python Function Arguments
Python Tutorial
Introduction
Python Data Types & Oper...
Python Strings
Python Lists
Python Tuples
Python Sets
Python Dictionaries
Conditional Statements
Python Loops
Python Functions
Python Modules
Python OOPS
Advanced Topics
File Handling
- Home
- /
- Python Function Arguments
Function Arguments in Python
In Python, when you define a function, you can control how you pass data into it using different types of arguments. This flexibility makes functions powerful and reusable.
There are four types of arguments we can use:
Default Arguments
Keyword Arguments
Required (Positional) Arguments
Variable-length Arguments
1. Built-in Functions
Default arguments allow you to assign a default value to a parameter. If the caller doesn’t provide that argument, the function uses the default.
def greet(first, middle="Ali", last="Khan"):
print("Welcome,", first, middle, last)
greet("Hamza")
Output:
Welcome, Hamza Ali Khan
If the caller provides values for all parameters, those values override the defaults.
2. Keyword Arguments
With keyword arguments, you can pass values by explicitly specifying which parameter they belong to. This way, the order doesn’t matter.
def greet(first, middle, last):
print("Welcome,", first, middle, last)
greet(middle="Tariq", last="Sheikh", first="Zain")
Output:
Welcome, Zain Tariq Sheikh
This approach helps make code more readable and avoids mistakes due to ordering.
3. Required (Positional) Arguments
If you don’t use keywords, then the values must match the function’s parameter order exactly. Also, the number of arguments must be correct.
Example: Too few arguments
def greet(first, middle, last):
print("Welcome,", first, middle, last)
greet("Sara", "Imran")
Output:
TypeError: greet() missing 1 required positional argument: 'last'
Example: Correct number of arguments in correct order
def greet(first, middle, last):
print("Welcome,", first, middle, last)
greet("Sara", "Imran", "Malik")
Output:
Welcome, Sara Imran Malik
4. Variable-length Arguments
Sometimes we don’t know how many arguments will be passed. Python gives us two ways to handle such cases:
a. Arbitrary Positional Arguments (*args)
Using *args
, the function collects extra values into a tuple.
def show_courses(*courses):
print("You have enrolled in:", courses[0], "and", courses[1])
show_courses("HTML", "CSS", "JavaScript")
Output:
You have enrolled in: HTML and CSS
You can access any number of arguments using indexing or loops.
b. Arbitrary Keyword Arguments (**kwargs)
Using **kwargs
, the function collects named arguments into a dictionary.
def show_student(**student):
print("Student:", student["name"], "| Class:", student["class"], "| ID:", student["id"])
show_student(class="10th", name="Ammar", id="S1024")
Output:
Student: Ammar | Class: 10th | ID: S1024
This is helpful when you want to pass named data that might vary.
Summary
Functions in Python allow you to pass arguments in different ways. Whether you’re using defaults for flexibility, keywords for clarity, or variable-length arguments for handling dynamic inputs, each type of argument lets you build smarter and cleaner code.