Tuple

  1. A Tuple is a collection of Python objects separated by commas.

  2. In someways a tuple is similar to a list in terms of indexing, nested objects and repetition but a tuple is immutable unlike lists which are mutable.

Example - 1:

# Creating non-empty tuples 
  
# One way of creation 
tup = 'python', 'gyansetu'
print(tup) 
  
# Another for doing the same 
tup = ('python', 'gyansetu') 
print(tup) 

('python', 'gyansetu')

('python', 'gyansetu')

Example - 2:

t = ( 5,6,7)
print(t)
print(type(t))

(5, 6, 7)

<class 'tuple'>

Example - 3:

TypeError Traceback (most recent call last)

in () 2 3 tuple1 = (0, 1, 2, 3) ----> 4 tuple1[0] = 4 5 print(tuple1)

TypeError: 'tuple' object does not support item assignment

Example - 4:

('hi', 'hello')

('bye', 1, 2, 3)

('hi', 'hello', 'bye', 1, 2, 3)

[1, 2, 3]

[4, 5, 6]

[1, 2, 3, 4, 5, 6]

Example - 5:

(1, 2, 3)

(3, 2, 1, 0)

(2, 3)

Example - 6:

2

Example - 7:

(0, 1, 2)

('p', 'y', 't', 'h', 'o', 'n')

Last updated

Was this helpful?