Loops, and where your data lives

INPUT · Slides

A group you cannot change afterwards

01 / 07

Values listed with commas

Values listed with commas inside () are called a tuple.

It looks a lot like a list. The only difference on the page is the brackets — [] for a list, () for a tuple. It comes out with () when you show it, too.

size = (150, 3)print(size)

Result

(150, 3)

02 / 07

You take things out as you would from a list

Taking the contents out works exactly as it does with a list. You index with numbers starting at zero, and -1 gets the last one.

Slicing works the same way as well. Things in a row are handled alike — that habit of Python is showing here too.

size = (150, 3, 20)print(size[0])print(size[-1])print(size[0:2])

Result

150
20
(150, 3)

03 / 07

You cannot change it afterwards

So what is different from a list? Try to change the contents and you get an error.

With a list you could swap one over with fruits[0] = "orange". A tuple will not have it. You cannot add to it or remove from it either.

size = (150, 3)size[0] = 200

Result

TypeError: 'tuple' object does not support item assignment

04 / 07

Why not being able to change it is a good thing

You might wonder how not being able to do something counts as an advantage.

The answer is that it lets you guarantee that what should not change will not. The size of a screen, the order of the days, the coordinates on a map — some things would cause trouble if you altered them by accident. Make them a tuple and the mistake shows up as an error the moment you make it.

A list if you plan to change it, a tuple if you do not. That is the rule of thumb.

days = ("Mon", "Tue", "Wed")for day in days:    print(day)

Result

Mon
Tue
Wed

05 / 07

A group of one needs a comma

Everyone trips here. A tuple with one item is written (1,), with a comma after it.

Write (1) and you have plain old 1. The brackets are read as the brackets that change the order of a sum.

So what makes a tuple is not the brackets but the comma.

single = (1,)print(single)number = (1)print(number)

Result

(1,)
1

06 / 07

The brackets can go

Since the comma is what counts, you can leave the brackets out. size = 150, 3 is a tuple as well.

It works the other way round too. Write height, width = size and you take the contents into two variables at once.

You have seen this somewhere. for key, value in person.items(): is exactly this shape — the pairs that items() was handing back were tuples.

size = 150, 3print(size)height, width = sizeprint(height)print(width)

Result

(150, 3)
150
3

07 / 07

Choosing between a list and a tuple

One last summary.

  • a list [] … you add to it, remove from it, swap things over. The count grows
  • a tuple () … made once and left alone. What each position means is fixed

A register is a list, a coordinate is a tuple. A list is fine when you cannot decide, but where you can honestly say "this will not change", a tuple is the safer bet.

That is the end of this group. A list you index by number, a dictionary you look up by name, a tuple that does not change — three containers, all yours. Next comes a piece of work that uses all of them together with looping.

person = {"name": "Yui", "size": (150, 42)}print(person["size"])

Result

(150, 42)