PythonArguments and Parameters

Arguments and Parameters in Python

Functions often need input values to perform a task. These inputs are called:

  • Parameters → the names listed in the function definition.
  • Arguments → the actual values passed when calling the function.

Understanding how arguments work is key to writing flexible and reusable functions.


Defining Parameters

params_basic.py
def greet(name):
    print("Hello,", name)
 
greet("Alice")   # Argument "Alice" passed
output.txt
Hello, Alice

Explanation:

  • name → parameter.
  • "Alice" → argument.
  • When called, the argument is passed to the parameter.

Multiple Parameters

params_multiple.py
def add(a, b):
    print("Sum:", a + b)
 
add(3, 5)
output.txt
Sum: 8

Explanation:

  • You can define any number of parameters.
  • Arguments must match the parameter order.

Parameters of Different Data Types

params_types.py
def display(name, age, is_member):
    print("Name:", name)
    print("Age:", age)
    print("Member:", is_member)
 
display("John", 30, True)
output.txt
Name: John
Age: 30
Member: True

Explanation:

  • Parameters can be of any type: string, integer, boolean, float, list, etc.

List as Argument

params_list.py
def show_items(items):
    for item in items:
        print("Item:", item)
 
show_items(["apple", "banana", "cherry"])
output.txt
Item: apple
Item: banana
Item: cherry

Explanation:

  • You can pass lists and other collections as arguments.

Tuple as Argument

params_tuple.py
def print_coords(coords):
    x, y = coords
    print("X:", x)
    print("Y:", y)
 
print_coords((10, 20))
output.txt
X: 10
Y: 20

Explanation:

  • Tuples can be passed to functions and unpacked.

Dictionary as Argument

params_dict.py
def print_user(info):
    for key, value in info.items():
        print(f"{key}: {value}")
 
print_user({"name": "Emma", "age": 25, "city": "Paris"})
output.txt
name: Emma
age: 25
city: Paris

Explanation:

  • Dictionaries allow passing structured data to functions.

Default Parameter Values

params_default.py
def greet(name="Guest"):
    print("Hello,", name)
 
greet()
greet("Bob")
output.txt
Hello, Guest
Hello, Bob

Explanation:

  • If no argument is provided, the function uses the default value.

Keyword Arguments

params_keyword.py
def intro(name, age):
    print(f"{name} is {age} years old.")
 
intro(age=22, name="Alice")
output.txt
Alice is 22 years old.

Explanation:

  • Keyword arguments specify which value goes to which parameter.
  • Order no longer matters.

Variable-Length Arguments *args

params_args.py
def print_numbers(*args):
    for num in args:
        print(num)
 
print_numbers(1, 2, 3, 4, 5)
output.txt
1
2
3
4
5

Explanation:

  • *args collects any number of arguments into a tuple.
  • Allows flexible function calls.

Variable-Length Keyword Arguments **kwargs

params_kwargs.py
def print_info(**kwargs):
    for key, value in kwargs.items():
        print(f"{key}: {value}")
 
print_info(name="John", age=30, city="London")
output.txt
name: John
age: 30
city: London

Explanation:

  • **kwargs collects keyword arguments into a dictionary.

When to Use

ScenarioTechnique
Fixed number of inputsRegular parameters
Optional argumentsDefault parameters
Order doesn’t matterKeyword arguments
Unknown number of positional arguments*args
Unknown number of keyword arguments**kwargs

Summary

  • Functions can accept different kinds of arguments: positional, keyword, default, *args, and **kwargs.
  • You can pass any type of data: string, int, float, list, tuple, dictionary, etc.
  • These techniques give you flexibility and power when designing functions.

In the next section, we will explore Return Values — how functions can send data back to the caller.