Python, from Zero, for AILesson 21 of 89

Contents

Python, from Zero, for AI

Lesson 21 of 89. Sign in to keep your place.

Module 3 · Functions, modules, and code you can read again in March

Functions, and the difference between printing and returning

7 min read

print talks to a person; return talks to the programdef wc(t): print(len(t.split()))Characters appear on the screenThe call’s value is Nonea = wc(text); a + 1 raises TypeError onNoneTypeNothing can store, add or send the numberA test has nothing to compare againstdef wc(t): return len(t.split())Nothing appears until you print itThe call’s value is the numbera = wc(text); a + 1 is 13The number can go to a file, a request or ascreenassert wc("a b") == 2 is a testBoth show 12 when you try them at the terminal, which is exactly why this confusion survives so long.The difference appears the moment you try to use the answer for anything: a function that prints canonly ever feed a screen.
Both show 12 when you try them at the terminal, which is exactly why this confusion survives so long. The difference appears the moment you try to use the answer for anything: a function that prints can only ever feed a screen.

Figure 3.1

Naming a piece of work

A function is a chunk of instructions with a name, so you can run it whenever you like without writing it out again.

Python
def word_count(text):
    return len(text.split())

print(word_count("the rain in spain"))   # 4
print(word_count("hello"))               # 1

def starts a definition. word_count is the name. text is a parameter: a name that will be attached to whatever you pass in. The indented block is the body.

Defining a function does not run it. Python reads the def, remembers it, and moves on. The body only runs when you call it, with brackets: word_count("hello").

return hands a value back

Python
def area(width, height):
    return width * height

space = area(3, 4)
print(space)          # 12
print(area(2, 5) + area(1, 1))   # 11

return does two things: it sends a value back to whoever called the function, and it ends the function immediately. Anything after a return that runs is never reached.

Because the call turns into a value, you can use it anywhere a value fits: store it, add it, pass it to another function.

print and return are not the same thing

This is the single biggest confusion at this stage, and it is worth being blunt about.

print puts characters on a screen for a human. return hands a value back to the rest of the program. They look the same when you are testing at the terminal, because in both cases you see 12 appear. They are completely different when the value has to be used.

Python
def double_printed(n):
    print(n * 2)

def double_returned(n):
    return n * 2

a = double_printed(21)    # prints 42
b = double_returned(21)   # prints nothing

print(a)     # None
print(b)     # 42

A function with no return still gives something back: None, Python’s word for “no value”. So a is None, and a + 1 fails with TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'. When you see NoneType in an error, the usual cause is a function that printed instead of returning.

Rule of thumb: functions should return. Print at the outer edge of your program, where you are actually talking to a person.

Default values

Python
def greet(name, greeting="Hello"):
    return f"{greeting}, {name}"

print(greet("Ivan"))                   # Hello, Ivan
print(greet("Ivan", "Good morning"))   # Good morning, Ivan
print(greet(name="Fatima"))            # Hello, Fatima

A parameter with a default becomes optional. You can also pass arguments by name, which makes calls with several options readable.

Names inside stay inside

Python
def tax_on(amount):
    rate = 0.18
    return amount * rate

print(tax_on(1000))   # 180.0
print(rate)
Text
NameError: name 'rate' is not defined

rate lives only while tax_on is running. This is a feature. It means you can write a function without worrying about which names the rest of the program has already used.

A function you will reuse later

Python
def build_messages(question, history=None):
    messages = list(history or [])
    messages.append({"role": "user", "content": question})
    return messages

convo = build_messages("Explain gravity in one sentence.")
convo = build_messages("Now in Hindi.", convo)
print(len(convo))            # 2
print(convo[1]["content"])   # Now in Hindi.

That is the exact structure lesson ten sends to an AI API, built by a function you wrote.

Try this now

Write average(numbers) that returns the mean of a list, and make it return 0 when the list is empty rather than crashing. Then call it and print the result from outside.

Check your understanding

A file defines def double(n): whose only body line is print(n * 2). Then it runs result = double(21) and print(result + 1). What happens?

Pick the one you would defend. Your answer stays private.

Next lesson · 9 minArguments: positional, named, default, and the trap in the default

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘Functions, and the difference between printing and returning’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/functions (Accessed: 31 October 2026).