Tuples are versatile and lightweight data structures in Python that allow you to store collections of items. While similar to lists, tuples have some key differences that make them unique and useful in various programming scenarios.
In this guide, we’ll delve into tuples in Python, covering their creation, properties, and practical applications with clear explanations and up-to-date code examples.
What are Tuples?
A tuple is an ordered collection of items, similar to a list. However, unlike lists, tuples are immutable, meaning their elements cannot be changed or modified after creation. Tuples are created using parentheses ()
and can contain elements of different data types.
Creating Tuples
Creating a tuple in Python is simple. You can define a tuple by enclosing comma-separated values within parentheses.
# Creating a tuple of fruits
fruits = ('apple', 'banana', 'cherry')
print(fruits) # Output: ('apple', 'banana', 'cherry')