Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Python » Python Tuple and Basic Tuple Operations with Examples

  • जन्माष्टमी व्रत पूजा विस्तार से | दही हांडी | Krishna Janmashtami Puja
    जन्माष्टमी व्रत पूजा विस्तार से, दही हांडी: Krishna Janmashtami Puja Festival
  • Prithviraj Chauhan Biography in Hindi | पृथ्वीराज चौहान जीवनी हिंदी
    Prithviraj Chauhan Biography in Hindi | पृथ्वीराज चौहान जीवनी हिंदी Biography
  • States of Matter : Physical Properties of Matter (Solid, Liquid, Gas)
    States of Matter | Physical Properties of Matter (Solid, Liquid, Gas) Science
  • Mahatma Gandhi Essay in Hindi | Gandhiji Biography
    Mahatma Gandhi Essay in Hindi | Gandhiji Biography Biography
  • International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं?
    International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं? General Knowledge
  • Fundamental Duties of Indian Citizens
    Fundamental Duties of Indian Citizens | 11 मौलिक कर्तव्य हिंदी में General Knowledge
  • World Red Cross Day and Red Crescent Day | विश्व रेडक्रॉस दिवस
    World Red Cross Day and Red Crescent Day | विश्व रेडक्रॉस दिवस General Knowledge
  • MS Dhoni (Mahendra singh Dhoni) Cricket Biography in Hindi
    MS Dhoni (Mahendra singh Dhoni) Cricket Biography in Hindi Biography

Python Tuple and Basic Tuple Operations with Examples

Posted on April 30, 2022April 30, 2022 By GeekCer Education No Comments on Python Tuple and Basic Tuple Operations with Examples
Python Tuple and Basic Tuple Operations with Examples

A python tuple is the optimal solution for keeping a collection of objects where the values are arranged in a certain sequence. A tuple is immutable in nature. We use tuples to hold heterogeneous (different) types of data.

The certain sequence means that the elements are arranged in a same order as we defined during creation which will not change.

For the tuple we use parenthesis or round brackets ( and ) and we separate the objects by using comma (,).

The important point to remember is that we can add duplicate values to a tuple, and the items of tuples are indexes, which means we can use the index to retrieve the tuple’s items. The first item’s index is [0], the second item’s index is [1], and so on.

Table of Contents

  • Python tuple example
  • Creating a Tuple in Python
  • Access tuple values in Python
  • Slicing in Tuples
  • Update Values of Tuple
  • Delete values of Tuple
  • Nesting of Tuple in Python
  • Basic Operations in Tuple in Python
    • Repeating tuples in Python
    • Concatenation of Tuples in Python
    • Iteration of Tuple in Python
    • Find the length of tuple in python

Python tuple example

Here is a basic example of a python tuple. We will go through the tuple operations like create, access, delete in detail one by one.

tpl = ("Virat", "Sachin", "AB")
print(tpl)

Creating a Tuple in Python

How to create a tuple in python? We can create a tuple with and without parenthesis. The use of parentheses is optional in python. You must use a comma to separate all of the items (,).


tp_empty = ()
tp1 = (10, "Smith", 20)    
tp2 = ("India", "US", "SA")     
tp3 = 100, 200, 300, 400 
  
print(tp_empty)  
print(tp1)  
print(tp2)  
print(tp3)  

Output:
()
(10, 'Smith', 20)
('India', 'US', 'SA')
(100, 200, 300, 400)

Access tuple values in Python

If you want to access the tuple’s values, use square brackets. If you want to access several values from the tuple, you can use semicolons(:).

Here’s an example that will show you how to get elements and values from a tuple.


tp1 = ('India', 'UK', 'US', 10, 20);
tp2 = (10, 20, 30, 40, 50, 60, 70);

print ("Value of tp1[0]: ", tp1[0]);
print ("Value of tp2[1:4]: ", tp2[1:4]);
print ("Value of tp2[2:4]: ", tp2[3:5]);

