Core programming fundamentals · Lesson 13 of 60 · Code lesson, JavaScript or Python · Optional
Mutability and immutability
A mutable value can be changed in place, while an immutable value never changes, and any "change" makes a new value.
Lists are mutable, and text strings are immutable.
Knowing which is which explains why a change can show up in more than one place.
Worked example
In JavaScript, const stops the name from being reassigned, but the list itself is still mutable, so push can change it.
const word = "cat";
const loud = word.toUpperCase();
console.log(word, loud);Output: cat CAT
In Python, lists are mutable, so append changes the list itself, while strings and tuples never change.
word = "cat"
loud = word.upper()
print(word, loud)Output: cat CAT
Your turn
Add "done" to the list tags, so the program prints 3 for the number of items.
Expected output: 3
Loading your practice…
Preparing your workspace…