PYTHON FUNCTIONS · FREE LESSON

Python functions for beginners:
write code you can reuse

Write your first Python function, understand return values and parameters, and build a small progress calculator. Includes working examples, common mistakes and exercises you can test.

By Lumio AI · Published October 31, 2026 · Free Python 3 tutorial

What does a Python function do?

A function is a named block of code you can run whenever it is needed. You define it with def and call it using parentheses. Functions make programs easier to read, reuse, and test—skills that matter well before you start working with machine learning.

All examples use only Python 3 and work offline. You can create a file called functions_practice.py and run it in a standard Python editor.

Your first function: say hello

def greet(name):
    return "Hello, " + name + "!"

message = greet("Asha")
print(message)

Expected output: Hello, Asha!. The name in the definition—name—is a parameter. The actual value you pass, "Asha", is an argument. If you write the definition but never call greet(), nothing is printed.

Return versus print: why it matters

Use return to send a result back to the caller. Use print() when you want to display something. A returned value can become part of another calculation or test.

def double(number):
    return number * 2

result = double(6)
print(result + 1)

Expected output: 13. If double() only printed 12 instead of returning it, result would be None and adding one would fail. Make this change in your editor to see the error, then restore return.

Mini-project: reusable study progress

Let's make a function that calculates the percentage of completed practice exercises. It has two parameters and handles an invalid total safely.

def progress_percent(completed, total):
    if total <= 0:
        return 0
    return round(completed / total * 100)

print(progress_percent(3, 5))
print(progress_percent(7, 10))
print(progress_percent(0, 0))

Expected output:

60
70
0

The if guard prevents division by zero. In a real app you might also clamp values or reject negative completed counts. For a beginner lesson, being explicit about assumptions makes your code easier to test.

Use default arguments thoughtfully

A function can provide a sensible default, but the caller can override it:

def build_message(topic, prefix="Today"):
    return prefix + " I practice " + topic

print(build_message("loops"))
print(build_message("functions", "Tomorrow"))

Expected output:

Today I practice loops
Tomorrow I practice functions

Parameters without defaults should appear before parameters with defaults. Start with immutable defaults such as strings or numbers; mutable lists and dictionaries as defaults can surprise beginners because they are shared between calls.

Avoid three common function bugs

  1. Calling the function too early: define it before the line where your program first calls it.
  2. Missing indentation: put function-body statements under def with consistent indentation.
  3. Mixing up display and results: use return if another part of your program needs the value; printing does not return it.

Four practice challenges and solutions

Before opening the solutions, write and test the function yourself. Say aloud what each parameter and return value means.

  1. Create add(a, b) that returns the sum of two numbers.
  2. Create is_even(number) that returns True for an even number.
  3. Create first_letter(word) that returns the first character, or an empty string if the word is empty.
  4. Predict multiply(2, 3) when multiply(a, b=2) returns a * b.
Show the worked solutions
def add(a, b):
    return a + b

def is_even(number):
    return number % 2 == 0

def first_letter(word):
    return word[0] if word else ""

def multiply(a, b=2):
    return a * b

print(add(2, 3))          # 5
print(is_even(6))         # True
print(first_letter(""))   # empty line
print(multiply(2, 3))     # 6

Try changing the inputs and writing your own assert statements, such as assert add(2, 3) == 5. A small repeatable test is better than hoping your code works.

Next steps: combine functions with loops

Practice a for loop and a while loop if you haven't already. Then combine what you know to build an interactive tool that repeatedly asks for input and uses a function to calculate or look up an answer.

Our free Python chatbot tutorial does exactly this without claiming that a rule-based FAQ bot is a generative AI model. For a bigger picture, follow the 7-day Python for AI plan or explore the AI engineering learning roadmap. Lumio's mobile missions are still being developed; these lessons are usable today. Continue learning with Python if, elif and else conditionals.