lists.org
changeset 114 751cb19549c8
child 116 8b650688f4e1
equal deleted inserted replaced
113:6388eacf7502 114:751cb19549c8
       
     1 * Lists
       
     2 *** Outline
       
     3 ***** Lists
       
     4 ***** Tuples
       
     5 ***** Arsenal Required
       
     6 *** Script
       
     7 	Welcome friends. 
       
     8 
       
     9 	In this tutorial we shall look at Data structures supported by
       
    10 	Python.	We shall look at Lists and Tuples. We have already seen
       
    11 	lists in some of previous sessions, here we shall cover more 
       
    12 	details about them.
       
    13 
       
    14 	The list type is a container that holds a number of other 
       
    15 	objects, in a given order. The list type implements the sequence
       
    16 	protocol, and also allows you to add and remove objects from 
       
    17 	the sequence. 
       
    18 	
       
    19 	First lets start the interpreter by typing ipython in terminal.
       
    20 	We create our first list by typing 
       
    21 	num = [1, 2, 3, 4]
       
    22 	Items enclosed in square brackets separated by comma 
       
    23 	constitutes a list.
       
    24 	One neat feature of Python list is we can store data belonging
       
    25 	to multiple kind of data structures. We can have a list with 
       
    26 	something:
       
    27 	var = [1, 1.2, 'string']
       
    28 	print var
       
    29 	and with this list we can perform most of list operations.
       
    30 	Python lists are very versatile, that is we can change it as we 
       
    31 	wish. It supports features like removal, addition. 
       
    32 
       
    33 	Similar to strings, we can concatenate two lists using '+' 
       
    34 	operator
       
    35 	so num + var will return a new list with 'var' added in end of
       
    36 	'num'
       
    37 	We have already covered append functionality.
       
    38 	To add single object at end of list 'append' function is used
       
    39 	num
       
    40 	num.append(-5)
       
    41 	num
       
    42 	append takes only one argument. And append behaves different 
       
    43 	from + operator. While + will return new list with two lists 
       
    44 	added if we try similar with append function like:
       
    45 	num.append([9, 10, 11])
       
    46 	num
       
    47 	It changes original list and add the argument as one element
       
    48 	and not separate elements.
       
    49 	To extend list with new list elements we use 'extend' function
       
    50 	num = [1, 4, -6]
       
    51 	num.extend([2, 8, 0])
       
    52 	num
       
    53 	As we can notice extend and append behaves differently.
       
    54 	To reverse a list 'reverse' function is available. 
       
    55 	num
       
    56 	This is current content of list
       
    57 	num.reverse()
       
    58 	Now after using reverse function, lets check the value of 'num'
       
    59 	num
       
    60 	To remove a particular element from list Python provides remove
       
    61 	num.reverse(8)
       
    62 	if argument is present more than once, then first occurrence is
       
    63 	removed from list.
       
    64 
       
    65 	Slicing and Striding concepts which we covered for Arrays work
       
    66 	with lists also. Lets revisit the concept with some examples
       
    67 	a = [1, 2, 3, 4, 5]
       
    68 	a[1:3] returns a list with second and third element of a
       
    69 	One important feature of list indexing is negative values. In
       
    70 	Lists -1 indicates last element of a list
       
    71 	a[-1]
       
    72 	similarly -2 will be second last and so forth. Now these 
       
    73 	negative indexes can also be used with slicing. If we try
       
    74 	a[1:-1]
       
    75 	we get list which excludes first and last element of a.
       
    76 	and if we miss start/end index value default are taken
       
    77 	a[:3] will return list from start till fourth element of a.
       
    78 	If we give step size we can do striding also
       
    79 	a[1:-1:2]
       
    80 	This gives second, fourth and so on items of a till we reach 
       
    81 	last item of list.
       
    82 	a[::2] will skip all the even placed elements of a
       
    83 	With step sizes, if we give negative values we get some 
       
    84 	interesting results. Lets try
       
    85 	a[::-1]
       
    86 	It returns reversed 'a'
       
    87 	We can check for containership also with lists
       
    88 	num
       
    89 	4 in a
       
    90 	True
       
    91 	
       
    92 	Python provides support to special immutable lists known as
       
    93 	'tuple'	To create a tuple instead of square brackets'[' we have
       
    94 	to use normal brackets '('
       
    95 	t = (1, 2, 3, 4, 5, 6, 7, 8)
       
    96 	its elements can also be accessed using indexes
       
    97 	t[0] + t[3] + t[-1]
       
    98 	but operation like
       
    99 	t[4] = 7 are not allowed
       
   100 	These features of tuples have their advantages. To see where 
       
   101 	are they used we first create two variables
       
   102 	a, b = 1, 6
       
   103 	print a, b
       
   104 	Now lets swap values their values. Normal approach would be 
       
   105 	to create a temporary to hold the value but because of tuples
       
   106 	we can do something cool like
       
   107 	b, a = a, b
       
   108 	print a, b
       
   109 	and values are swapped. And this swapping works for all types
       
   110 	of variables.
       
   111 	
       
   112 	With this we come to the end of this tutorial on Lists and 
       
   113 	tuples. In this tutorial we have learnt some more operations 
       
   114 	on lists and tuples. In next session we will cover more on 
       
   115 	Python supported data structures. Thank you!
       
   116 
       
   117 *** Notes