Labs ICT
๐ŸŽ“ SIWES/IT Login

Function Arguments

1 min read | Python Tutorial
๐ŸŽ“

Doing SIWES / IT attachment?

Get a structured training programme, a supervised digital logbook, and a completion certificate with LabsICT SIWES / IT.

Explore SIWES / IT Training

Function Arguments

Python lets you pass arguments in several flexible ways.

Positional & Keyword Arguments

Positional args are matched by order; keyword args are matched by name.

def describe_pet(animal, name):
    print(f"I have a {animal} named {name}.")

describe_pet("dog", "Rex")
describe_pet(name="Whiskers", animal="cat")

Default Arguments

Assign a default value so the argument becomes optional.

def greet(name, greeting="Hello"):
    print(f"{greeting}, {name}!")

greet("Alice")
greet("Bob", "Hi")

*args and **kwargs

*args captures extra positional args as a tuple. **kwargs captures extra keyword args as a dict.

def print_args(*args, **kwargs):
    print("Positional:", args)
    print("Keyword:", kwargs)

print_args(1, 2, 3, name="Alice", age=30)

๐Ÿงช Quick Quiz

What does *args allow in a function?