Learn Python
Lesson 1 - Introduction To Python
Lesson 2 - Basic Python Syntax
Lesson 3 - Control Flow
Lesson 4 - Functions
Lesson 6 - Exception Handling
Lesson 7 - Modules And Packages
Lesson 8 - Basic String Operations
Lesson 9 - Object-Oriented Programming (OOP)
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.