Functions in Python

A function is a reusable block of code that performs a specific task. Python has built-in functions, user-defined functions, lambda functions, and more.


1️⃣ Defining a Function

A function is defined using the def keyword.

Example:

def greet():
    print("Hello, Welcome to Python!")

greet()  # Calling the function

Output:

Hello, Welcome to Python!

2️⃣ Function with Parameters and Return Value

Functions can take parameters (inputs) and return values.

Example:

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

result = add(5, 3)
print(result)  # Output: 8

3️⃣ Types of Functions in Python

Python provides various types of functions:

Function Type Description Example
Built-in Functions Predefined in Python print(), len(), max()
User-defined Functions Created by the user def my_function():
Lambda Functions Anonymous (one-liner) functions lambda x: x * 2
Recursive Functions A function that calls itself Factorial calculation
Higher-order Functions Functions that take other functions as arguments map(), filter(), reduce()

4️⃣ Built-in Functions

Python has many built-in functions, such as:

Function Purpose
len(seq) Returns length of a sequence
sum(iterable) Returns sum of elements
max(iterable) Returns max element
min(iterable) Returns min element
sorted(iterable) Returns sorted sequence
round(number, ndigits) Rounds a number
abs(x) Returns absolute value

Example:

numbers = [4, 1, 8, 3]
print(max(numbers))  # Output: 8
print(sum(numbers))  # Output: 16
print(sorted(numbers))  # Output: [1, 3, 4, 8]

5️⃣ Lambda Functions (Anonymous Functions)

A lambda function is a one-liner function without a name.

Example:

square = lambda x: x * x
print(square(5))  # Output: 25

Example with map() function:

numbers = [1, 2, 3, 4]
squared_numbers = list(map(lambda x: x * x, numbers))
print(squared_numbers)  # Output: [1, 4, 9, 16]

6️⃣ Recursive Functions

A recursive function calls itself to solve a problem.

Example (Factorial Calculation):

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

print(factorial(5))  # Output: 120

7️⃣ Higher-order Functions (map(), filter(), reduce())

Higher-order functions take other functions as arguments.

Using map() (applies a function to each element):

numbers = [1, 2, 3, 4]
squared = list(map(lambda x: x ** 2, numbers))
print(squared)  # Output: [1, 4, 9, 16]

Using filter() (filters elements based on condition):

numbers = [1, 2, 3, 4, 5, 6]
even_numbers = list(filter(lambda x: x % 2 == 0, numbers))
print(even_numbers)  # Output: [2, 4, 6]

Using reduce() (reduces a list to a single value):

from functools import reduce

numbers = [1, 2, 3, 4]
product = reduce(lambda x, y: x * y, numbers)
print(product)  # Output: 24

🔹 Summary Table

Function Type Description
Built-in Functions Predefined in Python (print(), max())
User-defined Functions Created using def
Lambda Functions One-liner anonymous functions
Recursive Functions A function that calls itself
Higher-order Functions Functions that accept other functions (map(), filter(), reduce())

Would you like more real-world examples? 🚀


Leave a Reply

Your email address will not be published. Required fields are marked *