Python Tuples

Python Tuples, What is a Tuple?, Operations on Tuples, Access Tuple Items, Range of Indexes, Change Tuple Values, Loop Through a Tuple, Check if Item Exists, Find Tuple Length, Add Items, Remove Items, and Join Two Tuples.

Python Data Structures – Tuples

What is a Data Structure?
Organizing, managing, and storing data is important as it enables easier access and efficient modifications. Data Structures allows you to organize your data in such a way that enables you to store collections of data, relate them and perform operations on them accordingly.

Types of Data Structures in Python
Python has implicit support for Data Structures which enable you to store and access data. These structures are called List, Dictionary, Tuple, and Set.

What is a Tuple?

A tuple is a collection that is ordered and unchangeable. In Python, tuples are written with round brackets.

A Data Structure of Python or a Compound data type of Python language

Example:

Create a Tuple:

fruits = (“apple”, “banana”, “cherry”)
print (fruits)

Operations on Tuples

1. Access Tuple Items
2. Range of Indexes
3. Change Tuple Values
4. Loop Through a Tuple
5. Check if Item Exists
6. Find Tuple Length
7. Add Items
8. Remove Items
9. Join Two Tuples

1. Access Tuple Items

We can access tuple items by referring to the index number, inside square brackets:

Example:

Print the second item in the tuple:

fruits = (“apple”, “banana”, “cherry”, “orange”, “kiwi”, “melon”, “mango”)
print(fruits [1])

Negative Indexing

Negative indexing means beginning from the end, -1 refers to the last item, -2 refers to the second last item etc.

Example:
Print the last item of the tuple:

fruits = (“apple”, “banana”, “cherry”, “orange”, “kiwi”, “melon”, “mango”)
print(fruits[-1])

2. Range of Indexes

We can specify a range of indexes by specifying where to start and where to end the range.

When specifying a range, the return value will be a new tuple with the specified items.

fruits = (“apple”, “banana”, “cherry”, “orange”, “kiwi”, “melon”, “mango”)
print(fruits [2:5])

It will print: (‘cherry’, ‘orange’, ‘kiwi’)

Note: the item in position 5 is Not included

Range of Negative Indexes

Specify negative indexes if you want to start the search from the end of the tuple:

Example

fruits = (“apple”, “banana”, “cherry”, “orange”, “kiwi”, “melon”, “mango”)
print(fruits [-4:-1])

It will print: (‘orange’, ‘kiwi’, ‘melon’)
This example returns the items from index -4 (included) to index -1 (excluded)

3. Change Tuple Values

Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable, but we can convert the tuple into a list, change the list, and convert the list back into a tuple.

Example

x = (“apple”, “banana”, “cherry”)
y = list(x)
y[1] = “kiwi”
x = tuple(y)

print(x)

4. Loop Through a Tuple

We can loop through the tuple items by using a for loop.

Example
Iterate through the items and print the values:

fruits= (“apple”, “banana”, “cherry”)
for x in fruits:
print(x)

5. Check if Item Exists

To determine if a specified item is present in a tuple,

Example

Check if “apple” is present in the tuple:

fruits = (“apple”, “banana”, “cherry”)

if “apple” in fruits:
print(“Yes, ‘apple’ is in the fruits tuple”)

6. Find Tuple Length

To determine how many items a tuple has, use the len() keyword

Example

fruits = (“apple”, “banana”, “cherry”)
print(len(fruits))

7. Add Items

Once a tuple is created, we cannot add items to it. Tuples are unchangeable.

fruits = (“apple”, “banana”, “cherry”)
fruits[3] = “orange” # This will raise an error
print(fruits)

8. Remove Items

Note: We cannot remove items in a tuple.

Tuples are unchangeable, so we cannot remove items from it, but we can delete the tuple completely:

Example

The del keyword can delete the tuple completely:

fruits = (“apple”, “banana”, “cherry”)
del fruits
print(fruits ) #this will raise an error because the tuple no longer exists

9. Join Two Tuples

To join two or more tuples you can use the + operator:

Example
Join two tuples:

tuple1 = (“a”, “b” , “c”)
tuple2 = (1, 2, 3)

tuple3 = tuple1 + tuple2
print(tuple3)


Python Step by Step Tutorials

Python Video Tutorial

Follow me on social media:

2 thoughts on “Python Tuples”

Comments are closed.