liststart.rst
author amit
Wed, 22 Sep 2010 10:54:47 +0530
changeset 178 4c7b906e0d21
child 182 ddfb8b89f5bc
permissions -rw-r--r--
Initial commit getting started with lists.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
178
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     1
Hello friends and welcome to the tutorial on getting started with
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     2
lists
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     3
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     4
 {{{ Show the slide containing title }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     5
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     6
 {{{ Show the slide containing the outline slide }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     7
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     8
In this tutorial we will be getting acquainted with a python data
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
     9
structure called lists .  We will learn :
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    10
How to create lists. 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    11
Structure of lists .
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    12
Access list elements 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    13
Append elements to lists 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    14
Deleting elements from lists
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    15
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    16
I hope you have ipython running on your system .
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    17
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    18
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    19
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    20
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    21
List is a compound data type,it can contain data of other data
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    22
types.List is also a sequence data type , all the elements are in
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    23
order and there order has a meaning .
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    24
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    25
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    26
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    27
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    28
We will first create an empty list with no elements . On your ipython
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    29
shell type ::
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    30
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    31
   In []: empty = [] In []: type(empty)
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    32
   
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    33
   <type 'list'>
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    34
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    35
This is an empty list without any elements .
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    36
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    37
* filled lists
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    38
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    39
Lets now define a list nonempty and fill it with some random elements.
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    40
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    41
nonempty = ['spam','eggs', 100, 1.234]
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    42
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    43
Thus the simplest way of creating a list is typing out a sequence 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    44
of comma-separated values (items) between square brackets. 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    45
List items need not all have the same data type.
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    46
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    47
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    48
As you can see lists can contain different kinds of data . In the
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    49
previous example 'spam' and 'eggs' are strings and 100 and 1.234
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    50
integer and float . Thus you can put elements of heterogenous types in
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    51
lists.  Thus list themselves can be one of the element types possible
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    52
in lists.  Thus lists can also contain other lists in it .  Example ::
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    53
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    54
      list_in_list=[[4,2,3,4],'and', 1,2,3,4]
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    55
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    56
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    57
We access list elements using the number of index . The
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    58
index begins from 0 . So for list,  nonempty , nonempty[0] gives the
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    59
first element , nonempty[1] the second element and so on and
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    60
nonempty[3] the last element .::
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    61
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    62
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    63
	    nonempty[0] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    64
	    nonempty[1] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    65
	    nonempty[3]
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    66
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    67
We can also access the elememts from the end using negative indices ::
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    68
   
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    69
   nonempty[-1] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    70
   nonempty[-2] 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    71
   nonempty[-4]
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    72
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    73
-1 being the last element , -2 second to last and -4 being the first
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    74
 element .
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    75
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    76
* =append= elements We can append elements to the end of a list using
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    77
append command .::
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    78
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    79
   nonempty.append('onemore') 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    80
   nonempty.append(6) 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    81
   nonempty
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    82
   
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    83
As you can see non empty appends 'onemore' and 6 at the end
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    84
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    85
Using len function we can check the number of elements in the list
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    86
nonempty .Because we just appended two elements at the end this
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    87
returns us 6.::
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    88
	 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    89
	 len(nonempty)
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    90
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    91
Just like you can append elements to a list you can also remove them .
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    92
Their are two ways of doing one is by index no. ::
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    93
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    94
      del(nonempty[1])
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    95
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    96
deletes the element at index no.1 , i.e the second element of the
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    97
list, 'eggs'. The other way is removing element by content. Lets say
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    98
one wishes to delete 100 from nonempty list the syntax of the command
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
    99
shall be :: a.remove(100)
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   100
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   101
but what if their were two 100 's . To check that lets do a small
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   102
experiment . ::
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   103
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   104
	   a.append('spam') 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   105
	   a 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   106
	   a.remove('spam') 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   107
	   a
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   108
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   109
If we check a now we will see that the first element spam is remove
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   110
thus remove removes only the first instance of the element by sequence
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   111
and leaves others untouched .
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   112
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   113
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   114
{{{Slide for Summary }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   115
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   116
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   117
In this tutorial we came across a sequence data type called lists 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   118
We learned how to create lists .  
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   119
Append elements to list .
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   120
Delete Element from list.  
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   121
And Checking list length.
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   122
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   123
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   124
{{{ Sponsored by Fossee Slide }}}
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   125
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   126
This tutorial was created as a part of FOSSEE project.
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   127
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   128
I hope you found this tutorial useful.
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   129
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   130
Thank You
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   131
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   132
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   133
Author : Amit Sethi 
4c7b906e0d21 Initial commit getting started with lists.
amit
parents:
diff changeset
   134
First Reviewer :