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
- Calling the function too early: define it before the line where your program first calls it.
- Missing indentation: put function-body statements under
defwith consistent indentation. - Mixing up display and results: use
returnif 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.
- Create
add(a, b)that returns the sum of two numbers. - Create
is_even(number)that returnsTruefor an even number. - Create
first_letter(word)that returns the first character, or an empty string if the word is empty. - Predict
multiply(2, 3)whenmultiply(a, b=2)returnsa * 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.