Python for AIPython 3.10+ · Pydantic 2.12
Dashboard
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
34 small wins to finish your pathNext lesson →

Functions

A function is a named block of code, defined with def, that takes inputs as parameters and sends a result back with return.

Last updated: 30 Sep, 2026 · Python 3.14

The if chain in if, elif and else sorts one ticket. To sort another, you would copy it. A function gives the code a name so it can run again on different input.

def, calling a function, and print vs return · from Tutorial 12- Python Functions, Positional and Keywords Arguments · 0:38 to 5:41
The video runs each line in a Jupyter notebook, which shows the value of a cell's last line without print(). In a .py file and in the editor here, wrap it in print(), as the run below does.
ExampleFrom the video, run here
def even_odd(num):
    if num%2==0:
        print("The number is even")
    else:
        print("The num is odd")

even_odd(24)

def hello_world():
    print("Hello Welcome")

val = hello_world()
print(val)

def hello_world():
    return "Hello Welcome"

val = hello_world()
print(val)

def add_number(num1, num2):
    return num1 + num2

val = add_number(24, 48)
print(val)

The first hello_world printed its message, so val was None. The second returned it, so val held the text. The ticket version below makes the same choice: categorise returns its answer.

Syntax:

python
def name(parameter):
    ...
    return result

Defining categorise

Example
def categorise(text):
    text = text.lower()
    if "charged" in text or "refund" in text:
        return "billing"
    if "parcel" in text or "arrived" in text:
        return "shipping"
    return "other"

print(categorise("I was charged twice for one order"))
print(categorise("My parcel has not arrived"))
print(categorise("How do I change my password?"))

def starts a function. categorise is its name and text is its parameter: the name the input gets inside the function. The indented lines are what runs each time it is called.

return sends a value back to whoever called the function, and ends the function there. That is why the second if does not need an elif: once a return has run, nothing below it does.

Printing instead of returning

A function that prints its answer looks right until you try to use the answer.

Example
def categorise_and_print(text):
    if "charged" in text:
        print("billing")

result = categorise_and_print("I was charged twice")
print("result is", result)

billing appears, but result is None. A function without return gives back None, so the program cannot use what was shown. Print for people; return for code.

Calling one function from another

Example
def label(ticket):
    category = categorise(ticket["text"])
    return f"#{ticket['id']} {category}"

print(label({"id": 3, "text": "Can I get a refund for the blue mug?"}))

Once categorise exists, any later code can call it. Small functions that each do one thing are easier to test, and pytest tests this exact one.

print(x)return x
Sends x toThe screenThe code that called the function
The call gives backNonex
Use it forPeople reading the outputCode that uses the result

Where functions show up in AI code

  • A tool an agent can call is a plain Python function like categorise.
  • Wrapping a model call once, so the rest of the program calls ask_model(text).
Watch out. categorise without brackets does not run the function; it names it. Writing result = categorise stores the function itself, as Functions as values shows.
Try it yourself
  • Add a check for "password" that returns "account".
  • Call categorise() with nothing in the brackets and read the error.
  • Loop over a list of three ticket texts and print categorise of each.

You understood something today that you didn't yesterday.