16 * How to create lists |
10 * How to create lists |
17 * Structure of lists |
11 * Structure of lists |
18 * Access list elements |
12 * Access list elements |
19 * Append elements to lists |
13 * Append elements to lists |
20 * Deleting elements from lists |
14 * 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 |
15 |
29 List is a compound data type, it can contain data of other data |
16 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 |
17 types. List is also a sequence data type, all the elements are in |
31 order and there order has a meaning. |
18 order and there order has a meaning. |
32 |
19 |
47 |
34 |
48 Thus the simplest way of creating a list is typing out a sequence |
35 Thus the simplest way of creating a list is typing out a sequence |
49 of comma-separated values (items) between square brackets. |
36 of comma-separated values (items) between square brackets. |
50 All the list items need not have the same data type. |
37 All the list items need not have the same data type. |
51 |
38 |
52 .. #[Nishanth]: do not use "You" or anything else. Stick to "We" |
39 |
53 |
40 |
54 As we can see lists can contain different kinds of data. In the |
41 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 |
42 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 |
43 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 |
44 lists. Thus list themselves can be one of the element types possible |
72 |
59 |
73 nonempty[-1] |
60 nonempty[-1] |
74 nonempty[-2] |
61 nonempty[-2] |
75 nonempty[-4] |
62 nonempty[-4] |
76 |
63 |
77 -1 being the last element , -2 second to last and -4 being the first |
64 -1 gives the last element which is the 4th element , -2 second to last and -4 gives the fourth |
78 element. |
65 from last element which is first element. |
79 |
66 |
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 |
67 |
83 .. #[Nishanth]: Instead of saying -4 being the first, say -4 gives 4th |
|
84 from the last which is the first element. |
|
85 |
68 |
86 * =append= elements |
69 |
87 We can append elements to the end of a list using append command. :: |
70 We can append elements to the end of a list using append command. :: |
88 |
71 |
89 nonempty.append('onemore') |
72 nonempty.append('onemore') |
|
73 nonempty |
90 nonempty.append(6) |
74 nonempty.append(6) |
91 nonempty |
75 nonempty |
92 |
76 |
93 As we can see non empty appends 'onemore' and 6 at the end. |
77 As we can see non empty appends 'onemore' and 6 at the end. |
94 |
78 |
95 .. #[Nishanth]: First show an example with only one append. |
79 |
96 may be show the value of a after first append |
|
97 then show what happens after second append |
|
98 |
80 |
99 Using len function we can check the number of elements in the list |
81 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 |
82 nonempty. In this case it being 6: |
101 returns us 6.:: |
|
102 |
83 |
103 len(nonempty) |
84 len(nonempty) |
104 |
85 |
105 .. #[Nishanth]: the "because ..." can be removed. You can simply |
86 |
106 say len gives the no.of elements which is 6 here |
|
107 |
87 |
108 Just like we can append elements to a list we can also remove them. |
88 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. :: |
89 There are two ways of doing it. One is by using index. :: |
110 |
90 |
111 del(nonempty[1]) |
91 del(nonempty[1]) |
112 |
92 |
113 .. #[Nishanth]: do not use "You" or anything else. Stick to We |
93 |
114 |
94 |
115 deletes the element at index 1, i.e the second element of the |
95 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 |
96 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 |
97 one wishes to delete 100 from nonempty list the syntax of the command |
118 should be :: |
98 should be :: |
140 * We learned how to create lists. |
120 * We learned how to create lists. |
141 * Append elements to list. |
121 * Append elements to list. |
142 * Delete Element from list. |
122 * Delete Element from list. |
143 * And Checking list length. |
123 * And Checking list length. |
144 |
124 |
145 .. #[Nishanth]: See the diff. I have corrected punctuation in many places. |
125 |
146 The first thing you do before committing is compile the script. |
|
147 I have corrected syntax errors also in many places. |
|
148 |
126 |
149 {{{ Sponsored by Fossee Slide }}} |
127 {{{ Sponsored by Fossee Slide }}} |
150 |
128 |
151 This tutorial was created as a part of FOSSEE project. |
129 This tutorial was created as a part of FOSSEE project. |
152 |
130 |