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