CurriculumPython Functions, Parameters and return for KidsLists leak out. Numbers don't.
Why a Python Function Can Change a List But Not a Number
A function can change your list forever — but a number is safe no matter what happens inside.
Lists leak out. Numbers don't.
Every parameter you've used so far was a number or a piece of text. Watch what happens with a list instead.
add_recruit(team) never returns anything — but calling it with party still changes party, permanently.
def add_recruit(team):
team.append("Frost")
party = ["Ember", "Blaze"]
add_recruit(party)
print(party)Output
['Ember', 'Blaze', 'Frost']team inside the function and party outside it are the SAME list — exactly like list-s3's treasury = hoard. Passing a list doesn't copy it.
def add_recruit(team):
team.append("Frost")
party = ["Ember", "Blaze"]
add_recruit(party)
print(party)Now watch a number do the OPPOSITE thing, in a function that looks just as innocent.
heal(hp) reassigns hp — but health outside the function never changes. Numbers can't be mutated like this at all.
def heal(hp):
hp = hp + 10
health = 50
heal(health)
print(health)Output
50hp = hp + 10 doesn't change the number 50 — numbers can't change. It just points the LOCAL name hp at a new number, 60, and throws that away when the function ends.
So: to really change a number, the function must return it, and the caller must reassign — health = heal(health).
Check your understanding
def add_recruit(team): team.append("Frost"). party = ["Ember"]. add_recruit(party). What does print(party) show?
def add_recruit(team):
team.append("Frost")
party = ["Ember"]
add_recruit(party)
print(party)Why: party ends up with Frost added. Passing a list into a function doesn't copy it — team and party are one list with two names, same as list-s3's b = a. The function needs no return at all to change it.
def heal(hp): hp = hp + 10 (no return). health = 50. heal(health). What does print(health) show?
def heal(hp):
hp = hp + 10
health = 50
heal(health)
print(health)Why: health stays 50. Unlike the list example, reassigning a number parameter inside a function never affects the caller's variable — the only way to really change it is to return the new value and reassign.
What you'll practice
def add_recruit(team): team.append("Frost") — call add_recruit(party) with party = ["Ember", "Blaze"], then print(party). The function never used return — but party changed anyway.
More from this topic
Lists leak out. Numbers don't. is one lesson inside Python Functions, Parameters and return for Kids — see the full lesson order and what the whole topic covers.
View the full topic →