What is Computer

Module: M1-R5: Information Technology Tools and Network Basics

Chapter: Ch1 Computer Intro

🔹 What are Function Parameters?

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.

🔸 Types of Function Parameters in Python
  1. Positional Parameters
  2. Default Parameters
  3. Keyword Parameters
  4. Variable-length Parameters (*args)
  5. Keyword Variable-length Parameters (**kwargs)
1️⃣ Positional Parameters

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.

2️⃣ Default Parameters

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.

3️⃣ Keyword Parameters

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.

4️⃣ Variable-length Parameters (*args)

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
5️⃣ Keyword Variable-length Parameters (**kwargs)

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
🔹 Mixing Different Types of Parameters

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}
✅ Summary Table
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
💡 Best Practices
  • Use meaningful parameter names.
  • Keep functions small and focused.
  • Use default and keyword parameters for flexibility.
  • Use *args and **kwargs for handling unknown arguments.
Quick Links