Lesson Overview

In today's lesson, we'll explore tuples in Python, a simple yet powerful data structure. A tuple is similar to a list, except that its elements cannot be changed. It is an immutable, ordered collection of elements.

The beauty of tuples lies in their simplicity and efficiency; the tuple packing and indexing operations are optimized for swift creation and manipulation. Tuples are memory efficient and safer than lists due to their immutability. By the end of this lesson, you'll be able to create, manipulate, and understand the unique applications of tuples in Python.

Understanding Tuples

A tuple, stored as a single variable in Python, holds multiple items defined by enclosing elements in parentheses (). Each item is accessible via indexing by integers. This core immutability distinguishes tuples from mutable lists. Unlike lists, tuples can serve as dictionary keys and set elements.

Consider this Python tuple declaration as an example:

class TupleExample:
    def create_tuple(self):
        return ("apple", "banana", "cherry")

# create an instance and call the `create_tuple` method
tuple_example = TupleExample()
print(tuple_example.create_tuple())  # Output: ('apple', 'banana', 'cherry')
Creating Tuples

Tuples are created by enclosing elements in parentheses (), with commas to separate them — a style known as 'Tuple Packing'. For a single-element tuple, add a trailing comma: my_tuple = ("apple",). The built-in tuple function can convert other data types to tuples.

In the next Python example, we illustrate tuple creation:

class TupleExample:
    def create_tuples(self):
        packed_tuple = ("apple", "banana", "cherry")
        singleton_tuple = ("apple",)
        from_list = tuple(["apple", "banana", "cherry"])
        return packed_tuple, singleton_tuple, from_list

# create an instance and call the `create_tuples` method
tuple_example = TupleExample()
print(tuple_example.create_tuples())  
# Output: (('apple', 'banana', 'cherry'), 'apple', ('apple', 'banana', 'cherry'))

This example creates tuples via packing, a single-element tuple, and from a list.

Inspecting and Modifying Tuples

Just like lists or strings, tuple elements are accessible via indexes. Positive indexes reference from the beginning of the tuple, while negative indexes traverse from the end. Slicing a tuple, similar to slicing a pizza, provides a range of elements. Although tuples are immutable, we can create new tuples from existing ones in Python.

Consider the following example that depicts inspecting and modifying tuples:

class TupleExample:
    def inspect_tuple(self):
        my_tuple = ("apple", "banana", "cherry", "durian", "elderberry")
        print(my_tuple[1])   # Output: 'banana'
        print(my_tuple[-1])  # Output: 'elderberry'
        print(my_tuple[2:4]) # Output: ('cherry', 'durian')
        new_tuple = my_tuple[1:3] + ("dragonfruit",)
        print(new_tuple)     # Output: ('banana', 'cherry', 'dragonfruit')

# create an instance and call `inspect_tuple` method
tuple_example = TupleExample()
tuple_example.inspect_tuple()
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal