Course Content
How to Add a Virtual Environment in Python
0/1
Set
0/1
Magic Methods
0/1
Python
About Lesson

Python provides a rich set of built-in functions that perform common operations, such as:

  • print(), len(), sum(), max(), sorted(), etc.

These are functions defined by the user to perform specific tasks, as discussed above.

Lambda functions, also known as anonymous functions, are small one-liner functions that are defined using the lambda keyword. They can take any number of arguments, but only one expression.

Python
lambda arguments: expression

Example:

Python
multiply = lambda x, y: x * y
result = multiply(2, 3)
print(result)  # Output: 6

Variables defined inside a function are local to that function. They are not accessible from outside the function. Similarly, variables defined outside any function (global variables) can be accessed by any function unless shadowed by local variables.

Example:

Python
x = 10  # global variable

def my_function():
    x = 5  # local variable
    print(x)

my_function()  # Output: 5
print(x)  # Output: 10 (global x remains unchanged)

Local Scope: Variables defined inside a function are accessible only within that function.

Global Scope: Variables defined outside any function can be accessed globally.

Example:

Python
x = 20  # global variable

def test():
    global x  # Modify the global variable
    x = 30
    print(x)  # Output: 30

test()
print(x)  # Output: 30 (global variable x was modified)

A higher-order function is a function that takes one or more functions as arguments, returns a function, or both. Examples include map(), filter(), and reduce().

1. map() Function

The map() function applies a given function to all items in an iterable (like a list or a tuple) and returns a map object (an iterator). This is useful when you need to perform an operation on each element in an iterable and produce a new iterable with the results.

Syntax:

Python
map(function, iterable)
  • function: A function that takes an element from the iterable and performs some operation on it.
  • iterable: An iterable like a list, tuple, or string, to which the function will be applied.

The map() function returns an iterator, so you often need to convert the result to a list or another data structure if you want to see the results immediately.

Example:

Python
# Example: Squaring each number in a list
numbers = [1, 2, 3, 4, 5]
squared_numbers = map(lambda x: x ** 2, numbers)

# Convert the map object to a list to see the result
print(list(squared_numbers))  # Output: [1, 4, 9, 16, 25]

2. filter() Function

The filter() function filters elements from an iterable based on a condition. It constructs an iterator from those elements of the iterable for which the function returns True. It’s used when you want to extract elements that satisfy a certain condition.

Syntax:

Python
filter(function, iterable)
  • function: A function that tests each element in the iterable. It should return True or False (like a condition).
  • iterable: The iterable whose elements will be tested against the condition.

The filter() function returns an iterator, so again, you often need to convert it to a list or another collection type.

Example:

Python
numbers = [1, 2, 3, 4, 5, 6, 7, 8]
even_numbers = filter(lambda x: x % 2 == 0, numbers)

# Convert the filter object to a list to see the result
print(list(even_numbers))  # Output: [2, 4, 6, 8]

3. reduce() Function

The reduce() function is part of the functools module and is used to perform a cumulative operation on an iterable. It takes a binary function (a function that accepts two arguments) and applies it cumulatively to the elements of the iterable, so as to reduce the iterable to a single value.

Syntax:

Python
from functools import reduce
reduce(function, iterable[, initializer])
  • function: A function that takes two arguments and returns a single result. This function will be applied cumulatively to the elements of the iterable.
  • iterable: The iterable whose elements will be processed.
  • initializer (optional): An optional value that will be used as the initial value of the reduction. If provided, it will be passed as the first argument to the function during the first iteration.

The reduce() function returns a single result, which is the accumulated value after applying the function to all elements of the iterable.

Example:

Python
from functools import reduce

numbers = [1, 2, 3, 4]
product_result = reduce(lambda x, y: x * y, numbers)

print(product_result)  # Output: 24 (1 * 2 * 3 * 4)
  • Functions in Python are defined using the def keyword.
  • Functions can have parameters (positional, keyword, default, variable-length).
  • A function can return values using the return statement.
  • Variables have local or global scope.
  • Recursion is a powerful technique, but it must have a base case.
  • Closures allow inner functions to remember the environment where they were created.
  • Decorators are a way to modify or extend the behavior of functions.
  • Python supports higher-order functions like map(), filter(), and reduce().