Tuples in Python

In Python, a tuple is an immutable sequence of heterogeneous objects, including numeric and alphanumeric values. Once a tuple is created, it cannot be modified, nor can new elements be added.

elenco = (element1, element2, ..., elementN)

A Practical Example of a Tuple

Start by naming the variable, followed by the equals sign.

days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

The values in the tuple are enclosed in parentheses.

The values are separated by commas.

Note: Technically, parentheses are optional when defining a tuple, but it is good practice to use them. Commas, however, are mandatory, even if the tuple contains only one element.

A tuple can contain both alphanumeric and numeric values.

For example, you can write a tuple like this:

elenco = ("excellent", 10, "good", 9)

Alphanumeric values are enclosed in double quotes, while numeric values are not enclosed in any quotes.

What is the Difference Between Tuples and Lists in Python?

In Python, lists can be modified.

In a list, you can change the content of elements or add new elements to the end.

In contrast, with a tuple, you cannot change the content of elements or add new ones.

How to Distinguish a List from a Tuple? To differentiate between a list and a tuple, look at the brackets enclosing the values. If they are square brackets, it's a list. If they are parentheses, it's a tuple.

How to Convert a Tuple to a List

To convert a tuple to a list, use the list function.

list = list(tuple)

How to Convert a List to a Tuple

To convert a list to a tuple, use the tuple function.

tuple = tuple(list)

Why Use Tuples?

If a sequence should not be modified, it is preferable to use a tuple rather than a list for the following reasons:

  1. Faster Computation. The processing time for a tuple is shorter compared to a list.
  2. Less Memory Usage. Tuples consume less memory space than lists.

In conclusion, using tuples can reduce both the spatial and temporal complexity of an algorithm.

Single-Element Tuple

When creating a tuple with a single element, you must include a comma.

Otherwise, Python will treat it as a string.

Example

tuple = ('one',)

If you write tuple = ('one'), Python will interpret it as tuple = 'one'.

Examples

Here are some practical examples of using tuples:

Example 1

You can use slicing with tuples.

days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
print(days[:3])

Slicing extracts the first three elements [:3]

Zero is implied, so [:3] is equivalent to [0:3]

('Monday', 'Tuesday', 'Wednesday')

Example 2

days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
print(days[-1])

The output is:

('Sunday')

This retrieves only the last element of the tuple.

Tuple Methods

Tuples have a few methods, only count and index.

Count()

The count method counts the occurrences of an element in the tuple. If it doesn't find it, it returns an exception.

Example

days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
print(days.count('Tuesday'))

The output is:

1

The searched element ('Tuesday') appears once in the tuple.

Index()

The index method searches for an element in the tuple and returns its position. If it doesn't find it, it returns an exception.

Example

days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
print(days.index('Saturday'))

The output is:

5

The searched element ('Saturday') is in the sixth position of the tuple.

Its index is five (5) because the first element always has an index of zero.

How to Search for an Element in a Tuple

To search for something in a tuple, you can use the index or count methods.

However, these methods return an exception if nothing is found.

It is preferable to use the IN operator, which does not return exceptions but only a boolean value, true or false.

A Practical Example

I want to check if a value ('Saturday') exists in the tuple.

days = ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
'Saturday' in days

The result is:

true

 
 

Please feel free to point out any errors or typos, or share suggestions to improve these notes. English isn't my first language, so if you notice any mistakes, let me know, and I'll be sure to fix them.

FacebookTwitterLinkedinLinkedin
knowledge base

Variables in Python

  1. Variables
  2. Variable Types
  3. Strings
  4. Inline Assignment
  5. Lists
  6. Tuples
  7. Sets
  8. Dictionaries