Tuples in Python

Tuples : 

Tuples in a python are nothing just a list in Python. Tuples posses same property as list except for one thing which is it is not mutable and instead of square brackets, it is represented using Parenthesis. 

Example :

  • a = ( ) #empty Tup
Tuples in Python

Tuples : 

  • Creating tuples in python is faster than creating a list in python. 
  • For competitive coding, wherever we need a list we can use a tuple if there is no need for modification of an element in the list.
  • It consumes less memory than a list. 
  • We can not delete an element from the tuple but we have some hacks to perform this operation.  
  • We can edit or change any element of the tuple. 
  • If we try to modify an element of the tuple, we will get a compilation error.
  • Sometime python get TLE, in that case, we can use a tuple instead of a list.

Code #1 :

#Python Program 
#rishikesh 
#creating a empty Tuple
a=()
print(a)
#we can add diffrent type of data type in a single tuple 
a=(1,2,‘3’)
print(‘Elements of tuple ‘  ,a)

Output : 

()
Elements of tuple (1, 2, '3')

Code #2 (creating Tuple) :

#Python Program 
#rishikesh 
#creating a  Tuple
a=1,2,‘3’
print(‘first method : ‘ ,a)
# 2nd method
a=(1,2,‘3’)
print(‘2nd method of :’  ,a)

Output :

first method : (1, 2, '3')
2nd method of : (1, 2, '3')

Code #3 : ( adding two tuples to form single tuple)

#Python Program 
#rishikesh 
#add two tuples to form a single tuple 
a=(‘P’,‘r’,‘e’,‘p’)
print(‘first Tuple : ‘ ,a)
# 2nd Tuple
b=(‘i’ , ‘n’ , ‘s’ , ‘t’ , ‘a’)
print(‘2nd Tuple :’  ,b) 

#final Tuple 
print(‘Final Tuple :’,(a+b))

Output : 

first Tuple : ('P', 'r', 'e', 'p')
2nd Tuple : ('i', 'n', 's', 't', 'a')
Final Tuple : ('P', 'r', 'e', 'p', 'i', 'n', 's', 't', 'a')

Code #4 ( nested tuple) :

#Python Program 
#rishikesh 
#nested tuple 
a=(‘P’,‘r’,‘e’,‘p’)
print(‘first Tuple : ‘ ,a)
# 2nd Tuple
b=(‘i’ , ‘n’ , ‘s’ , ‘t’ , ‘a’)
print(‘2nd Tuple :’  ,b) 

#final Tuple 
c=(a,b)
print(‘Nested Tuple :’,c) 

#2nd method 
d=((‘a’,‘b’),(‘b’,‘c’))
print(‘Nested Tuple:’,d)

Output : 

first Tuple : ('P', 'r', 'e', 'p')
2nd Tuple : ('i', 'n', 's', 't', 'a')
Nested Tuple : (('P', 'r', 'e', 'p'), ('i', 'n', 's', 't', 'a'))
Nested Tuple: (('a', 'b'), ('b', 'c'))

Code #5 ( Slicing of Tuple) :

#Python Program 
#rishikesh 
#nested tuple 
a=(‘P’,‘r’,‘e’,‘p’,‘I’,‘n’,‘S’,‘t’,‘a’)
print(‘complete Tuple :’,*a,sep=

 

#slice Tuple 
print(‘Sliced Tuple’ , a[4:])

 

#reversed Tuple
print(‘Revesrse Tuple :’ , a[::-1])




Output :

complete Tuple :PrepInSta
Sliced Tuple ('I', 'n', 'S', 't', 'a')
Revesrse Tuple : ('a', 't', 'S', 'n', 'I', 'p', 'e', 'r', 'P')

Code # 6 ( Other Methods on Tuple ): 

min, max, convert into a list, compare, delete

#Python Program 
#rishikesh 
#Other MEthods 
a=(‘P’,‘r’,‘e’,‘p’,‘I’,‘n’,‘S’,‘t’,‘a’)

#max 
print(‘Max element :’ , max(a))

#min 
print(‘Min of the Tuple :’ , min(a))

#comp 
b=(1,2,3,4)
if(a!=b):
    print(‘Not same’
#len
print(‘Length of tuple :’ , len(a))
#convert to list 
print(‘list:-‘ , list(b))
#del tuple 
del a 



Output :

Max element : t
Min of the Tuple : I
Not same
Length of tuple : 9
list:- [1, 2, 3, 4]