Codzy

Tuples and sets

A tuple looks like a list, but it is written between parentheses and can't be changed: point = (3, 4). You use it for values that belong together and don't change. You can "unpack" it: x, y = point.

A set is written between curly braces and keeps no duplicates. set(my_list) turns a list into a set, which removes repeated values, and in checks very quickly whether a value is present.

point = (3, 4)
print(point[0])
x, y = point
print(x + y)

colors = {"red", "blue", "red"}
print(len(colors))
print("blue" in colors)

Start the lesson