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