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