Python Language / Python Tuples

It is a collection which is ordered and unchangeable and written with round brackets.

Sample Programs
Program Output
# Tuple Creation
ourtuple = ('Welcome to', 'Python', ' Language ')
print(ourtuple)
('Welcome to ', ' Python', ' Language ')
#Tuple Creation using tuple() Constructor
ourtuple = tuple(('Welcome to', 'Python', ' Language'))
print(ourtuple)
('Welcome to', 'Python', ' Language')
#Access Tuple Items using index number
ourtuple = ('Welcome to ', ' Python', ' Language ')
print(ourtuple [1])
Python
# After Tuple  creation we cannot Change Tuple Values
ourtuple = ("Welcome to ", " Python", " Language ")
ourtuple [ 1] =  "Java"
print(ourtuple)
'tuple' object does not support item assignment
# Loop aTuple and print its items
ourtuple = ("Welcome to ", " Python", " Language ")
for x in ourtuple:
  print(x)
Welcome to Python Language
#Check if Item Exists/ not in tuple
ourtuple  = ("Welcome to ", "Python", " Language ")
if "Python" in ourtuple :
  print("Yes, 'Python' is in the ourtuple")
Yes, 'Python' is in the ourtuple
#find Tuple Length(Number of items)
ourtuple  = ("Welcome to ", "Python", " Language ")
print(len(ourtuple))
3
# Add Items to tuple cannot
ourtuple  = ("Welcome to ", "Python", " Language ")
ourtuple[3]  = "Java"
print(ourtuple)
'ourtuple' object does not support item assignment
# Remove Items to tuple cannot
ourtuple = ("Welcome to ", "Python", " Language ")
del  ourtuple print(ourtuple)
name 'thistuple' is not defined error will be raise
because the tuple no longer exists


Home     Back