Learn Without Walls
← Back to Python Practice Labs
Lab 4 of 10

Functions

Write once, use anywhere

← Lab 3: Loops Lab 4 of 10 Lab 5: Lists & Dicts →
⏳ Loading Python... (first load takes ~10 seconds)

📖 Concept Recap

👀 Worked Example

A BMI calculator with default parameters and multiple return values:

def calculate_bmi(weight_kg, height_m, unit="metric"): if unit == "imperial": weight_kg = weight_kg * 0.453592 height_m = height_m * 0.0254 bmi = weight_kg / (height_m ** 2) if bmi < 18.5: category = "Underweight" elif bmi < 25: category = "Normal weight" elif bmi < 30: category = "Overweight" else: category = "Obese" return round(bmi, 1), category bmi, category = calculate_bmi(70, 1.75) print(f"BMI: {bmi} — {category}") bmi2, cat2 = calculate_bmi(154, 69, unit="imperial") print(f"BMI: {bmi2} — {cat2}")
✏️ Guided

Exercise 1 — Temperature Converter

Complete the temperature converter by filling in the blanks. The formulas are:

Output will appear here...
💡 Hint: In Python, 5/9 gives a float automatically. You can write 5/9 directly.
💪 Independent

Exercise 2 — List Statistics

Write a function summarize_list(numbers) that takes a list of numbers and returns a dictionary with these keys: min, max, sum, average, count, range (max - min).

Test it with: [4, 8, 15, 16, 23, 42] and print all the results neatly.

Output will appear here...
💡 Hint: Use Python’s built-ins: min(), max(), sum(), len(). Return a dict: return {"min": ..., "max": ..., ...}
🔥 Challenge

Exercise 3 — Table Formatter

Write a function format_table(headers, rows) that prints data as a formatted table with | separators and a header divider line. Example output:

| Name | Score | Grade | |-------------|-------|-------| | Alice Smith | 95 | A | | Bob Jones | 82 | B | | Carol Wu | 74 | C |
Output will appear here...
💡 Hint: Find the max width for each column. Use str(val).ljust(width) or rjust(width) for alignment. Join columns with " | ".join(...).
🏆 Mini Project

Mini Project — Unit Converter Toolkit

Build a toolkit of 4 converter functions. Then write a master function that runs all four on any input value and prints a neat summary. Test with value = 100.

Output will appear here...
💡 Hint: Each converter is 1-2 lines. The run_conversions function calls all four and uses f-strings to print results. E.g.: print(f"100 miles = {miles_to_km(100):.2f} km")

✅ Lab 4 Complete!

You’ve built functions with default parameters, multiple return values, and created a reusable toolkit. Functions are the key to writing code that’s clean, testable, and reusable.

Continue to Lab 5: Lists & Dictionaries →

← Lab 3: Loops Lab 4 of 10 Lab 5: Lists & Dicts →