Hello everyone, I hope you are having a fantastic day! I took a brief hiatus from posting due to work related obligation, but I am ready to get back to posting. Today I wanted to keep this short and sweet. I will be talking about tuples. Now what is a tuple you might ask? A tuple is almost indistinguishable from a list with one distinct difference, they are immutable. This means that one that tuple is created you cannot add or remove any values from it, if you need to do this you need to recreate the list. This is useful if you have a set of values that shouldn’t ever need to be changed. Like the addresses of houses on a street, those shouldn’t ever change or change very rarely. Another key difference about tuples is how they are defined
Tuple
tuple_test = (1, 2, 3)
List
list_test = [1, 2, 3]
As you can see tuple’s use parenthesis in order to be defined. I will note when assigning just one value to a tuple it needs to be defined as such, it will need to end with a comma.
t1 = (1,)
You can combine tuples and if you set the variable that was previously a tuple to a different value you can overwrite the tuple. You can add and multiple tuples as well
Code:
t1 = (1, 2, 3)
t2 = (4, 5, 6)
t1 = t1 + t2
print(t1)
t1 = (1, 2, 3)
t1 = t1 *3
print(t1)
t1 = 5
print(t1)
Result:
(1, 2, 3, 4, 5, 6)
(1, 2, 3, 1, 2, 3, 1, 2, 3)
5
Final things I would like to note are in and not in also work on tuples however it is identical to lists so I won’t go into detail