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.

  • Lesson 5 of 5
  • PRO
  • Ages 10+
  • 3 hands-on levels
See what PRO unlocksA one-time $49.99 purchase. Topics 1–3 are free.

The dragon explains

Lists leak out. Numbers don't.

  1. The dragon: Every parameter you've used so far was a number or a piece of text. Watch what happens with a list instead.

  2. The dragon: add_recruit(team) never returns anything — but calling it with party still changes party, permanently.

    example.pyPython 3
    def add_recruit(team):    team.append("Frost")party = ["Ember", "Blaze"]add_recruit(party)print(party)
    Output['Ember', 'Blaze', 'Frost']
  3. The dragon: 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.

    example.pyPython 3
    def add_recruit(team):    team.append("Frost")party = ["Ember", "Blaze"]add_recruit(party)print(party)
  4. The dragon: Now watch a number do the OPPOSITE thing, in a function that looks just as innocent.

  5. The dragon: heal(hp) reassigns hp — but health outside the function never changes. Numbers can't be mutated like this at all.

    example.pyPython 3
    def heal(hp):    hp = hp + 10health = 50heal(health)print(health)
    Output50
  6. The dragon: hp = 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.

  7. The dragon: So: to really change a number, the function must return it, and the caller must reassign — health = heal(health).

Quick check

Check your understanding

Say your answer out loud before reading the marked one. Every wrong option is a mistake real beginners make.

  1. Question 1 of 2

    def add_recruit(team): team.append("Frost"). party = ["Ember"]. add_recruit(party). What does print(party) show?

    question.pyPython 3
    def add_recruit(team):    team.append("Frost")party = ["Ember"]add_recruit(party)print(party)
    • ['Ember', 'Frost'] — party changed, even with no returnCorrect
    • ['Ember'] — party is untouched, because the function never returned anything
    • This causes an error, since team was never given a real list

    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.

  2. Question 2 of 2

    def heal(hp): hp = hp + 10 (no return). health = 50. heal(health). What does print(health) show?

    question.pyPython 3
    def heal(hp):    hp = hp + 10health = 50heal(health)print(health)
    • 50 — health never changedCorrect
    • 60 — health was healed by 10

    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.

In the game

What you'll practice

Three levels in the game: one guided first try with the shape of the answer already there, then two to solve on your own.

  1. 1

    GuidedYour first try

    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.

  2. +2

    PracticeTwo more, on your own

This lesson is part of PRO.

PRO is a one-time $49.99 purchase, no subscription, and unlocks this whole topic along with everything else. Topics 1–3 are free, start to finish.

Unlock this lesson with PRO

Stuck three times? The game offers the answer — unasked.