|
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 |
1 Hello friends and welcome to the tutorial on getting started with |
7 Hello friends and welcome to the tutorial on getting started with |
2 lists. |
8 lists. |
3 |
9 |
4 {{{ Show the slide containing title }}} |
10 {{{ Show the slide containing title }}} |
5 |
11 |
6 {{{ Show the slide containing the outline slide }}} |
12 {{{ Show the slide containing the outline slide }}} |
7 |
13 |
8 In this tutorial we will be getting acquainted with a python data |
14 In this tutorial we will be getting acquainted with a python data |
9 structure called lists. We will learn : |
15 structure called lists. We will learn : |
10 * How to create lists. |
16 * How to create lists |
11 * Structure of lists. |
17 * Structure of lists |
12 * Access list elements. |
18 * Access list elements |
13 * Append elements to lists. |
19 * Append elements to lists |
14 * Deleting elements from lists. |
20 * Deleting elements from lists |
15 |
21 |
16 I hope you have ipython running on your system . |
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 |
17 |
28 |
18 List is a compound data type, it can contain data of other data |
29 List is a compound data type, it can contain data of other data |
19 types.List is also a sequence data type, all the elements are in |
30 types. List is also a sequence data type, all the elements are in |
20 order and there order has a meaning. |
31 order and there order has a meaning. |
21 |
32 |
22 We will first create an empty list with no elements. On your ipython |
33 We will first create an empty list with no elements. On your IPython |
23 shell type :: |
34 shell type :: |
24 |
35 |
25 empty = [] |
36 empty = [] |
26 type(empty) |
37 type(empty) |
27 |
38 |
28 |
39 |
29 This is an empty list without any elements . |
40 This is an empty list without any elements. |
30 |
41 |
31 * Filled lists |
42 * Filled lists |
32 |
43 |
33 Lets now define a list, nonempty and fill it with some random elements. |
44 Lets now define a list, nonempty and fill it with some random elements. |
34 |
45 |
35 nonempty = ['spam', 'eggs', 100, 1.234] |
46 nonempty = ['spam', 'eggs', 100, 1.234] |
36 |
47 |
37 Thus the simplest way of creating a list is typing out a sequence |
48 Thus the simplest way of creating a list is typing out a sequence |
38 of comma-separated values (items) between square brackets. |
49 of comma-separated values (items) between square brackets. |
39 List items need not all have the same data type. |
50 All the list items need not have the same data type. |
40 |
51 |
|
52 .. #[Nishanth]: do not use "You" or anything else. Stick to "We" |
41 |
53 |
42 As you can see lists can contain different kinds of data. In the |
54 As we can see lists can contain different kinds of data. In the |
43 previous example 'spam' and 'eggs' are strings and 100 and 1.234 |
55 previous example 'spam' and 'eggs' are strings and 100 and 1.234 |
44 integer and float . Thus you can put elements of heterogenous types in |
56 integer and float. Thus we can put elements of heterogenous types in |
45 lists. Thus list themselves can be one of the element types possible |
57 lists. Thus list themselves can be one of the element types possible |
46 in lists. Thus lists can also contain other lists in it . Example :: |
58 in lists. Thus lists can also contain other lists. Example :: |
47 |
59 |
48 list_in_list=[[4,2,3,4],'and', 1, 2, 3, 4] |
60 list_in_list=[[4,2,3,4],'and', 1, 2, 3, 4] |
49 |
61 |
50 |
|
51 We access list elements using the number of index. The |
62 We access list elements using the number of index. The |
52 index begins from 0. So for list, nonempty , nonempty[0] gives the |
63 index begins from 0. So for list nonempty, nonempty[0] gives the |
53 first element , nonempty[1] the second element and so on and |
64 first element, nonempty[1] the second element and so on and |
54 nonempty[3] the last element.:: |
65 nonempty[3] the last element.:: |
55 |
|
56 |
66 |
57 nonempty[0] |
67 nonempty[0] |
58 nonempty[1] |
68 nonempty[1] |
59 nonempty[3] |
69 nonempty[3] |
60 |
70 |
63 nonempty[-1] |
73 nonempty[-1] |
64 nonempty[-2] |
74 nonempty[-2] |
65 nonempty[-4] |
75 nonempty[-4] |
66 |
76 |
67 -1 being the last element , -2 second to last and -4 being the first |
77 -1 being the last element , -2 second to last and -4 being the first |
68 element. |
78 element. |
69 |
79 |
70 * =append= elements We can append elements to the end of a list using |
80 .. #[Nishanth]: -1 being last element sounds like -1 is the last element |
71 append command. :: |
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. :: |
72 |
88 |
73 nonempty.append('onemore') |
89 nonempty.append('onemore') |
74 nonempty.append(6) |
90 nonempty.append(6) |
75 nonempty |
91 nonempty |
76 |
92 |
77 As you can see non empty appends 'onemore' and 6 at the end. |
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 |
78 |
98 |
79 Using len function we can check the number of elements in the list |
99 Using len function we can check the number of elements in the list |
80 nonempty. Because we just appended two elements at the end this |
100 nonempty. Because we just appended two elements at the end this |
81 returns us 6.:: |
101 returns us 6.:: |
82 |
102 |
83 len(nonempty) |
103 len(nonempty) |
84 |
104 |
85 Just like you can append elements to a list you can also remove them . |
105 .. #[Nishanth]: the "because ..." can be removed. You can simply |
86 Their are two ways of doing one is by index no. :: |
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. :: |
87 |
110 |
88 del(nonempty[1]) |
111 del(nonempty[1]) |
89 |
112 |
90 deletes the element at index no.1, i.e the second element of the |
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 |
91 list, 'eggs'. The other way is removing element by content. Lets say |
116 list, 'eggs'. The other way is removing element by content. Lets say |
92 one wishes to delete 100 from nonempty list the syntax of the command |
117 one wishes to delete 100 from nonempty list the syntax of the command |
93 should be :: |
118 should be :: |
94 |
119 |
95 a.remove(100) |
120 a.remove(100) |
100 a.append('spam') |
125 a.append('spam') |
101 a |
126 a |
102 a.remove('spam') |
127 a.remove('spam') |
103 a |
128 a |
104 |
129 |
105 If we check a now we will see that the first element 'spam' is removed |
130 If we check a now we will see that the first occurence 'spam' is removed |
106 thus remove removes only the first instance of the element by sequence |
131 thus remove removes the first occurence of the element in the sequence |
107 and leaves others untouched. |
132 and leaves others untouched. |
108 |
133 |
109 |
134 |
110 {{{Slide for Summary }}} |
135 {{{Slide for Summary }}} |
111 |
136 |
112 |
137 |
113 In this tutorial we came across a sequence data type called lists. |
138 In this tutorial we came across a sequence data type called lists. :: |
|
139 |
114 * We learned how to create lists. |
140 * We learned how to create lists. |
115 * Append elements to list. |
141 * Append elements to list. |
116 * Delete Element from list. |
142 * Delete Element from list. |
117 * And Checking list length. |
143 * And Checking list length. |
118 |
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. |
119 |
148 |
120 {{{ Sponsored by Fossee Slide }}} |
149 {{{ Sponsored by Fossee Slide }}} |
121 |
150 |
122 This tutorial was created as a part of FOSSEE project. |
151 This tutorial was created as a part of FOSSEE project. |
123 |
152 |