× Lesson 1 Lesson 2 Lesson 3 Lesson 4 Lesson 5 Lesson 6 Lesson 7 Lesson 8 Lesson 9 Lesson 10 Lesson 11 Lesson 12 Lesson 13 Lesson 14 Lesson 15 Lesson 16 Lesson 17 Lesson 18 Lesson 19 Lesson 20 Lesson 21 Lesson 22 Lesson 23 Lesson 24 Lesson 25 Mini Lesson 1 Mini Lesson 2 Mini Lesson 3 Mini Lesson 4 Mini Lesson 5


Mini Lesson 4: Tuples

A tuple is considered a type of array, which consists of dictionaries, lists, sets, and tuples, and are in many ways similiar to lists. However, there are a few key differences between the two. The most obvious is that tuples use parenthesises rather than brackets when enclosing data. They are also considered immutable, aka unchangeable, so once they are set, you can not change or add any data.

a_tuple = (1, 2, 3)


a_tuple[0] = 4

First, we create a tuple that contains 1, 2, 3. Then we print the tuple out, then we try to reassign the first element to 4, however that brings up in error because as stated early tuples are immutable.

Tuples, just like lists can be created using the tuple() function. The main reason for using a tuple over a list is if you want the data stored in the tuple to be stored forever, and the only way to "change" the data is by using the del keyword to get rid of the tuple completly.