Is a tuple iterable in Python

Python tutorial - data type tuples

  1. Tutorial
  2. Python 3 basic tutorial
  3. Python tutorial - data type tuples

Created: January-05, 2020 | Updated: November-05, 2020

  1. Advantages of tuples over lists
  2. Create tuples
  3. Access to tuple elements
  4. Python tuple methods
  5. Tuple built-in functions:
  6. Tuple membership check
  7. Iterating through a tuple

In this section we show you how to create and use tuples.

A tuple is similar to a list with the difference that the tuple is immutable, but the list is mutable.

Advantages of tuples over lists

  1. Tuples are mostly used when the elements are of different data types, while lists are used when the elements are of the same data type.
  2. Iterating through tuples is faster because you cannot update a tuple.
  3. You can use the immutable elements of the tuple as keys in the dictionary. This cannot be done with a list.
  4. Tuples guarantee the protection of the data because they cannot be changed.

Create tuples

You can define a Python tuple with the parentheses and separate elements with the commas. A tuple can have elements of any data type.

If the tuple contains only one element, e.g. if the element is of the data type string, then the tuple is not regarded as a tuple but as a string. See the following example:

You can see here that the data type is of rather than. To make it a tuple, a trailing comma is used as the tuple:

Access to tuple elements

The elements of the tuple can be accessed in one of the following ways:

index

An index operator can be used to access elements of a tuple. The index of the tuple starts at 0. If the index is not in the range (element that is not defined at this index in the tuple), you have one. It should be noted here that the index must be an integer, otherwise a will occur.

Negative index

Just like with Python list, you can use negative indexes to access tuple elements.

Slice

A slicing operator is used to extract a range of elements from a tuple.

Concatenated tuples

Two tuples can be concatenated using the operator.

Delete a tuple

The elements of a tuple cannot be deleted because tuples are immutable. But you can delete the entire tuple with the keyword:

Python tuple methods

Tuples only have two methods because tuples are immutable:

Methodsdescription
is used to return the number of elements equal.
is used to return the index of the first item equal.

Tuple built-in functions:

Below are tuples of built-in functions that are applicable to to perform various tasks:

Functions:description
return if there are all elements of the tuple. It also returns when the tuple is empty.
return if is one of the elements of the tuple. It returns when the tuple is empty.
Returns the index and the value of all elements of the tuple as a tuple. There is an object in return.
returns the number of elements in a tuple or the length of the tuple.
convert a sequence (tuple, set, string, dictionary) into tuples.
returns the maximum number in the tuple.
returns the minimum number in the tuple.
return a sorted tuple.
returns the sum of all elements of the tuple.

Tuple membership check

The keyword checks whether an element is a member of the tuple or not. See the code example below:

Iterating through a tuple

You can iterate through a tuple using loop: