Added lists.org.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lists.org Tue Apr 27 16:47:49 2010 +0530
@@ -0,0 +1,117 @@
+* Lists
+*** Outline
+***** Lists
+***** Tuples
+***** Arsenal Required
+*** Script
+ Welcome friends.
+
+ In this tutorial we shall look at Data structures supported by
+ Python. We shall look at Lists and Tuples. We have already seen
+ lists in some of previous sessions, here we shall cover more
+ details about them.
+
+ The list type is a container that holds a number of other
+ objects, in a given order. The list type implements the sequence
+ protocol, and also allows you to add and remove objects from
+ the sequence.
+
+ First lets start the interpreter by typing ipython in terminal.
+ We create our first list by typing
+ num = [1, 2, 3, 4]
+ Items enclosed in square brackets separated by comma
+ constitutes a list.
+ One neat feature of Python list is we can store data belonging
+ to multiple kind of data structures. We can have a list with
+ something:
+ var = [1, 1.2, 'string']
+ print var
+ and with this list we can perform most of list operations.
+ Python lists are very versatile, that is we can change it as we
+ wish. It supports features like removal, addition.
+
+ Similar to strings, we can concatenate two lists using '+'
+ operator
+ so num + var will return a new list with 'var' added in end of
+ 'num'
+ We have already covered append functionality.
+ To add single object at end of list 'append' function is used
+ num
+ num.append(-5)
+ num
+ append takes only one argument. And append behaves different
+ from + operator. While + will return new list with two lists
+ added if we try similar with append function like:
+ num.append([9, 10, 11])
+ num
+ It changes original list and add the argument as one element
+ and not separate elements.
+ To extend list with new list elements we use 'extend' function
+ num = [1, 4, -6]
+ num.extend([2, 8, 0])
+ num
+ As we can notice extend and append behaves differently.
+ To reverse a list 'reverse' function is available.
+ num
+ This is current content of list
+ num.reverse()
+ Now after using reverse function, lets check the value of 'num'
+ num
+ To remove a particular element from list Python provides remove
+ num.reverse(8)
+ if argument is present more than once, then first occurrence is
+ removed from list.
+
+ Slicing and Striding concepts which we covered for Arrays work
+ with lists also. Lets revisit the concept with some examples
+ a = [1, 2, 3, 4, 5]
+ a[1:3] returns a list with second and third element of a
+ One important feature of list indexing is negative values. In
+ Lists -1 indicates last element of a list
+ a[-1]
+ similarly -2 will be second last and so forth. Now these
+ negative indexes can also be used with slicing. If we try
+ a[1:-1]
+ we get list which excludes first and last element of a.
+ and if we miss start/end index value default are taken
+ a[:3] will return list from start till fourth element of a.
+ If we give step size we can do striding also
+ a[1:-1:2]
+ This gives second, fourth and so on items of a till we reach
+ last item of list.
+ a[::2] will skip all the even placed elements of a
+ With step sizes, if we give negative values we get some
+ interesting results. Lets try
+ a[::-1]
+ It returns reversed 'a'
+ We can check for containership also with lists
+ num
+ 4 in a
+ True
+
+ Python provides support to special immutable lists known as
+ 'tuple' To create a tuple instead of square brackets'[' we have
+ to use normal brackets '('
+ t = (1, 2, 3, 4, 5, 6, 7, 8)
+ its elements can also be accessed using indexes
+ t[0] + t[3] + t[-1]
+ but operation like
+ t[4] = 7 are not allowed
+ These features of tuples have their advantages. To see where
+ are they used we first create two variables
+ a, b = 1, 6
+ print a, b
+ Now lets swap values their values. Normal approach would be
+ to create a temporary to hold the value but because of tuples
+ we can do something cool like
+ b, a = a, b
+ print a, b
+ and values are swapped. And this swapping works for all types
+ of variables.
+
+ With this we come to the end of this tutorial on Lists and
+ tuples. In this tutorial we have learnt some more operations
+ on lists and tuples. In next session we will cover more on
+ Python supported data structures. Thank you!
+
+*** Notes