Wednesday, July 8, 2015

List & Tuples

List are mutable sequences, means we can change elements once declared. List creation syntax is given here.

>>> li = [1,2,3]
>>>li[2] = 5     #  possible in Lists

Tuples are immutable sequences, means tuples forms a constant list, once created we are unable to change the tuple elements.

>>> tp = (1,2,3)
>>> tp[2]
>>> 3     # it prints the 3rd element in tuple
>>> tp[2] = 5 # if you try to change the element of tuple will throw you a an error 
Traceback (most recent call last):
  File "<pyshell#25>", line 1, in <module>
    tp[2] = 5
TypeError: 'tuple' object does not support item assignment
>>>

No comments:

Post a Comment