What is tuple in Python with example - Python

What is tuple in Python with example

Hello Guys once again welcome to our new post in this blog you will learn tuple in Python with example. So must check out the full blog also share to everyone.

Tuple is one of the immutable sequence data type supported by Python to handle large volume of data. Tuple is a collection of data items of any type,separated by comma, written within parenthesis. Python provides several functions and methods to access and process the elements of Tuple. This chapter discusses the concepts, functions and methods of Python Tuple.

Introduction: The Tuple is one of the sequence data type Supported by Python. it can be defined as a collection of values of any data type.

Unlike List, Tuple is an immutable data type of Python i.e we cannot change elements of Tuple in place. A Tuple is always depicted in parentheses () or curved brackets. The following are some examples of tuple.



TUPLE in Python

()                                                                               #EmptyTuple

(1,2,3,4,5)                                                               #TUPLE OF Integers

(1.2,2.3,3.4)                                                            #Tuple of floating point

(‘x’,’y’,’z’)                                                                  # Tuple of Characters

(“Delhi”,”mumbai”,”chennai”,”Kolkata”)        #Tuple of Strings

(1, “Rajesh”,12.5,2,”kiran”,45.7)                          # Tuple of Mixed val

How to creating  Tuple in python

In Python, the sequence data type Tuple can be created in three ways. 

  1. By Assigning Values
  2. Through other sequence
  3. Dynamically via Keyboard

Here you will also learn python tuple example 

1) Creating Tuple by assigning values:

To create Tuple by asssignment method, put a number of data values in parentheses and assig it to a variable. The data values in parentheses must be separated by commas and the variable to which it is assigned becomes a Python Tuple.

Syntax  : <tuple name>=(<comma separated number of values>)

Example-1 : num=(23,65,42,82,27,19)

This example will create a Python Tuple by the name ‘num’ with few integers.

Example-2   : tup1=(3)

The above example will create a Tuple with single element. As we can see to create Tuple with single element we add a comma after the single element otherwise Python will store the single

element as simple data into a variable. This mean the following example will not create a Tuple.

   tup=(3)

 print() statement can be used to print the whole Tuple of values, as:

Syntax : print(<tuple name>)

  Example:  print(num)

Q) Write a program to create a Tuple of monthly sale of 5 salesmen. Also print the Tupple. Sales are- 41000,39000,44000,21500,32750.

   msales=(41000,39000,44000,21500,32750)

   print(msales)

2) Creating Tuples from Existing Sequence:

we can create a new Tuple from any exising sequence data type by using a built-in function tuple() in   the following general format. Sequence data type could be String . List or even another Tuple. The tuple() functions usses individual elements of given sequence data to create a new tuple.

 Syntax : <tuple name>=tuple(<sequence>)

Example-1 : tup1=tuple(“PYTHON”)

                print(tup1)

This example is taking a string as sequence type data and creating a tuple using its characters as elements of tuple. Finally, it will print the newly created tuple as

[‘P’,’Y’,’T’,’H’,’O’,’N’]

 Example-2 : tup2=tuple([‘a’,’e’,’i’,’o’,’u’]

                         print(tup2)

This example is taking a Python List as sequence type data and creating a tuple using its element are element of tuple. Finally, it will print newly

created tuple as   (‘a’,’e’,’i’,’o’,’u’)




Q) write a program to create a Tuple using a List containing name of 5 persons. Also print the tuple on screen.

 p=[‘Aditi’,’Bhawna’,’Shivangi’,’Dipika’,’Ekta’]

                nam=tuple(p)

                print(num)

3) Creating Tuple dynamically via keyboard:

Using input() statement within eval() function we can create a Tuple dynamically. The input() statement will take input from the user as a list of values separated by comma and eval() function will separate values as element of Python Tuple. Finally tuple() function will convert these values in a Tuple and store them in the variable provided.In this case user has to input values separating with comma.

Syntax  :  <variable>=eval(input(“<any message>”))

                <tuple name>=tuple(<variable>)

Example : n=eval(input(“Enter tuple elements:”))

              tup=tuple(n)

Q) Write a program to input temperature in 5 cities and create a tuple using them.

   m=eval(input(“Enter temperature in 5 cities”)

   tmptup=tuple(m)

   print(“\nTuple is=”,tmptup)

 Accessing the Tuple:

Python Tuple ia an immutable sequence data type in which elements cannot be edited in its place. Python allow us to access Tuple in both the ways: Whole Tuple at once and individual element one by one.Accessing whole Tuple: Python Tuple is a collection of values of several data types stored with a single name. All the elements of Tuple can do displayed on screen by using Tuple name with print() statement.




Syntax-1 : print(<tuple name>)

Example : marks=(38,41,29,13,25)

           print(marks)

output : (38,41,29,13,25)

Explanation : In the above example we have created a tuple of five subject marks with the name ‘marks’. When we print the tuple name, all the element gets printed on screen.

Syntax-2  :  print(*<tuple name>)

Example   : mrktup=(38,41,29,13,25)

Output : 38 41 29 13 25

Explanation : In the above example we have created a Tuple of five subject marks with the name ‘mrktup’. When we print the tuple name with asterisk sign(*), only the elements get printed on screen without bracket and comma.

Q) write a program to create a tuple of roll no, name and marks obtained  by three students and print all the elements on screen.

student=(1,’Anchal’,58,2,’Somya’,61,3,’Vishal’,52)

                 print(student)

                 print()

                 print(*student)

Accessing individual element:

 Once a Tuple is created and elements are stored in it, the position of each element in the Tuple is assigned a number. The position of element in the Tuple is called Index and index numbering begins with 0. The diagram below shows the index position of element in a Tuple.To access elements of a Tuple individually we write Tuple name followed by index of that element in square bracket in the following format:

 Syntax  :  <tuple name>[<index of element>]

Example : marks[0]

The above example will display first element of Tuple’marks’.

Q) Write a program to create a Tuple of numbers and print all the numbers separately. Also calculate and print their total. 

tot=0

   ntup=(12,65,42,75,26,78,52,48,50,26)

   print(“Forward printing…”)

   for a in range(10):

                   print(ntup[a],” “,end=”)

                   tot=tot+ntup[a]

To Learn Complete Python in 2 Hours Watch our Full Video

Traversing the tuple:

In Tuple Accessing and processing each and every element of the is known Traversing the Tuple. For traversal of the Tuple rather than accessing elements using index number we use for() loop to access elements elements from the List.

Syntax  : for<variable>in<tuple name>:

                          body of loop

                Example : for a in marks:

                                       print(a)




Q) Write a program to create a Tuple of age of 10 persons. While traversing the Tuple print all its elements and count how many of them are Major and Minor.

   mj=mn=0

   age=(15,28,12,40,61,22,14,50,16,61)

   print(“Tuple elements…”)

   for a in age:

                   print(a,” “,end=”)

                   if(a>=18):

                                   mj=mj+1

                    else:

                                   mn=mn+1

print(“\n\nNo.of Major=”,mj)

print(“No.of Minor=”, mn)

What is a tuple vs list?
List is a mutable data type whereas tuple is a immutable data type.In list we can insert,update and delete the element this property represent list is mutable. On the other hand we cannot insert,update and delete the element in tuple so this property represent tuple is immutable data type. So here i clearly provide the explaination about tuple vs list? If you learn more about the list  then click on the link below
Watch Our Full List Video on Youtube Click on the link

Leave a Comment