Articles → Python → Tuples in python

Tuples in python






What are tuples?





Syntax


Variable_name = (element1,element 2,…..)



Example




classmates = ("Karan","sumit","amit")

print(classmates)

classmates[1] = "Tarun"

print(classmates)














('Karan', 'sumit', 'amit')
Traceback (most recent call last):
  File "C://python/datatype.py", line 5, in <module>
    classmates[1] = "Tarun"
TypeError: 'tuple' object does not support item assignment





Looping through all the elements in tuple




tuple = ("Banana","Orange","Grapes","Straw berry")

x= 0

while x < 4:
    print(tuple[x])
    x = x + 1





Click to Enlarge


Specifying the range




tuple = ("Banana","Orange","Grapes","Straw berry")

print(tuple[1:3])





Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Thursday, February 14, 2019
 
Updated On  -  Thursday, February 21, 2019

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250