Tuple in Python
Understanding Tuples in Python In Python, a tuple is an immutable, ordered collection of elements. It is similar to a list, but unlike lists, tuples cannot be modified once they are created. This makes tuples a good choice for storing data that should not change throughout the program. Tuples are often used for heterogeneous data (different types of data) or when you want to ensure that the data remains constant. Key Characteristics of Tuples Immutable : Once defined, the elements cannot be altered, added, or removed. Ordered : Tuples maintain the order of their elements. Allows Duplicates : Elements can repeat within a tuple. Heterogeneous : Tuples can hold items of different data types. How to Create a Tuple You can create a tuple by enclosing elements in parentheses () separated by commas. # Creating tuples empty_tuple = () # An empty tuple single_element_tuple = (42,) # A single-element tuple (comma is required) mixed_tuple = (1, "Hello", 3.14, True) #