|
1 Hello friends and welcome to the tutorial on getting started with |
|
2 lists |
|
3 |
|
4 {{{ Show the slide containing title }}} |
|
5 |
|
6 {{{ Show the slide containing the outline slide }}} |
|
7 |
|
8 In this tutorial we will be getting acquainted with a python data |
|
9 structure called lists . We will learn : |
|
10 How to create lists. |
|
11 Structure of lists . |
|
12 Access list elements |
|
13 Append elements to lists |
|
14 Deleting elements from lists |
|
15 |
|
16 I hope you have ipython running on your system . |
|
17 |
|
18 |
|
19 |
|
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 :: |
|
30 |
|
31 In []: empty = [] In []: type(empty) |
|
32 |
|
33 <type 'list'> |
|
34 |
|
35 This is an empty list without any elements . |
|
36 |
|
37 * filled lists |
|
38 |
|
39 Lets now define a list nonempty and fill it with some random elements. |
|
40 |
|
41 nonempty = ['spam','eggs', 100, 1.234] |
|
42 |
|
43 Thus the simplest way of creating a list is typing out a sequence |
|
44 of comma-separated values (items) between square brackets. |
|
45 List items need not all have the same data type. |
|
46 |
|
47 |
|
48 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 |
|
50 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 |
|
52 in lists. Thus lists can also contain other lists in it . Example :: |
|
53 |
|
54 list_in_list=[[4,2,3,4],'and', 1,2,3,4] |
|
55 |
|
56 |
|
57 We access list elements using the number of index . The |
|
58 index begins from 0 . So for list, nonempty , nonempty[0] gives the |
|
59 first element , nonempty[1] the second element and so on and |
|
60 nonempty[3] the last element .:: |
|
61 |
|
62 |
|
63 nonempty[0] |
|
64 nonempty[1] |
|
65 nonempty[3] |
|
66 |
|
67 We can also access the elememts from the end using negative indices :: |
|
68 |
|
69 nonempty[-1] |
|
70 nonempty[-2] |
|
71 nonempty[-4] |
|
72 |
|
73 -1 being the last element , -2 second to last and -4 being the first |
|
74 element . |
|
75 |
|
76 * =append= elements We can append elements to the end of a list using |
|
77 append command .:: |
|
78 |
|
79 nonempty.append('onemore') |
|
80 nonempty.append(6) |
|
81 nonempty |
|
82 |
|
83 As you can see non empty appends 'onemore' and 6 at the end |
|
84 |
|
85 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 |
|
87 returns us 6.:: |
|
88 |
|
89 len(nonempty) |
|
90 |
|
91 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. :: |
|
93 |
|
94 del(nonempty[1]) |
|
95 |
|
96 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 |
|
98 one wishes to delete 100 from nonempty list the syntax of the command |
|
99 shall be :: a.remove(100) |
|
100 |
|
101 but what if their were two 100 's . To check that lets do a small |
|
102 experiment . :: |
|
103 |
|
104 a.append('spam') |
|
105 a |
|
106 a.remove('spam') |
|
107 a |
|
108 |
|
109 If we check a now we will see that the first element spam is remove |
|
110 thus remove removes only the first instance of the element by sequence |
|
111 and leaves others untouched . |
|
112 |
|
113 |
|
114 {{{Slide for Summary }}} |
|
115 |
|
116 |
|
117 In this tutorial we came across a sequence data type called lists |
|
118 We learned how to create lists . |
|
119 Append elements to list . |
|
120 Delete Element from list. |
|
121 And Checking list length. |
|
122 |
|
123 |
|
124 {{{ Sponsored by Fossee Slide }}} |
|
125 |
|
126 This tutorial was created as a part of FOSSEE project. |
|
127 |
|
128 I hope you found this tutorial useful. |
|
129 |
|
130 Thank You |
|
131 |
|
132 |
|
133 Author : Amit Sethi |
|
134 First Reviewer : |