DEV Community

Irene  Njuguna
Irene Njuguna

Posted on

Exploring Python Tuples: A Beginner's Guide.

Introduction

Tuples is a datatype in Python, it provides a way to store multiple items within a single variable. This article explores tuples, comparing them with other data types like lists, sets, and dictionaries, showing their unique features.

What is a tuple?

A tuple is an ordered and unchangeable collection written with round brackets.

Example of a tuple:

my_tuple = ("Kiwi", "orange", "mango")

Basic Tuple Properties

Ordered: Tuples maintain a defined order of items.
Unchangeable: Once created, tuples remain immutable; items cannot be altered, added, or removed.
Allow Duplicates: Similar values can exist in a tuple.

How to:

Create A Tuple

fruits = ("apple", "kiwi", "banana")
print(fruits)
Enter fullscreen mode Exit fullscreen mode

1. Accessing items in a tuple;

There are two ways to access a tuple.

By index;

if you want to access the second item in the fruits tuple above, you can do this;


second_fruit = fruits[1]
print(second_fruit, fruits)

Enter fullscreen mode Exit fullscreen mode

The first item has index 0.

By negative indexing;

Negative indexing means starting from the end.
so the last item is accessed by -1, 2nd last item on the list is -2, and so on

last_fruit = fruits([-1])
print(last_fruit)
Enter fullscreen mode Exit fullscreen mode

Range of indexes;

You can specify a range of indexes by specifying where to start and where to end the range. The new tuple will be a new list.

partial_fruits = fruits[1:3]
print(partial_fruits)
Enter fullscreen mode Exit fullscreen mode

2. Modifying tuples

By changing the value;

So we will still use the fruits tuple.
fruits = ("apple", "kiwi", "banana")
to change the values you first convert the tuple to a list and then change it back to a tuple, because tuples are immutable.
example;

fruits = ("apple", "kiwi", "banana")
to_list = list(fruits)
to_list[0] = "Mango"
to_tuple = tuple(to_list)
print(to_tuple)

Enter fullscreen mode Exit fullscreen mode

So to check the data type of the tuple you can do that by using the type() method;

This checks if the above code is a tuple or still a list after changing the initial values in our tuple.
print(type(to_tuple))

Adding Items

You can add an item to a tuple by converting it to a list first and then to a tuple, you use the append() method to add the item as the last item.

How it's done;

fruits = ("apple", "kiwi", "banana")
x =  list(fruits)
x.append("orange")
fruits = tuple(x)
print(fruits)
Enter fullscreen mode Exit fullscreen mode

or you can do this;

thistuple = ("apple", "banana", "cherry")
y = ("orange",)
thistuple += y
print(thistuple)
Enter fullscreen mode Exit fullscreen mode

3. Iterating Through Tuples

you can iterate through tuples using the loop or while loop method.

Using For Loop

thistuple = ("apple", "banana", "cherry")
for fruit in thistuple:
 print(fruit)
Enter fullscreen mode Exit fullscreen mode

Looping Through Index Numbers

thistuple = ("apple", "banana", "cherry")
for i in range(len(thistuple)):
 print(thistuple[i])
Enter fullscreen mode Exit fullscreen mode

Using a While Loop

thistuple = ("apple", "banana", "cherry")
i = 0
while i < len(thistuple):
 print(thistuple[i])
 i = i + 1
Enter fullscreen mode Exit fullscreen mode

4. Combining and Multiplying Tuples

Combining Tuples/Adding them together

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

tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)

Enter fullscreen mode Exit fullscreen mode

Multiplying tuples

If you want to multiply the content of a tuple a given number of times, you can use the * operator:

fruits = ("apple", "banana", "cherry")
doubled_fruits = fruits * 2
print(doubled_fruits)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)