Tuple shares some similarities to
Just like a list, a tuple variable can hold multiple values.
- There are no square brackets
- The values are separated by a
- It is good practice to use brackets
)around the values, but it is not required. However, where python can't understand whether to evaluate the values as a tuple or a list, brackets will be required.
a_tuple = "Rishi", "Abee" another_tuple = (1, 2)
⚠️ NOTE: Leaving a trailing
, at the end of a value will cause python to evaluate the variable as a tuple instead.
a_string = "Rishi" a_short_string_tuple = "Rishi", a_short_number_tuple = 2,
Tuples can be added to a list.
💡 To help python better understand whether to evaluate the values as a tuple, brackets
) are required.
a_list = ["Orange", "Yellow"] a_tuple_inside_a_list= [('🍏', '🍎'), another_tuple, 'Hello']
Unlike a list, a tuple cannot be modified by appending values to it.
If we need to add values to a tuple, we will have to re-assign the same tuple a new value.
# Adding to a tuple. a_tuple = "Rishi", "Abee" a_tuple = a_tuple + ("Python",) print(a_tuple )