Tuple is defined as an immutable list of data items. Once created its items can't be altered. So basically we use it in some specific situation. Pythofrank


 Python - List


Tuple is defined as an mutable list of data items. We can modify the items of a list after its creation, and this is one of the important difference between tuple and list .



  """
      created_at       : 16 Feb 2017.
      updated_at       : 16 Feb 2017.
      aim of script    : To understand the concept of lists in Python.
      coded by         : Rishikesh Agrawani.
      Python version   : 2.7.12
  """
  

 #Interactive mode programming - #1

>>> l = [1,2,3,4,5] >>> >>> print l [1, 2, 3, 4, 5] >>> >>> type(l) <type 'list'> >>> >>> l[0] 1 >>> l[4] 5 >>> l[4] = 67 >>> >>> print l [1, 2, 3, 4, 67] >>> >>> l.append(69) #Apending item to list >>> >>> print l [1, 2, 3, 4, 67, 69] >>> >>> print l.remove(4) #Removing item from list None >>> >>> print l [1, 2, 3, 67, 69] >>> >>> print l.remove(333) Traceback (most recent call last): File "", line 1, in ValueError: list.remove(x): x not in list >>> >>> print l.pop() #Popping out item from list 69 >>> >>> print l [1, 2, 3, 67] >>> >>> print l.pop() 67 >>> print l.pop() 3 >>> print l.pop() 2 >>> print l.pop() 1 >>> print l.pop() Traceback (most recent call last): File "", line 1, in IndexError: pop from empty list >>>

 #Interactive mode programming - #2 (ways of creating list)

>>> l = xrange(5,11) >>> l xrange(5, 11) >>> >>> l = list(l) >>> l [5, 6, 7, 8, 9, 10] >>> >>> l2 = range(7,16) >>> l2 [7, 8, 9, 10, 11, 12, 13, 14, 15] >>> >>> l3 = [item for item in l2] >>> l3 [7, 8, 9, 10, 11, 12, 13, 14, 15] >>> >>> id(l) 4430567688 >>> >>> id(l2) 4430196960 >>> >>> id(l3) #focus here 4430567472 >>> >>> l4=l3 #focus here >>> id(l4) 4430567472 >>> >>> l5=l3[:] #focus here >>> id(l5) 4430567112 >>>