0Pricing
Python Academy · Lesson

Return Values and Scope

Understand return statements and variable scope (local vs global).

Return Values and Scope is a free Python Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

Understanding return statements and variable scope is fundamental to writing predictable, bug-free Python functions.

The return Statement

return expr exits the function and sends expr back to the caller. return without a value sends None.
def double(x):
    return x * 2
print(double(5))

Early Return

Multiple return statements are fine — the first one hit exits the function. Useful for guard clauses.
def abs_val(x):
    if x < 0:
        return -x
    return x
print(abs_val(-7))

Local Scope

Variables created inside a function are local — they don't exist outside.
def f():
    x = 10
    print(x)
f()
# print(x)  # NameError

Global Scope

Variables defined at module level are global. Functions can READ globals without declaring them.
name = 'Alice'
def greet():
    print('Hello', name)  # reads global
greet()

global Keyword

global x inside a function allows you to WRITE to a global variable. Use sparingly — it makes code hard to reason about.
counter = 0
def increment():
    global counter
    counter += 1
increment()
print(counter)

nonlocal Keyword

nonlocal x in a nested function lets you write to an enclosing (but not global) scope variable.
def make_counter():
    count = 0
    def inc():
        nonlocal count
        count += 1
        return count
    return inc
c = make_counter()
print(c(), c())

LEGB Rule

Python resolves names in order: Local → Enclosing → Global → Built-in. This is the LEGB rule.
x = 'global'
def outer():
    x = 'enclosing'
    def inner():
        print(x)  # enclosing
    inner()
outer()

Returning Multiple Values

def f(): return 1, 2, 3 returns a tuple. a, b, c = f() unpacks it.
def stats(lst):
    return min(lst), max(lst), sum(lst)/len(lst)
lo, hi, avg = stats([1,2,3,4,5])
print(lo, hi, avg)

Returning Functions (Closures)

A function can return another function. The inner function remembers the outer scope — this is a closure.
def make_adder(n):
    def adder(x):
        return x + n
    return adder
add5 = make_adder(5)
print(add5(3))

None vs Missing Return

Always return None explicitly when signalling absence. Relying on implicit None is valid but explicit is clearer.
def find(lst, val):
    for i, x in enumerate(lst):
        if x == val:
            return i
    return None
print(find([1,2,3], 9))

Quick Check

In the LEGB rule, which scope is checked last?

Recap

return exits a function with a value. Local vars don't leak. Use global/nonlocal to write to outer scopes. LEGB: Local Enclosing Global Built-in.

Keep Going

Great work! Move on to the next lesson to keep progressing.

Frequently asked questions

Is the “Return Values and Scope” lesson free?

Yes — the full text of “Return Values and Scope” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Return Values and Scope”?

Understand return statements and variable scope (local vs global). You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Return Values and Scope” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Defining and Calling Functions
  2. Default and Keyword Arguments
  3. args and kwargs
  4. Return Values and Scope
← Back to Python Academy