author | Nishanth <nishanth@fossee.in> |
Thu, 23 Sep 2010 10:58:39 +0530 | |
changeset 190 | 6dcc77582e26 |
parent 185 | 35a3811ca91e |
permissions | -rw-r--r-- |
185
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
1 |
.. #[Nishanth]: liststart is not a good name. there is no consistency. |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
2 |
Use underscores or hyphens instead of spaces and |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
3 |
make the filename from LO name |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
4 |
Ex: getting_started_with_lists (or) |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
5 |
getting_started_lists |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
6 |
|
178 | 7 |
Hello friends and welcome to the tutorial on getting started with |
182 | 8 |
lists. |
178 | 9 |
|
10 |
{{{ Show the slide containing title }}} |
|
11 |
||
12 |
{{{ Show the slide containing the outline slide }}} |
|
13 |
||
14 |
In this tutorial we will be getting acquainted with a python data |
|
182 | 15 |
structure called lists. We will learn : |
185
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
16 |
* How to create lists |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
17 |
* Structure of lists |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
18 |
* Access list elements |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
19 |
* Append elements to lists |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
20 |
* Deleting elements from lists |
178 | 21 |
|
185
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
22 |
.. #[Nishanth]: Did you compile this?? |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
23 |
There must an empty before the bulleted list |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
24 |
|
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
25 |
I hope you have ipython running on your system. |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
26 |
|
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
27 |
.. #[Nishanth]: need not specify. Implicit that IPython is running |
178 | 28 |
|
182 | 29 |
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
|
30 |
types. List is also a sequence data type, all the elements are in |
182 | 31 |
order and there order has a meaning. |
178 | 32 |
|
185
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
33 |
We will first create an empty list with no elements. On your IPython |
178 | 34 |
shell type :: |
35 |
||
182 | 36 |
empty = [] |
37 |
type(empty) |
|
178 | 38 |
|
39 |
||
185
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
40 |
This is an empty list without any elements. |
178 | 41 |
|
182 | 42 |
* Filled lists |
178 | 43 |
|
182 | 44 |
Lets now define a list, nonempty and fill it with some random elements. |
178 | 45 |
|
182 | 46 |
nonempty = ['spam', 'eggs', 100, 1.234] |
178 | 47 |
|
48 |
Thus the simplest way of creating a list is typing out a sequence |
|
49 |
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
|
50 |
All the list items need not have the same data type. |
178 | 51 |
|
185
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
52 |
.. #[Nishanth]: do not use "You" or anything else. Stick to "We" |
178 | 53 |
|
185
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
54 |
As we can see lists can contain different kinds of data. In the |
178 | 55 |
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
|
56 |
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
|
57 |
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
|
58 |
in lists. Thus lists can also contain other lists. Example :: |
178 | 59 |
|
182 | 60 |
list_in_list=[[4,2,3,4],'and', 1, 2, 3, 4] |
178 | 61 |
|
182 | 62 |
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
|
63 |
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
|
64 |
first element, nonempty[1] the second element and so on and |
182 | 65 |
nonempty[3] the last element.:: |
178 | 66 |
|
67 |
nonempty[0] |
|
68 |
nonempty[1] |
|
69 |
nonempty[3] |
|
70 |
||
71 |
We can also access the elememts from the end using negative indices :: |
|
72 |
||
73 |
nonempty[-1] |
|
74 |
nonempty[-2] |
|
75 |
nonempty[-4] |
|
76 |
||
77 |
-1 being the last element , -2 second to last and -4 being the first |
|
185
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
78 |
element. |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
79 |
|
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
80 |
.. #[Nishanth]: -1 being last element sounds like -1 is the last element |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
81 |
Instead say -1 gives the last element which is 4 |
178 | 82 |
|
185
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
83 |
.. #[Nishanth]: Instead of saying -4 being the first, say -4 gives 4th |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
84 |
from the last which is the first element. |
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 |
* =append= elements |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
87 |
We can append elements to the end of a list using append command. :: |
178 | 88 |
|
89 |
nonempty.append('onemore') |
|
90 |
nonempty.append(6) |
|
91 |
nonempty |
|
92 |
||
185
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
93 |
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
|
94 |
|
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
95 |
.. #[Nishanth]: First show an example with only one append. |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
96 |
may be show the value of a after first append |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
97 |
then show what happens after second append |
178 | 98 |
|
99 |
Using len function we can check the number of elements in the list |
|
182 | 100 |
nonempty. Because we just appended two elements at the end this |
178 | 101 |
returns us 6.:: |
102 |
||
103 |
len(nonempty) |
|
104 |
||
185
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
105 |
.. #[Nishanth]: the "because ..." can be removed. You can simply |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
106 |
say len gives the no.of elements which is 6 here |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
107 |
|
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
108 |
Just like we can append elements to a list we can also remove them. |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
109 |
There are two ways of doing. One is by using index. :: |
178 | 110 |
|
111 |
del(nonempty[1]) |
|
112 |
||
185
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
113 |
.. #[Nishanth]: do not use "You" or anything else. Stick to We |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
114 |
|
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
115 |
deletes the element at index 1, i.e the second element of the |
178 | 116 |
list, 'eggs'. The other way is removing element by content. Lets say |
117 |
one wishes to delete 100 from nonempty list the syntax of the command |
|
182 | 118 |
should be :: |
119 |
||
120 |
a.remove(100) |
|
178 | 121 |
|
182 | 122 |
but what if their were two 100's. To check that lets do a small |
123 |
experiment. :: |
|
178 | 124 |
|
125 |
a.append('spam') |
|
126 |
a |
|
127 |
a.remove('spam') |
|
128 |
a |
|
129 |
||
185
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
130 |
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
|
131 |
thus remove removes the first occurence of the element in the sequence |
182 | 132 |
and leaves others untouched. |
178 | 133 |
|
134 |
||
135 |
{{{Slide for Summary }}} |
|
136 |
||
137 |
||
185
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
138 |
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
|
139 |
|
182 | 140 |
* We learned how to create lists. |
141 |
* Append elements to list. |
|
142 |
* Delete Element from list. |
|
143 |
* And Checking list length. |
|
178 | 144 |
|
185
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
145 |
.. #[Nishanth]: See the diff. I have corrected punctuation in many places. |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
146 |
The first thing you do before committing is compile the script. |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
147 |
I have corrected syntax errors also in many places. |
178 | 148 |
|
149 |
{{{ Sponsored by Fossee Slide }}} |
|
150 |
||
151 |
This tutorial was created as a part of FOSSEE project. |
|
152 |
||
153 |
I hope you found this tutorial useful. |
|
154 |
||
155 |
Thank You |
|
156 |
||
157 |
||
158 |
Author : Amit Sethi |
|
185
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
159 |
First Reviewer : |
35a3811ca91e
reviewed getting_started_with_lists a.k.a liststart
Nishanth <nishanth@fossee.in>
parents:
182
diff
changeset
|
160 |
Second Reviewer : Nishanth |