Python, from Zero, for AILesson 24 of 89

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

Returning: one value, several values, or nothing at all

In this lesson

  • 7 min read
  • 6 reading sections
  • Knowledge check
  • Lesson 4 of 9 in this chapter

This chapter’s goalSplit a working script into functions and modules with explicit arguments, return values and docstrings, explain from Python’s scoping rules why a variable changed inside a function did or did not change outside it, and run the result from a terminal with named options.

This chapter’s media

The film explains chapter ideas. The audiobook narrates the complete teaching text.

FormatEnglishहिंदी
Chapter filmWatch1:44Not available
Full audiobookNot availableNot available

A little at a time

Explore an idea, try it, then check what stays.

Step 1 of 8Free · no account needed
Lesson steps

return ends the function immediately

Python
def first_even(numbers):
    for n in numbers:
        if n % 2 == 0:
            return n
    return None

The return inside the loop stops the loop and the function. Nothing after it runs. That is why the guard-clause style from the branching lesson works so well: each early return removes a case and the remaining code gets simpler rather than more nested.

A function with no return statement, or with a bare return, hands back None. This is not an error; it is a value, and it will show up later as TypeError: 'NoneType' object is not subscriptable if you forget.

Your place and first choices stay on this browser. Classroom credit is recorded separately.

Stuck on something?Ask Divya, your AI tutor

How to cite: Addaly (2026) ‘Returning: one value, several values, or nothing at all’, in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/returning-values (Accessed: 29 November 2026).