Table of contents
1.
Introduction
2.
What are Tuples in Python?
3.
How to Create a tuple?
3.1.
Python
3.2.
Python
3.3.
Python
4.
Operations in Python Tuple
4.1.
Accessing of Python Tuples
4.2.
Python
4.3.
Concatenation in a tuple
4.4.
Python
4.5.
Deleting a tuple
4.6.
Python
4.7.
Slicing in a tuple
4.8.
Python
5.
Finding the length of the Python tuple
5.1.
Python
6.
Tuples in a loop
6.1.
Python
7.
Inbuilt Functions of Python Tuples
7.1.
1. type()
7.2.
Python
7.3.
2. cmp()
7.4.
Python
7.5.
3. max() 
7.6.
Python
7.7.
4. min()
7.8.
Python
8.
Functions of Tuples in Python
9.
Frequently Asked Questions
9.1.
How are lists different from tuples?
9.2.
What is the performance difference between lists and tuples?
9.3.
Why use tuples in Python?
9.4.
What are tuple methods in Python?
10.
Conclusion
Last Updated: Nov 1, 2024
Easy

Python Tuples

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Python programming, data structures play a crucial role in organizing and managing information efficiently. Among these structures, tuples stand out as a powerful and versatile option. A tuple is a collection type that allows you to store an ordered sequence of elements, which can be of different data types, including numbers, strings, and even other collections. Unlike lists, tuples are immutable, meaning once created, their contents cannot be altered. This characteristic not only ensures data integrity but also enhances performance in certain scenarios. In this blog, we will explore the fundamentals of Python tuples.

Python Tuples

What are Tuples in Python?

Tuples in Python are immutable sequences that can hold a collection of heterogeneous items. Defined by enclosing elements in parentheses, tuples allow for data storage without modification, making them useful for fixed data sets and as dictionary keys due to their hashability.

Tuples store multiple items separated by commas in a single variable. Items are written with round brackets. There are various properties of tuples that should be considered while creating or performing the operations.

Unchangeable- Tuples are immutable, which means we cannot change or add the items after creating the tuple.

Ordered- means that items are defined in an order that will not change after the insertion operation.

Heterogenous- A single tuple variable can contain different data types.

Contains Duplicates- Allows to store duplicate data items.

Also see, Merge Sort Python

How to Create a tuple?

To create a tuple in Python, enclose elements in parentheses, separating them with commas. For example: my_tuple = (1, 'apple', 3.14) creates a tuple with three elements.

We can create an empty tuple in python by writing nothing in the parentheses.

Example 1

Input:

  • Python

Python

tup1 = ()
print(tup1)
You can also try this code with Online Python Compiler
Run Code

Output

()

 

To create a tuple with a single element, we have to insert a comma at the end.

 

Example 2

Input:

  • Python

Python

tup1 = (“Hello”,)
print(type(thistuple))
You can also try this code with Online Python Compiler
Run Code

Output

 

<class 'tuple'>

 

To create a tuple with multiple data type values in one variable.

 

Example 3

Input:

  • Python

Python

tup1 = ("ab", 314, True, 40, "Hello")
print(tup1)
You can also try this code with Online Python Compiler
Run Code

Output

 

('ab', 314, True, 40, 'Hello')

 

You can compile it with online python compiler.

Operations in Python Tuple

The operation of Tuples in Python are as follows

  1. Accessing of Python Tuples
  2. Concatenation of Tuples
  3. Slicing of Tuple
  4. Deleting a Tuple 

Accessing of Python Tuples

We can assess the elements of the tuple using square brackets. Let's go through the below example for improving your visualization.

 

Example

Input:

  • Python

Python

tup1 = ('Hello', 'Wolrd', 20)
print tup1[0]
print tup1[2]
You can also try this code with Online Python Compiler
Run Code

Output

 

Hello
20

Concatenation in a tuple

To concatenate two tuples, we can use the ‘+’ operator.

Example

Input:

  • Python

Python

tup1 = ('Coding', 'Ninjas')
tup2 = ('Hello', 1)
print(tup1+tup2)
You can also try this code with Online Python Compiler
Run Code

Output

(Coding, Ninjas, Hello, 1)

Deleting a tuple

Removing elements from the tuple is not possible. So to remove an entire tuple explicitly, use the del statement.

 

Example

Input:

  • Python

Python

tup1 = (‘Hello’, 'World', 20)
print tup1;
del tup1;
print tup1;
You can also try this code with Online Python Compiler
Run Code

Output

 

(‘Hello’, 'World', 20)
Traceback (most recent call last):
   File "example.py", line 9, in <module>
      print tup1;
