Master AI & Build your First Coding Portfolio with SkillReactor | Sign Up Now

Lesson 5 - Basic Data Structures

5.2 Tuples

Tuples are similar to lists but are enclosed by () and their elements cannot be changed after declaration.

Tuples are commonly used for storing a fixed collection of related data, especially for function-to-function data sharing.

Here's how you declare a tuple in Python:

my_tuple = (1, 2, 'orange', True)

Accessing an item from a tuple is similar to accessing an item from a list; you use its index (position). Remember that tuple indices start from 0, not 1.

For example:

my_tuple = (1, 2, 'orange', True)
print(my_tuple[1])

Feel free to change the index inside the print() function to access different elements of my_tuple. However, remember that unlike lists, you cannot change the elements of a tuple after it has been declared.