🐍⚙️Python Functions Explained in Simple Way



Python Functions tutorial explaining function declaration, positional arguments, default arguments, args and kwargs with examples.



A Function is a reusable block of code that performs a specific task. Instead of writing the same code again and again, we create functions.


✅ Function Declaration

Functions are created using the def keyword.

def greet():
    print("Hello Python")

greet()

👉 Used to organize programs and reuse logic.


✅ Types of Python Functions

1️⃣ Positional Arguments

Values are passed based on their position.

def add(a, b):
    print(a + b)

add(5, 3)

2️⃣ Default Arguments

A parameter can have a default value. Default parameters must be placed on the right side.

def add_numbers(a, b=8):
    return a + b

print(add_numbers(5))

3️⃣ Keyword Arguments

Arguments are passed using parameter names.

def student(name, age):
    print(name, age)

student(age=20, name="John")

4️⃣ Arbitrary Positional Arguments (*args)

Allows function to accept any number of values. Values are stored as a tuple.

def show_numbers(*args):
    print(args)

show_numbers(1,2,3,4)

5️⃣ Arbitrary Keyword Arguments (**kwargs)

Accepts unlimited keyword arguments. Stored as a dictionary.

def show_details(**kwargs):
    print(kwargs)

show_details(name="John", age=30)

📌 Practical Examples

✔ Remove Duplicates from List

list_1 = [10,20,20,10,40,50]

def remove_duplicates(list):
    unique_list = []
    for item in list:
        if item not in unique_list:
            unique_list.append(item)
    return unique_list

print(remove_duplicates(list_1))

✔ Factorial Using While Loop

def factorial(n):
    if n == 0:
        return 1

    fact = 1
    while n != 1:
        fact *= n
        n -= 1
    return fact

print(factorial(4))

✔ Factorial Using For Loop

def factorial(n):
    if n == 0:
        return 1

    result = 1
    for i in range(1, n+1):
        result *= i
    return result

print(factorial(3))

✔ Power Function (Default Argument)

def calculate_val(base, exponent=2):
    return base ** exponent

print(calculate_val(2,3))
print(calculate_val(4))

✔ Net Salary Calculation

def net_salary(base_salary, bonus=0, tax_rate=0.1):
    gross = base_salary + bonus
    net = gross - (gross * tax_rate)
    return net

print(net_salary(50000,10000,0.1))
print(net_salary(base_salary=50000,bonus=20000,tax_rate=0.1))

✔ Multiply Any Numbers (*args)

def multiply(*args):
    value = 1
    for i in args:
        value *= i
    return value

print(multiply(1,4,5,6))
print(multiply())

✔ Maximum Number Without Built-in Function

def maximum_number(*args):
    if not args:
        return "No numbers"

    max_value = args[0]
    for i in args:
        if i > max_value:
            max_value = i
    return max_value

print(maximum_number(4,12,5,9))

✔ Dictionary Keys & Values

my_dict = {
    "name":"Alice",
    "age":30,
    "city":"New York"
}

for key in my_dict.keys():
    print(key)

for value in my_dict.values():
    print(value)

for key,value in my_dict.items():
    print(key,value)

✔ Student Management System

def add_student(**kwargs):
    students = {}
    for key,value in kwargs.items():
        students[key] = value
    return students

def display_student(students):
    for key,value in students.items():
        print(f"Student {key}: {value}")

students = add_student(
    s001="Amal",
    s002="Vimal",
    s003="Kamal",
    s004="Nimal",
    s005="Sunil"
)

display_student(students)

🎯 Functions help developers write clean, reusable, and maintainable code 🚀.

📚 Related Articles

Article No Article Title & Link
1 🐍 Getting Started with Python for Data Science – Fundamentals Guide 🚀
2 🐍 Python Collections (Arrays) Simplified: List, Tuple, Set, & Dictionary 🐍
3 🐍 Understanding Python Lists in Simple Way 📋
4 🐍 Understanding Python Tuples in Simple Way 📦
5 🐍 Understanding Python Sets in Simple Way 🎴
6 🐍 Understanding Python Range & Dictionaries 📘
7 🐍Python Operators Guide: Arithmetic, Logical, & Precedence Explained 🐍
8 🐍 Python Control Statements Complete Guide 🔄
9 🐍 Python Functions Explained in Simple Way ⚙️
10 🐍 Python Map, Filter, Lambda & Modules – Simple Notes
11 🐍 Python OOP Concepts – Simple Short Notes
12 🐍  NumPy, Pandas & Web Scraping – Simple Python Notes📊

Post a Comment

Previous Post Next Post