|
1 .. #[Nishanth]: liststart is not a good name. there is no consistency. |
|
2 Use underscores or hyphens instead of spaces and |
|
3 make the filename from LO name |
|
4 Ex: getting_started_with_lists (or) |
|
5 getting_started_lists |
|
6 |
|
7 Hello friends and welcome to the tutorial on getting started with |
|
8 lists. |
|
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 |
|
15 structure called lists. We will learn : |
|
16 * How to create lists |
|
17 * Structure of lists |
|
18 * Access list elements |
|
19 * Append elements to lists |
|
20 * Deleting elements from lists |
|
21 |
|
22 .. #[Nishanth]: Did you compile this?? |
|
23 There must an empty before the bulleted list |
|
24 |
|
25 I hope you have ipython running on your system. |
|
26 |
|
27 .. #[Nishanth]: need not specify. Implicit that IPython is running |
|
28 |
|
29 List is a compound data type, it can contain data of other data |
|
30 types. List is also a sequence data type, all the elements are in |
|
31 order and there order has a meaning. |
|
32 |
|
33 We will first create an empty list with no elements. On your IPython |
|
34 shell type :: |
|
35 |
|
36 empty = [] |
|
37 type(empty) |
|
38 |
|
39 |
|
40 This is an empty list without any elements. |
|
41 |
|
42 * Filled lists |
|
43 |
|
44 Lets now define a list, nonempty and fill it with some random elements. |
|
45 |
|
46 nonempty = ['spam', 'eggs', 100, 1.234] |
|
47 |
|
48 Thus the simplest way of creating a list is typing out a sequence |
|
49 of comma-separated values (items) between square brackets. |
|
50 All the list items need not have the same data type. |
|
51 |
|
52 .. #[Nishanth]: do not use "You" or anything else. Stick to "We" |
|
53 |
|
54 As we can see lists can contain different kinds of data. In the |
|
55 previous example 'spam' and 'eggs' are strings and 100 and 1.234 |
|
56 integer and float. Thus we can put elements of heterogenous types in |
|
57 lists. Thus list themselves can be one of the element types possible |
|
58 in lists. Thus lists can also contain other lists. Example :: |
|
59 |
|
60 list_in_list=[[4,2,3,4],'and', 1, 2, 3, 4] |
|
61 |
|
62 We access list elements using the number of index. The |
|
63 index begins from 0. So for list nonempty, nonempty[0] gives the |
|
64 first element, nonempty[1] the second element and so on and |
|
65 nonempty[3] the last element.:: |
|
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 |
|
78 element. |
|
79 |
|
80 .. #[Nishanth]: -1 being last element sounds like -1 is the last element |
|
81 Instead say -1 gives the last element which is 4 |
|
82 |
|
83 .. #[Nishanth]: Instead of saying -4 being the first, say -4 gives 4th |
|
84 from the last which is the first element. |
|
85 |
|
86 * =append= elements |
|
87 We can append elements to the end of a list using append command. :: |
|
88 |
|
89 nonempty.append('onemore') |
|
90 nonempty.append(6) |
|
91 nonempty |
|
92 |
|
93 As we can see non empty appends 'onemore' and 6 at the end. |
|
94 |
|
95 .. #[Nishanth]: First show an example with only one append. |
|
96 may be show the value of a after first append |
|
97 then show what happens after second append |
|
98 |
|
99 Using len function we can check the number of elements in the list |
|
100 nonempty. Because we just appended two elements at the end this |
|
101 returns us 6.:: |
|
102 |
|
103 len(nonempty) |
|
104 |
|
105 .. #[Nishanth]: the "because ..." can be removed. You can simply |
|
106 say len gives the no.of elements which is 6 here |
|
107 |
|
108 Just like we can append elements to a list we can also remove them. |
|
109 There are two ways of doing. One is by using index. :: |
|
110 |
|
111 del(nonempty[1]) |
|
112 |
|
113 .. #[Nishanth]: do not use "You" or anything else. Stick to We |
|
114 |
|
115 deletes the element at index 1, i.e the second element of the |
|
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 |
|
118 should be :: |
|
119 |
|
120 a.remove(100) |
|
121 |
|
122 but what if their were two 100's. To check that lets do a small |
|
123 experiment. :: |
|
124 |
|
125 a.append('spam') |
|
126 a |
|
127 a.remove('spam') |
|
128 a |
|
129 |
|
130 If we check a now we will see that the first occurence 'spam' is removed |
|
131 thus remove removes the first occurence of the element in the sequence |
|
132 and leaves others untouched. |
|
133 |
|
134 |
|
135 {{{Slide for Summary }}} |
|
136 |
|
137 |
|
138 In this tutorial we came across a sequence data type called lists. :: |
|
139 |
|
140 * We learned how to create lists. |
|
141 * Append elements to list. |
|
142 * Delete Element from list. |
|
143 * And Checking list length. |
|
144 |
|
145 .. #[Nishanth]: See the diff. I have corrected punctuation in many places. |
|
146 The first thing you do before committing is compile the script. |
|
147 I have corrected syntax errors also in many places. |
|
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 |
|
159 First Reviewer : |
|
160 Second Reviewer : Nishanth |