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