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
The return Statement
def double(x):
return x * 2
print(double(5))Early Return
def abs_val(x):
if x < 0:
return -x
return x
print(abs_val(-7))Local Scope
def f():
x = 10
print(x)
f()
# print(x) # NameErrorGlobal Scope
name = 'Alice'
def greet():
print('Hello', name) # reads global
greet()global Keyword
counter = 0
def increment():
global counter
counter += 1
increment()
print(counter)nonlocal Keyword
def make_counter():
count = 0
def inc():
nonlocal count
count += 1
return count
return inc
c = make_counter()
print(c(), c())LEGB Rule
x = 'global'
def outer():
x = 'enclosing'
def inner():
print(x) # enclosing
inner()
outer()Returning Multiple Values
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)
def make_adder(n):
def adder(x):
return x + n
return adder
add5 = make_adder(5)
print(add5(3))None vs Missing Return
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
Recap
Keep Going
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
- Defining and Calling Functions
- Default and Keyword Arguments
- args and kwargs
- Return Values and Scope