Free Python Course | Programming Tutorials
599 subscribers
9 photos
2 files
6 links
Join Noob to Pro in Python in 100 Days! 🚀 Learn Python with daily lessons, practical examples, free code samples, exercises, and homework. Ideal for beginners and those looking to advance their skills. Start your journey to becoming a Python pro! 🐍
Download Telegram
The map() Function

What is
map()?
The map() function applies a given function to all items in an input iterable (like a list) and returns an iterator. This is useful for transforming the elements of an iterable in a single line.

Syntax
map(function, iterable, ...)


Example: Using map() to Square Numbers
def square(x):
return x * x

numbers = [1, 2, 3, 4, 5]
squared_numbers = map(square, numbers)

print(list(squared_numbers)) # Convert the map object to a list


Output:
[1, 4, 9, 16, 25]

In this example, the map() function applies the square() function to each element of the numbers list.

Example: Using lambda with map()
numbers = [1, 2, 3, 4, 5]
squared_numbers = map(lambda x: x ** 2, numbers)

print(list(squared_numbers))


Output:
[1, 4, 9, 16, 25]

You can use a lambda function to achieve the same result in a more concise way.
The filter() Function

What is
filter()?
The filter() function filters elements from an iterable based on a condition defined by a function. It returns only the elements for which the function returns True.

Syntax:
filter(function, iterable)


Example: Using filter() to Filter Even Numbers
def is_even(x):
return x % 2 == 0

numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(is_even, numbers)

print(list(even_numbers)) # Convert the filter object to a list


Output:
[2, 4, 6]

In this example, the filter() function keeps only the even numbers from the list.

Example: Using lambda with filter()
numbers = [1, 2, 3, 4, 5, 6]
even_numbers = filter(lambda x: x % 2 == 0, numbers)

print(list(even_numbers))


Output:
[2, 4, 6]


Again, using a lambda function simplifies the code.
The reduce() Function

What is r
educe()?
The reduce() function from the functools module applies a function cumulatively to the items of an iterable, reducing the iterable to a single value.

Syntax
reduce(function, iterable)

To use reduce(), you need to import it from the functools module.

Example: Using reduce() to Sum a List
from functools import reduce

def add(x, y):
return x + y

numbers = [1, 2, 3, 4, 5]
total_sum = reduce(add, numbers)

print(total_sum)


Output:
15

In this example, reduce() adds each number in the list to the next, resulting in the sum of all numbers.

Example: Using lambda with reduce()
from functools import reduce

numbers = [1, 2, 3, 4, 5]
total_sum = reduce(lambda x, y: x + y, numbers)

print(total_sum)


Output:
15

Using lambda with reduce() simplifies the function definition.
👍31
New users check pinned message 📌
👍6
Creating 100 days perfect plan that's why didn't post anything today🙂
6👍5🔥1
pythoncourse.pdf
433.5 KB
🔥41
Are you dead?
👎27👨‍💻11👍86😁5💯2🗿2🤗1
If you are alive the comment your name👇
👍2713🔥3