A mutable thing can change, while an immutable one stays the same, like a toy that you can take apart or one that’s stuck together forever.
Imagine you have two boxes of crayons. One box is mutable, you can open it, take out some crayons, and even add new ones. It's like having a bag of snacks where you can eat some and put others in. The other box is immutable, once it’s closed, no one can change what’s inside. It’s like getting a sealed candy bar, you can’t open it or swap the candies inside.
Crayons vs. Stickers
Let’s say you're drawing with crayons from the mutable box. You use up red and blue, but then you add green. The box still has crayons, just different ones now. That's mutability in action, changing what's inside without replacing the whole thing.
Now think about a sticker book. Once you stick a sticker on paper, it stays there forever. If you want to change it, you have to rip it off and replace it with another one. That’s like an immutable object, once it's set, you can't just alter parts of it easily.
So, mutable things are like toys that let you play around with them, while immutable ones are more like sealed treasures, nice to have, but a bit harder to change! A mutable thing can change, while an immutable one stays the same, like a toy that you can take apart or one that’s stuck together forever.
Imagine you have two boxes of crayons. One box is mutable, you can open it, take out some crayons, and even add new ones. It's like having a bag of snacks where you can eat some and put others in. The other box is immutable, once it’s closed, no one can change what’s inside. It’s like getting a sealed candy bar, you can’t open it or swap the candies inside.
Examples
- A list that can change its items is mutable, like a shopping cart where you can add or remove things. A tuple that stays the same, like a fixed set of toppings for pizza, is immutable.
Ask a question
See also
- Programming vs Coding - What's the difference?
- What are programming languages?
- What are modules?
- What are memory leaks?
- How Does 6 Coding Concepts You MUST Know For Beginners Work?