Reusing Logic Clearly and Efficiently¶

Functions turn repeated steps into reusable tools¶
Loops taught Python how to repeat logic. Functions teach Python how to package that logic behind a clear name, with inputs and outputs, so the same idea can be reused across reports, dashboards, models, and business rules.
Why this matters¶
A growing project becomes easier to manage when repeated logic is turned into reusable building blocks. In business and ML work, functions help you:
calculate the same KPI for many teams without rewriting the formula
package a scoring rule so it can be tested and reused
separate data cleaning, transformation, and reporting into understandable steps
prepare code so it can later move into modules and packages
Continuity from the loops notebook
Loops help you repeat work across many values. Functions help you name that repeated work so it can be called whenever needed.
Core Explanation¶
A function is a named block of code that performs one clear task. Instead of rewriting the same steps every time, you define the logic once and call it whenever you need it.
Why return matters
A function becomes much more useful when it returns a value instead of only printing one. Returned values can be stored, compared, reused in later calculations, or passed into another function.
Use a named function when the logic deserves a clear label, may be reused often, or needs more than one line to stay readable.
def discount_price(price, rate):
return price * (1 - rate)Use a lambda for very small, short-lived expressions, especially when passing a tiny transformation into another function.
double = lambda value: value * 2If the logic starts needing explanations, conditions, or multiple steps, switch back to a named function.
Visual intuition: calling a function many times¶
A loop can call the same function for many records. That is the bridge between repeated processing and reusable logic.
def greet_user(name):
return f"Welcome, {name}."
print(greet_user("Business Python Learner"))Welcome, Business Python Learner.
Worked Example: turning a formula into a reusable helper¶
Suppose a company wants the same profit calculation to work for many product lines. A function makes the rule reusable and easy to test.
What to notice
the function name explains the task
revenueandcostare parametersthe loop reuses the same function across multiple records
the returned values could later feed a dashboard or report
Small example: lambda for a tiny transformation¶
Guided Practice¶
What is the main purpose of a function parameter?¶
When is a lambda usually appropriate?¶
Exercises¶
Exercise 1: Total invoice¶
Hint
Multiply price by quantity and return the result instead of printing inside the function.
Exercise 2: Pass or review¶
Hint
You can return the Boolean expression directly: return score >= 50.
Exercise 3: Tiny lambda helper¶
Hint
A lambda takes inputs like a function header, followed by a colon and the expression to return.
Quick Summary
functions package reusable logic behind a meaningful name
parameters let the same logic work with different inputs
returnmakes a result available to other codelambdas are useful for short expressions but should not replace clear function names when logic grows
The next notebook shows how reusable logic moves beyond one file and into modules and packages.
double = lambda x: x * 2
print(double(6))12
Practice Lab¶
Expected output
Conversion Rate: 4.8%Expected output
24
36
50Extension idea
Try rewriting the lambda as a named function called double_value. Which version would be clearer in a larger project?
Key Takeaway¶
Functions are the first major tool for turning a script into a maintainable program. Once logic has a name, inputs, and outputs, it becomes much easier to reuse, test, and eventually move into separate modules.