LEARN Python - 4.3 -Tuples Lists That Refuse to Change - zerotopyhero.com

Learn Python – 4.3: Tuples – Lists That Refuse to Change

Lists are flexible.
Sometimes a little too flexible.

You can change them, reorder them, remove things, add things, and accidentally ruin your program without noticing right away.

Tuples exist for the moments when Python should say:

“No. This stays exactly as it is.”

What Is a Tuple, Really?

A tuple is a collection of items, just like a list.

The difference is simple:

  • Lists can change

  • Tuples cannot

Lists use square brackets:

				
					fruits = ["apple", "banana", "cherry"]

				
			

Tuples use parentheses:

				
					fruits = ("apple", "banana", "cherry")

				
			

Same data.
Very different rules.

You Can Read Tuples (But Not Change Them)

You can access tuple items using indexes, just like lists.

				
					numbers = (10, 20, 30)
print(numbers[1])

				
			

Output:

				
					20

				
			

Reading is fine.

Changing is not.

				
					numbers[1] = 99

				
			

Python will stop you.
Not politely.

So this means: 

  • Lists are mutable
  • Tuples are immutable

Why Would You Use Tuples?

Because sometimes data should never change.

Examples:

  • Coordinates like (x, y)

  • Days of the week

  • Fixed settings

  • Values you want to protect from mistakes

Tuples are Python’s way of saying:
“Trust me, you don’t want this edited later.”

The One-Item Tuple Trap

This one gets almost everyone once.

This is not a tuple:

				
					item = (5)

				
			

That’s just the number 5.

This is a tuple:

				
					item = (5,)

				
			

The comma is what makes it a tuple.
Without it, Python doesn’t care about the parentheses.

Yes, it’s weird.
You’ll remember it because it’s weird.

Tuples Care About Order

Just like lists, tuples keep their order.

				
					letters = ("a", "b", "c")

				
			

Indexing works the same way:

				
					print(letters[0])

				
			

Output:

				
					a

				
			

The only difference is what happens after that.

Nothing changes. Ever.

Common Beginner Mistakes (Very Normal)

Mistake 1: Trying to change a tuple

				
					coords = (10, 20)
coords[0] = 5

				
			

This will crash.
Every time.

Mistake 2: Forgetting the comma in a one-item tuple

				
					single = (3)

				
			

Not a tuple.

Mistake 3: Using a tuple when you actually need a list

If you plan to:

  • Add items

  • Remove items

  • Update values

You want a list, not a tuple.

What You’ve Learned

You now know:

  • What a tuple is

  • How tuples differ from lists

  • How to access tuple items

  • Why tuples are useful

  • The one-item tuple comma rule

Solid progress.

Mini Quiz

Try these:

1. What is the main difference between a list and a tuple?

2. Which line creates a tuple?

				
					A) data = [1, 2, 3]
B) data = (1, 2, 3)
C) data = {1, 2, 3}

				
			

3. What happens if you try to change a tuple value?

4. What will this print?

				
					values = (5, 10, 15)
print(values[2])

				
			

5. How would you change 10 to 99 in the tuple above?

ZeroToPyHero