Output:
Value of tp1[0]:  India
Value of tp2[1:4]:  (20, 30, 40)
Value of tp2[3:5]:  (40, 50)

Slicing in Tuples


tuple1 = (10 ,11, 12, 13)
print(tuple1[0:])
print(tuple1[1:])
print(tuple1[::-2])
print(tuple1[3:4])

Output:
(10, 11, 12, 13)
(11, 12, 13)
(13, 11)
(13,)

Update Values of Tuple

A tuple, as previously stated, is immutable in nature. We can not update the tuple’s values. However, we can create a new tuple by taking the values of existing tuple.

Let’s look at an example to help you understand the concept.


tp1 = (10, 20, 30, 40, 10.32, 20.22);
tp2 = ("Apple", "Orange", "Banana");

tp3 = tp1 + tp2;
print (tp3);

Output:
(10, 20, 30, 40, 10.32, 20.22, 'Apple', 'Orange', 'Banana')
tp1 = (10, 20, 30, 40, 10.32, 20.22);
# Following action invalid for tuples
# tp1[0] = 10;

Delete values of Tuple

We can’t remove values from a tuple since it’s immutable but we can remove the tuple completely. We use the del keyword with the tuple name to remove a tuple.


tp1 = (10, 20, 30, 40, "Apple", "Orange");
del tp1[1];
print (tp1);

Output:
Traceback (most recent call last):
  File "\geekcer\python\4.py", line 2, in <module>
    del tp1[1];
TypeError: 'tuple' object doesn't support item deletion

tp1 = (10, 20, 30, 40, "Apple", "Orange");
print (tp1);
del tp1;
print (tp1);

Output:
(10, 20, 30, 40, 'Apple', 'Orange')
Traceback (most recent call last):
  File "E:\PythonExercise\30042022\4.py", line 4, in <module>
    print (tp1);
NameError: name 'tp1' is not defined

In the above output you can see the statement “NameError: name ‘tp1’ is not defined“, it means that the tuple has been deleted successfully.

Nesting of Tuple in Python

Here example code for creating nested tuples where two tuple are being taken for nesting.


tp1 = (10, 20, 30, 40);
tp2 = ("Apple", "Orange", "Banana");

tp3 = (tp1, tp2)
print (tp3);

Output:
((10, 20, 30, 40), ('Apple', 'Orange', 'Banana'))

Basic Operations in Tuple in Python

For tuple you can apply many operations like + for concatenation, * for iteration, in for membership etc. One thing to note is that the result will be the new tuple.

Repeating tuples in Python

When we need to repeat a tuple multiple times, we use the repetition operator (*).

<tuple-name> * N

tp1 = (10 ,11, 12)
print(tp1 * 3)

Output:
(10, 11, 12, 10, 11, 12, 10, 11, 12)

Concatenation of Tuples in Python

We use the + operator to concatenate tuples in Python.


tp1 = (10 ,11, 12)
tp2 = ("A", "B", "C")
tp3 = tp1 + tp2;
print(tp3)

Output:
(10, 11, 12, 'A', 'B', 'C')

Iteration of Tuple in Python

We iterate over the tuple elements using a for loop. Here we have an example of iteration of tuple elements.


tp1 = (10 ,11, 12, 13, 14)
for itr in tp1: 
    print(itr)

Output:
10
11
12
13
14

Find the length of tuple in python

The len() function returns the length of the tuple. We use the len() function returns the number of elements from a tuple.


tp1 = (10 ,11, 12, 13, 14, "A", "B")
l = len(tp1);
print("Length is", l)

Output:
Length is 7

Read Also : Basic Operation of Python List

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Python Tags:How to create a tuple in python?, Python tuple index

Post navigation

Previous Post: Python List

More Related Articles

Python Tutorial for Beginners Python Tutorial for Beginners Python
Python Variable and Python Datatype Python Variable and Python Datatype Python
Python List Python List Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Python Home
  • Python Variable and Datatype
  • Python List
  • Python Tuple
  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved