What are tuples in python

In Python,  tuples are an ordered, immutable collection of elements, which can be of different data types. Tuples are similar to lists, but they are immutable, meaning that once a tuple is created, its elements cannot be modified.

Alright, imagine you’re at a pizza restaurant and you want to order a pizza. You can order a pizza with one topping, two toppings, three toppings, or even more toppings if you’re feeling adventurous. And let’s say you want to keep track of what toppings you ordered, but you don’t want to be able to change your order later. That’s where tuples come in!

A tuple in Python is like a pizza order – it’s an ordered collection of things, and once you place your order (i.e. create the tuple), you can’t change it (i.e. it’s immutable). So, if you order a pizza with pepperoni, mushrooms, and olives, that’s like creating a tuple with the elements “pepperoni”, “mushrooms”, and “olives”.

pizza tupples python example

How to define tuples in Python

A tuple is defined by enclosing a sequence of elements in parentheses, separated by commas. Here’s an example:

my_tuple = (1, 2, 'three', True)

In this example, we define a tuple my_tuple that contains four elements of different data types. Once a tuple is created, you cannot modify its elements. For example, attempting to assign a new value to an element of a tuple will result in a TypeError.

How to use tuples

Tuples can be used in various ways, such as:

  • Returning multiple values from a function: A function can return multiple values by returning a tuple. For example:

def get_name_and_age():<br>
    return 'Alice', 30</p>
<p>name, age = get_name_and_age()<br>
print(name)  # output: Alice<br>
print(age)  # output: 30<br>

  • As keys in a dictionary: Because tuples are immutable and hashable, they can be used as keys in a dictionary. For example:

my_dict = {('Alice', 30): 'Engineer', ('Bob', 25): 'Designer'}

    • You can use tuples to group related data:  similar to how a struct or record is used in other programming languages.

For example:

person = ('Alice', 30, 'Engineer')<br>
name, age, occupation = person<br>
print(name)  # output: Alice<br>
print(age)  # output: 30<br>
print(occupation)  # output: Engineer<br>

    • But wait, there’s more! Tuples are also great for returning multiple values from a function.

Imagine you have a function that calculates the area and perimeter of a rectangle, and you want to return both values.

    You could use a tuple to do that! Here’s an example:

def calculate_area_and_perimeter(width, height):<br>
    area = width * height<br>
    perimeter = 2 * (width + height)<br>
    return area, perimeter</p>
<p>result = calculate_area_and_perimeter(5, 4)<br>
print(result)  # output: (20, 18)

In this example, the function calculate_area_and_perimeter returns a tuple with the area and perimeter of a rectangle. And when we call the function and store the result in the variable result, we get a tuple with the values (20, 18).

So there you have it, tuples in Python are like pizza orders. They are great for returning multiple values from a function. And the best part is, you don’t have to worry about someone changing your pizza order or tuple later on!

Learn more about tuples in Python

Here are two references you can use to learn more about tuples in Python:

  1. The official Python documentation on tuples: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences

This documentation provides a detailed explanation of tuples in Python, including their syntax, creation, indexing, slicing, and methods. It also covers some common use cases for tuples, such as returning multiple values from a function and using tuples as keys in a dictionary.

  1. Real Python’s article on tuples: https://realpython.com/python-tuples/

This article provides a beginner-friendly introduction to tuples in Python, covering their basic syntax, creation, and indexing. It also explains the differences between tuples and lists, and provides some practical examples of how to use tuples in Python. The article also includes some exercises and quizzes to help you practice what you’ve learned.