NameError: name 'tup1' is not defined

Also see, Python Filter Function

Slicing in a tuple

We can access the elements of a particular index using slicing. It is used to fetch a range of items. Slicing can be represented in the below format:

[start:stop: step], where step part is not necessary to be mentioned The compiler considers it '1' by default if we do not mention the step part.

 

Example 1

Input:

tup = (22, 3, 45, 4, 2, 56, 890, 1)
print(tup[1:5])
You can also try this code with Online Python Compiler
Run Code

 

Output

(3, 45, 4, 2)

 

Example 2

Input:

  • Python

Python

tup = (22, 3, 45, 4, 2, 56, 890, 1)
print(tup[:3])
You can also try this code with Online Python Compiler
Run Code

Output

(22, 3, 45)

Finding the length of the Python tuple

To determine the length of the tuple, python language provides us with the len() function.

Example

Input

  • Python

Python

tup = (22, 3, 45, 4, 2, 56, 890, 1)
print(len(tup))
You can also try this code with Online Python Compiler
Run Code

Output

8

 

Tuples in a loop

We can loop through the tuples using the 'for' or 'while' loop.

 

Example

Input:

  • Python

Python

tup1 = (1, 2, 3)
n = 3
for i in range(int(n)):
print(tup1)
You can also try this code with Online Python Compiler
Run Code

Output

(1, 2, 3)
(1, 2, 3)
(1, 2, 3)

 

Must Read Python List Operations

Inbuilt Functions of Python Tuples

Here are some Inbuilt Functions of Python Tuples:

  1. type()
  2. cmp()
  3. max()
  4. min()

1. type()

Tuples are defined as objects with the data type 'tuple':

Example

Input:

  • Python

Python

tup1 = ("Coding", "Ninjas")
print(type(tup1))
You can also try this code with Online Python Compiler
Run Code

Output

<class 'tuple'>

 

2. cmp()

It compares the elements of both the tuples.

 

Example:

Input:

  • Python

Python

tup1 = ('Coding', 'Ninjas')
tup2 = ('Hello', 1)

if (cmp(tup1, tup2) != 0):
print('Not same')
else:
print('Same')
You can also try this code with Online Python Compiler
Run Code

Output

Not same

 

3. max() 

It returns the item with the max value in the tuple.

 

Example

Input:

  • Python

Python

tup1 = ('Coding', 'Ninjas')
tup2 = ('Hello', 1)
print ( str(max(tup1)))
You can also try this code with Online Python Compiler
Run Code

Output

Ninjas 

 

4. min()

It returns the item with the max value in the tuple.

 

Example

Input:

  • Python

Python

tup1 = ('Coding', 'Ninjas')
tup2 = ('Hello', 1)
print ( str(min(tup2)))
You can also try this code with Online Python Compiler
Run Code

Output

1Functions of Tuples in Python

Functions of Tuples in Python

FunctionsDescription
count(value)Returns the number of occurrences of the specified value in the tuple.
index(value)Returns the first index of the specified value in the tuple. Raises an error if the value is not found.
len(tuple)Returns the number of elements in the tuple.
max(tuple)Returns the maximum value from the tuple.
min(tuple)Returns the minimum value from the tuple.
tuple(iterable)Converts an iterable (like a list or string) into a tuple.

Frequently Asked Questions

How are lists different from tuples?

Both lists and tuples are the collection data types of the python language, but the main difference is that tuples are immutable and lists are mutable.

What is the performance difference between lists and tuples?

Tuples are generally faster than lists when it comes to iteration and access due to their immutability, which allows for optimized memory usage. Lists, being mutable, require additional overhead for dynamic resizing and element modification, impacting performance.

Why use tuples in Python?

Tuples are used in Python for their immutability, which ensures data integrity and prevents accidental changes. They are also more memory-efficient than lists, making them ideal for fixed collections of items, such as coordinates or records.

What are tuple methods in Python?

Tuple methods in Python include count(value), which returns the number of occurrences of a specified value, and index(value), which returns the first index of a specified value. These methods facilitate efficient data retrieval within tuples.

Conclusion

In this blog, we have covered the Python tuples. They are a fundamental and versatile data structure that offers significant advantages, including immutability, memory efficiency, and ease of use. Their ability to store ordered collections of diverse data types makes them ideal for various applications, from representing simple records to serving as keys in dictionaries. Understanding how to effectively utilize tuples, along with their built-in methods, can enhance your programming skills and improve the performance of your applications.

You can also consider our paid courses such as DSA in Python to give your career an edge over others!

Live masterclass