Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Operations
3.
Creating a tuple
4.
Accessing of elements in the tuple
5.
Deleting a tuple
6.
Slicing in a tuple
7.
Concatenation in a tuple
8.
Finding the length of the tuple
9.
Tuples in a loop
10.
Inbuilt Functions of Python
10.1.
type()
10.2.
cmp()
10.3.
max() 
10.4.
min()
10.5.
Check out this article - String slicing in Python
11.
FAQ'S
12.
Key Takeaways
Last Updated: Mar 27, 2024

Introduction to Python Tuples

Introduction

Python is one of the most famous and used programming languages of the coding hub used on a server to create applications.

Here we will be discussing Python data type, i.e., Tuples. Tuples are one of the four built-in data types that store the data with different capabilities.

 

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.

 

Representation

 

Also see, Merge Sort Python

Operations

We will be heading towards some of the operations performed in the tuple, like concatenation, deleting, etc.

 

Creating a tuple

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

Example 1

Input:

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

 

Output

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

 

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

 

Example 2

Input:

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

 

Output

 

<class 'tuple'>
You can also try this code with Online Python Compiler
Run Code

 

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

 

Example 3

Input:

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 also try this code with Online Python Compiler
Run Code

 

You can compile it with online python compiler.

Accessing of elements in the tuple

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

 

Example

Input:

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
You can also try this code with Online Python Compiler
Run Code

 

Deleting a tuple

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

 

Example

Input:

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
You can also try this code with Online Python Compiler
Run Code

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)
You can also try this code with Online Python Compiler
Run Code

 

Example 2

Input:

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)
You can also try this code with Online Python Compiler
Run Code

Concatenation in a tuple

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

Example

Input:

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)
You can also try this code with Online Python Compiler
Run Code

 

Finding the length of the tuple

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

 

Example

Input

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
You can also try this code with Online Python Compiler
Run Code

 

Tuples in a loop

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

 

Example

Input:

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)
You can also try this code with Online Python Compiler
Run Code

 

Must Read Python List Operations

Inbuilt Functions of Python

type()

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

 

Example

Input:

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

 

Output

<class 'tuple'>
You can also try this code with Online Python Compiler
Run Code

 

cmp()

It compares the elements of both the tuples.

 

Example:

Input:

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
You can also try this code with Online Python Compiler
Run Code

 

max() 

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

 

Example

Input:

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

 

Output

Ninjas 
You can also try this code with Online Python Compiler
Run Code

 

min()

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

 

Example

Input:

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

 

Output

1
You can also try this code with Online Python Compiler
Run Code

 

Check out this article - String slicing in Python

FAQ'S

1). What do you mean by a tuple?

A tuple is a built-in data type that stores multiple items in a single variable. Tuples show the immutable property, which means they can't be changed or modified.

 

2). 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.

 

3). What do you mean by immutability?

Immutable means tuples can't be changed or modified.

 

4). Name the different built-in data types in Python language.

There are four inbuilt data types in Python language, namely:

  • Lists
  • Tuples
  • Dictionaries
  • Sets

 

Key Takeaways

In this blog, we have covered the following things:

  • What do you mean by python language?
  • What is the meaning of a tuple
  • Operations performed in a tuple
  • Inbuilt functions like max(), min(), etc.

 

For more clarity and details regarding the Python language, one can refer to this article.

 

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

 

Happy Coding!!!

 

 

 

Live masterclass