Thursday, 24 September 2026

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

 


Code Explanation:

1. Import partial
from functools import partial
functools is a built-in Python module.
partial is a function available inside functools.
It is used to fix some arguments of an existing function and create a new function.

2. Define the calc() Function
def calc(a, b, c):

Here, we create a function named calc.

It accepts three parameters:

a
b
c

For example:

calc(2, 4, 5)

means:

a = 2
b = 4
c = 5

3. Perform the Calculation
return a * b + c

The function calculates:

a × b + c

For example:

2 × 4 + 5
= 8 + 5
= 13

4. Create a Partial Function
f = partial(calc, 2, c=5)

⭐ This is the most important line.

partial() creates a new function from calc() while fixing some arguments.

Here:

a = 2
c = 5

are fixed.

Only b needs to be supplied later.

So we can think of f as:

f(b) → calc(2, b, 5)

5. First Function Call
x = f(4)

The 4 becomes the remaining parameter b.

Therefore:

a = 2
b = 4
c = 5

Now calculate:

2 × 4 + 5
= 8 + 5
= 13

Therefore:

x = 13

6. Second Function Call
y = f(10)

Again, a and c are already fixed.

a = 2
b = 10
c = 5

Calculation:

2 × 10 + 5
= 20 + 5
= 25

Therefore:

y = 25

7. Print the Values
print(x, y)

At this point:

x = 13
y = 25

So the output is:

13 25

๐Ÿ”„ Internal Working

This:

f = partial(calc, 2, c=5)

can conceptually be understood as:

Original:
calc(a, b, c)

Fixed:
a = 2
c = 5

Remaining:
b

Therefore:

f(4)
   ↓
calc(2, 4, 5)
   ↓
13

and:

f(10)
   ↓
calc(2, 10, 5)
   ↓
25

๐Ÿ“Š Execution Table
Expression a b c Result
f(4) 2 4 5 13
f(10) 2 10 5 25

๐Ÿง  Key Concept

partial() pre-fills arguments of a function.

Instead of repeatedly writing:

calc(2, 4, 5)
calc(2, 10, 5)

we fix the common arguments once:

f = partial(calc, 2, c=5)

and then provide only the changing argument:

f(4)
f(10)

๐ŸŽฏ Final Output
13 25

400 Days Python Coding Challenges with Explanation


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (119) AI (345) Android (25) AngularJS (1) Api (7) Assembly Language (2) aws (31) Azure (12) BI (10) book (1) Books (359) Bootcamp (14) C (78) C# (12) C++ (83) cloud (1) Course (93) Coursera (305) Cybersecurity (36) data (10) Data Analysis (47) Data Analytics (31) data management (16) Data Science (434) Data Strucures (19) Deep Learning (222) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (9) Excel (24) Finance (13) flask (4) flutter (1) FPL (17) Generative AI (77) Git (13) Google (55) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (43) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (406) Meta (24) MICHIGAN (5) microsoft (13) Nvidia (8) Pandas (16) PHP (20) Projects (35) Python (1377) Python Coding Challenge (1251) Python Library (8) Python Mathematics (19) Python Mistakes (51) Python Pattern Challenge (12) Python Quiz (641) Python Tips (112) Questions (3) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (21) SQL (55) Udemy (22) UX Research (1) web application (11) Web development (9) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)