Friday, 3 July 2026

Python Coding challenge - Day 1189| What is the output of the following Python Code?

 


Code Explanation:

๐Ÿ”น 1. Creating a List
nums = [1, 2, 3, 4, 5]
✅ Explanation:

A list named nums is created.

Contents:

[1, 2, 3, 4, 5]

Current state:

nums
 ↓
[1, 2, 3, 4, 5]

๐Ÿ”น 2. Calling filter()
filter(lambda x: x % 2 == 0, nums)
✅ Explanation:

The filter() function checks every element of the list and keeps only those elements for which the condition returns True.

Syntax:

filter(function, iterable)

Here:

lambda x: x % 2 == 0

means:

Keep only even numbers.

๐Ÿ”น 3. Understanding the Lambda Function
lambda x: x % 2 == 0
✅ Explanation:

This lambda function checks whether a number is divisible by 2.

Equivalent code:

def check(x):
    return x % 2 == 0

Examples:

1 % 2 = 1 → False ❌

2 % 2 = 0 → True ✅

3 % 2 = 1 → False ❌

4 % 2 = 0 → True ✅

5 % 2 = 1 → False ❌

๐Ÿ”น 4. Result of filter()

Python checks every element one by one.

Number Condition Action
1 False Remove ❌
2 True Keep ✅
3 False Remove ❌
4 True Keep ✅
5 False Remove ❌

After filtering:

2
4

The filter object contains:

2, 4

๐Ÿ”น 5. Calling map()
map(
    lambda x: x * 10,
    filter(...)
)
✅ Explanation:

Now map() receives the filtered values:

2
4

Its job is to apply a function to every element.

Syntax:

map(function, iterable)

๐Ÿ”น 6. Understanding the Second Lambda
lambda x: x * 10
✅ Explanation:

This lambda multiplies every value by 10.

Equivalent function:

def multiply(x):
    return x * 10
๐Ÿ”น 7. First Iteration of map()

Current value:

x = 2

Calculation:

2 * 10

Result:

20

๐Ÿ”น 8. Second Iteration of map()

Current value:

x = 4

Calculation:

4 * 10

Result:

40

๐Ÿ”น 9. Result of map()

Generated values:

20
40

Internally:

map object

Not a list yet.

๐Ÿ”น 10. Converting to List
list(result)
✅ Explanation:

The map object is converted into a normal list.

Result:

[20, 40]

๐Ÿ”น 11. Printing the Output
print(list(result))
✅ Explanation:

Prints:

[20, 40]

๐ŸŽฏ Final Output
[20, 40]

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (300) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (30) Azure (12) BI (10) Books (268) Bootcamp (12) C (78) C# (12) C++ (83) cloud (1) Course (87) Coursera (300) Cybersecurity (32) data (7) Data Analysis (38) Data Analytics (26) data management (16) Data Science (380) Data Strucures (23) Deep Learning (187) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (21) Finance (10) flask (4) flutter (1) FPL (17) Generative AI (74) Git (12) Google (53) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (335) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (14) PHP (20) Projects (34) Python (1396) Python Coding Challenge (1179) Python Mathematics (2) Python Mistakes (51) Python Quiz (557) Python Tips (19) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (20) SQL (52) Udemy (18) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)