Tuple Functions in Python

Pallavi Dhotre

8 months ago

Tuples in Python | insideAIML
Table of Content
Introduction Python tuples
Tuple operations in python
  • How to create a tuple in python?
  • How to Get Values from tuple?
  • Deleting the element from Python tuple
Tuple functions in python
  • len()
  • max()
  • min()
  • sum()
  • sorted()
  • Python tuples concatenation
Create a list of tuples in python
Summary

Introduction 

          In the previous articles, we have discussed lists and we also used lists in various programs. In real life, there are many situations where we need to store multiple values. In this Python Tuple tutorial, we'll rather take a deeper check out Python tuple. First, let’s check out what a Python tuple is then we'll discuss the way to create, access, delete tuples in Python. Moreover, we'll learn the functions, methods, and operations of Python tuples. Python provides a variety of constructs to affect items. These include python lists, dictionaries, sets, tuples, and lots more. It also supports in-built functions and methods that we will apply to these constructs. In this article, we will learn how to create a list of tuples in Python, write a python program to find the repeated items of a tuple, and know the difference between a list and a tuple.

Python tuples

          Python tuples  are  types  of  data structures  that  can  hold  multiple  values under a single common name. We can define tuple in python as a collection of elements where each element is assigned a unique number known as an index. Entries in the python tuples are known as elements or items.  Python tuples are ordered and immutable which means we cannot change the data in the tuple. You can use tuple if you don’t want to change the data within this.

Tuple operations in python

          There are several operations that we can perform on tuple. 

How to create a tuple in python? 

          To create a tuple in python we use parentheses (round brackets) and elements are comma-separated. We do not use any method to create a python tuple instead we use tuple syntax in python: 
# python tuple example
demo=() #empty tuple
demo=(1,) #tuple with single element
demo=(1,2,3,4) #tuple with integer values
demo=('Ram','Sham','Siya') #tuple of strings
 
If you are adding only one element to the tuple then a comma should be given after the element before closing the round bracket.

How to Get Values from tuple?

          To get an entry from the tuple we use index numbers of the tuple. See the following example for more details:
# python tuple example
demo_tuple=('Ram','Sham','Siya') #tuple of strings
print(demo_tuple[0]) #this will access the first element
print(demo_tuple[1]) #this will access the second element
print(demo_tuple[2]) #this will access the third element
Output:
Ram
Sham
Siya

Deleting the element from Python tuple

          Python tuples are immutable that’s why we cannot delete elements from the tuple. Rather we can delete the entire tuple in the following way:
#example of tuple in python
demo_tuple=('Ram','Sham','Siya') #tuple of strings 
print(demo_tuple) #this will display the python tuple before deleting
del demo_tuple #deleting entire tuple
print(demo_tuple)
Output
('Ram', 'Sham', 'Siya')
Traceback (most recent call last):
  File "C:/Users/Pallavi/PycharmProjects/demo/demo1.py", line 4, in <module>
    print(demo_tuple)
NameError: name 'demo_tuple' is not defined
This program will display the tuple first and then it will give NameError because we have deleted the tuple. 

Tuple functions in python

          Many functions that we use with python lists are used with python tuples. 

len()

          This function returns the length of the tuples. 
a=(10,20,55,44,34,9,6) #first tuple
print(len(a))#display the length of the list
Output
7

max()

          This function displays the maximum element from the given tuple
Code:
a=(12,3,4,44,2,34,6) #tuple
print(max(a)) #display max element from the tuple
output
44

min()

          This function displays the minimum element from the given tuple
Code:
a=(12,3,4,44,2,34,6) #tuple
print(min(a)) #display min element from the tuple
output
2

sum()

          This function returns the total of all the elements within the tuple.
Code:
a=(12,3,4,44,2,34,6) #tuple
print(sum(a)) #display sum of the elements in the tuple
Output
105

sorted()

          This method returns the sorted version of python tuple.
Code
a=(12,3,4,44,2,34,6) #tuple
print(sorted(a)) #display sorted tuple
Output
[2, 3, 4, 6, 12, 34, 44]

Python tuples concatenation 

          Python tuples are concatenated using the + operator and replication is done using the * operator. Example code is given below
#example of tuple in python
a=(10,20,55,44,34,9,6) #first tuple
b=(1,2,3) # second tuple
print(a+b) #python tuples concatenation
print(b*4) #python tuples replication
output
(10, 20, 55, 44, 34, 9, 6, 1, 2, 3)

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

Create a list of tuples in python

          Elements of the tuple are enclosed within the square bracket (list) this approach is known as a list of tuples in python. A list of tuples follows the same characteristics as a list in python.  let’s take a deeper check out with the program
listOfTuple=[(1,2,4),('ram','sham')]
print(listOfTuple)
Output
[(1, 2, 4), ('ram', 'sham')]
Write a python program to find the repeated items of a tuple
#write a python program to find the repeated items of a tuple
demo_tuple=(1,2,3,1,4,3,4,5,2,1,) #tuple
print(demo_tuple) #display tuple
c=demo_tuple.count(1) #count the repeted item
print(c) #display number of occurances
Output
(1, 2, 3, 1, 4, 3, 4, 5, 2, 1)

3

Difference between list and tuple

Difference between list and tuple | insideAIML

Summary

          In this article, we discussed python tuples, how to create a tuple in Python, tuple operations in python, tuple functions in python, and a list of tuples, and the difference between list and tuple in python.  We can define tuple in python as a collection of elements where each element is assigned a unique number known as an index. Entries in the python tuples are known as elements or items.  Python tuples are ordered and immutable. We hope you enjoyed the article. If you have any related queries, feel free to ask in the comment section below. 
      
Enjoyed reading this blog? Then why not share it with others. Help us make this AI community stronger. 
To learn more about such concepts related to Artificial Intelligence, visit our blog page.
You can also ask direct queries related to Artificial Intelligence, Deep Learning, Data Science and Machine Learning on our live discussion forum.
Keep Learning. Keep Growing. 
    

Submit Review