Introduction
Tuples are like boxes that keep things safe and unchanged. Once you put something inside, you cannot change it. This helps keep your data safe and reliable.
Jump into concepts and practice - no test required
Tuples are like boxes that keep things safe and unchanged. Once you put something inside, you cannot change it. This helps keep your data safe and reliable.
my_tuple = (item1, item2, item3)
colors with three color names.colors = ('red', 'green', 'blue')
empty_tuple = ()
single_item = ('hello',)This program shows that you cannot change items in a tuple. It tries to change the first item and catches the error. Then it prints the first item to show reading works fine.
my_tuple = (1, 2, 3) print("Original tuple:", my_tuple) # Trying to change the first item will cause an error try: my_tuple[0] = 10 except TypeError as e: print("Error:", e) # You can read items but not change them print("First item:", my_tuple[0])
Tuples are faster than lists because they cannot change.
If you need to change data, use a list instead.
Even though tuples are immutable, if they contain mutable items like lists, those items can change.
Tuples hold items that cannot be changed after creation.
Trying to change a tuple item causes an error.
Use tuples when you want fixed, safe data.
t = (1, 2, 3) t[1] = 5 print(t)
my_tuple = (10, 20, 30) my_tuple[0] = 5 print(my_tuple)
data = (1, [2, 3], 4). Which statement is true about modifying this tuple?