Skip to content

Latest commit

 

History

History
58 lines (47 loc) · 1.76 KB

File metadata and controls

58 lines (47 loc) · 1.76 KB

Understanding Tuples

What is a Tuple?

A tuple is a data structure similar to a list, but unlike lists, tuples are immutable, meaning their contents cannot be changed after creation. Tuples are typically used for grouping related data.

Creating Tuples

You can create a tuple in various programming languages. In Python, for example, you create a tuple using parentheses:

my_tuple = (1, 2, 'apple', 'banana')

Tuple Indexing

Tuple elements are indexed, starting from 0 for the first element. You can access elements by their index, just like lists.

first_element = my_tuple[0]  # Access the first element (1)

Tuple Length

You can find the length of a tuple using the len() function.

tuple_length = len(my_tuple)  # Length of the tuple (4)

Common Tuple Operations

Accessing Tuple Elements

Tuples are immutable, so you can only access their elements.

second_element = my_tuple[1]  # Access the second element (2)

Tuple Packing and Unpacking

You can pack multiple values into a tuple and unpack them into separate variables.

coordinates = (3, 4)
x, y = coordinates  # Unpack the tuple into x and y (x=3, y=4)

Concatenating Tuples

You can concatenate two or more tuples to create a new tuple.

new_tuple = my_tuple + (3.14, 'cherry')  # Concatenates my_tuple with a new tuple

Checking for an Element

You can check if an element exists in a tuple using the in keyword.

is_present = 'apple' in my_tuple  # Checks if 'apple' is in the tuple (True)

Using Tuples for Multiple Return Values

Tuples are often used to return multiple values from a function.

def get_coordinates():
    return (3, 4)

x, y = get_coordinates()  # Unpack the returned tuple (x=3, y=4)