Always free. For everyone.
Course outline
Module 04 of 20 · Foundations

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 Lab
LESSON 1 OF 3

Put a calculation in a function

20 minEnergy
WHAT YOU WILL DO

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

example.pyPython 3
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")
Keep this in mind

Write a function when one clear calculation needs to work for different inputs.

LESSON 2 OF 3

Turn a list into an answer

20 minHealthcare
WHAT YOU WILL DO

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

example.pyPython 3
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([]))
Keep this in mind

Give empty input a clear meaning instead of letting it break your calculation.

LESSON 3 OF 3

Combine small functions

20 minFinance
WHAT YOU WILL DO

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

example.pyPython 3
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))
Keep this in mind

Compose small functions so each calculation has one clear home.

ABOUT THE LEARNING LAB

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.

Course outline · Project lab · Visit engineering-fit.org