Write reusable solutions
Build small functions that turn clear inputs into useful outputs. This module has 3 lessons and takes about 1h. Each lesson has a practice exercise that runs real Python in your browser and a short quiz.
Start module 04 in the Learning LabPut a calculation in a function
Reuse one energy formula for several devices.
def introduces a function. Names inside the brackets are parameters: the inputs the function needs. The indented body describes the work.
return sends a result back to the caller. print only displays a value. Return calculations when another part of your program needs to use the answer.
This fictional workshop compares two appliances. A function lets us use one formula for both, reducing repeated code and unit mistakes.
See it in action
def energy_kwh(power_w, hours):
return power_w * hours / 1000
# Synthetic appliance use
fan_kwh = energy_kwh(80, 5)
light_kwh = energy_kwh(40, 6)
print("Total:", fan_kwh + light_kwh, "kWh")
Write a function when one clear calculation needs to work for different inputs.
Turn a list into an answer
Build a reusable summary for fictional clinic waiting times.
A function can accept a list and return a dictionary. This makes both the inputs and outputs clear. Variables created inside the function are local to that call.
An empty list needs a deliberate rule. Here, an empty list returns a count of zero and a mean of None. None means that no mean is available.
We use synthetic waiting times with no patient details. A useful summary shows the count alongside the mean so the reader knows how much data supports it.
See it in action
def waiting_summary(minutes):
if len(minutes) == 0:
return {"count": 0, "mean": None}
return {"count": len(minutes), "mean": sum(minutes) / len(minutes)}
# Synthetic queue data
print(waiting_summary([12, 18, 15]))
print(waiting_summary([]))
Give empty input a clear meaning instead of letting it break your calculation.
Combine small functions
Separate a budget rule from the report that uses it.
One function can call another. Keep each function focused on one job so you can change or check that job by itself.
A default parameter provides a value when the caller leaves it out. For example, reserve_rate=0.10 gives a default 10% reserve.
Our fictional project calculator uses one function for total cost and another for the budget result. Changing the reserve should change the calculation in only one place.
See it in action
def with_reserve(base_cost, reserve_rate=0.10):
return base_cost * (1 + reserve_rate)
def budget_result(base_cost, budget, reserve_rate=0.10):
total = with_reserve(base_cost, reserve_rate)
return {"total": total, "remaining": budget - total}
# Synthetic student project values in NGN
print(budget_result(20000, 25000))
print(budget_result(20000, 25000, 0.20))
Compose small functions so each calculation has one clear home.
Free, open and run by a nonprofit.
This page is part of Python to AI, a free course of 20 modules and 60 lessons in the engineering.fit( ) Learning Lab. The Learning Lab is built and run by engineering.fit( ), a nonprofit founded by Abdullateef Opeyemi Bakare that helps African engineering students gain research experience. You do not need an account or pay a fee to learn.