Module: M1-R5: Information Technology Tools and Network Basics
Chapter: Ch1 Computer Intro
In Python, parameters are variables listed inside the parentheses in a function definition. They are used to pass data into a function when it is called.
These are the most common type of parameters. The values are assigned based on their position in the function call.
def add(a, b):
print("Sum:", a + b)
add(5, 10) # Output: Sum: 15
➡ Here, a=5 and b=10 are assigned by their position.
You can provide a default value to parameters. If no argument is passed, the default value will be used.
def greet(name="User"):
print("Hello,", name)
greet() # Output: Hello, User
greet("Sourav") # Output: Hello, Sourav
➡ Default parameters make arguments optional.
When calling a function, you can pass arguments by parameter name instead of position.
def student_info(name, age):
print("Name:", name)
print("Age:", age)
student_info(age=20, name="Sourav")
➡ Order doesn’t matter when using keyword arguments.
The *args parameter allows you to pass a variable number of arguments to a function.
It collects them into a tuple.
def total(*numbers):
print("Numbers:", numbers)
print("Sum:", sum(numbers))
total(10, 20, 30, 40)
➡ Output:
Numbers: (10, 20, 30, 40)
Sum: 100
The **kwargs parameter collects arguments as key-value pairs (dictionary format).
def details(**info):
for key, value in info.items():
print(key, ":", value)
details(name="Sourav", age=24, course="Python")
➡ Output:
name : Sourav
age : 24
course : Python
Python allows combining different types of parameters in a specific order:
def example(a, b=10, *args, **kwargs):
print("a =", a)
print("b =", b)
print("args =", args)
print("kwargs =", kwargs)
example(5, 20, 30, 40, x=100, y=200)
➡ Output:
a = 5
b = 20
args = (30, 40)
kwargs = {'x': 100, 'y': 200}
| Type | Symbol | Description | Stores As |
|---|---|---|---|
| Positional | — | Values passed by position | Individual variables |
| Default | — | Parameters with predefined values | Individual variables |
| Keyword | — | Values passed by parameter name | Individual variables |
| Variable-length | *args | Stores multiple values | Tuple |
| Keyword Variable-length | **kwargs | Stores multiple key-value pairs | Dictionary |