tuple in Python

Tuple is a immutable object in Python.

Declaring a tuple

Using constructor and getting the object data type by using type()
my_tuple=tuple()
print(type(my_tuple))
Output: (There is no point in creating blank tuple as we can't add data.)
<class 'tuple'>

Tuple with data

my_tuple=('Alex','Ronald','John')
print(my_tuple)
Output
('Alex', 'Ronald', 'John')

  • Video Tutorial on Tuple


Creating a tuple by using data from a range()
x=range(5,50,10)
my_tuple=tuple(x)
print(my_tuple)
Output
(5, 15, 25, 35, 45)

Unpack a tuple

my_tuple=(4,2,5,1,3)
print(*my_tuple)
Output
4 2 5 1 3

Displaying all items by looping

We used for loop to display all items of the tuple.
my_tuple=('Alex','Ronald','John')
for i in my_tuple:
    print(i)
Output is here
Alex
Ronald
John

Position of elements in a tuple

The first position is 0th position. The end position is before the given value. Displaying elements based on position
my_tuple=("Alex","Ronald","John")
print(my_tuple[1])
Output is here
Ronald
The position starts from 0 and first element value is my_tuple[0], so the data at my_tuple[1] gives us Ronald.
my_tuple=('a','c','d','c','x','k','d','c')
print("first position : ",my_tuple[0])
print("Last  position : ",my_tuple[-1])
print("third position : ",my_tuple[2])
print("All from 2nd postion : ",my_tuple[1:])
print("All from first position : ",my_tuple[0:])
print("All from 3rd position : ",my_tuple[2:])
print("All from 4th position to 6th position : ",my_tuple[3:6])
Output
first position :  a
Last  position :  c
third position :  d
All from 2nd postion :  ('c', 'd', 'c', 'x', 'k', 'd', 'c')
All from first position :  ('a', 'c', 'd', 'c', 'x', 'k', 'd', 'c')
All from 3rd position :  ('d', 'c', 'x', 'k', 'd', 'c')
All from 4th position to 6th position :  ('c', 'x', 'k')

Single element tuple

We used type() to get the data type of the object. Note that we must use one trailing comma when there is only one element in our tuple.
my_tuple=('abc') # not a tuple 
print(type(my_tuple))
my_tuple=('abc',) # it is a tuple
print(type(my_tuple))
Output
<class 'str'>
<class 'tuple'>

Add , remove , update elements

We can't add or remove elements of tuple. There is no append() or insert() method in tuple
AttributeError: 'tuple' object has no attribute 'append'
Tuples are immutable i.e we can't change the elements.

However we can use + operator to add element but that will create a new tuple. We can check this by using id() . After adding we will get new id for the tuple ( object )
my_tuple=(5,7,5,4,3,5) 
print(id(my_tuple)) # 139787173397704
my_tuple +=(9,8)
print(id(my_tuple)) # 139787174601112
print(my_tuple)     #(5, 7, 5, 4, 3, 5, 9, 8)

Searching element

my_tuple=('Alex','Ronald','John')
if 'Ronald' in my_tuple:
 print("Yes, Present in Tuple")
else:
 print("No , not present in Tuple")
Output is here
Yes, Present in Tuple
We can use len(), max(), min(),sum() functions using a tuple as input.
my_tuple=(8,2,4,3,7,3)
print("Maximum value : ",max(my_tuple))
print("Minimum value : ",min(my_tuple))
print("Sum of all values : ",sum(my_tuple))
print("Number of elements : ",len(my_tuple))
Output
Maximum value :  8
Minimum value :  2
Sum of all values :  27
Number of elements :  6

Number of elements in the tuple ( string elements )

my_tuple=('Alex','Ronald','John')
print("Number of elements: ",len(my_tuple))
Output is here
Number of elements: 3

Deleting tuple

my_tuple=('Alex','Ronald','John')
del my_tuple
print("Number of elements: ",len(my_tuple))
The last line will create error as we are deleting the tuple before this line. Read more on del keyword.

Methods

Tuple supprts two methods count(x) and index(x). Here x the element or value to check.

Number of occurrence

count()
my_tuple=(5,7,5,4,3,5)
print ("Number of times 5 is : ",my_tuple.count(5) )
Output is here
Number of times 5 is :  3

Position of element

index()
my_tuple=(5,7,5,4,3,5)
print ("Position of 3 is  : ",my_tuple.index(3) )
Output
Position of 3 is  :  4
Python Questions on Tuple with solutions



plus2net.com



Post your comments , suggestion , error , requirements etc here





Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer