author | Puneeth Chaganti <punchagan@fossee.in> |
Wed, 01 Dec 2010 16:51:35 +0530 | |
changeset 522 | d33698326409 |
permissions | -rw-r--r-- |
522
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
1 |
.. Objectives |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
2 |
.. ---------- |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
3 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
4 |
.. By the end of this tutorial, you will be able to |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
5 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
6 |
.. Create Lists. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
7 |
.. Access List elements. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
8 |
.. Append elemets to list |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
9 |
.. Delete list elemets |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
10 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
11 |
.. 1. getting started with ipython |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
12 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
13 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
14 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
15 |
.. Prerequisites |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
16 |
.. ------------- |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
17 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
18 |
.. 1. getting started with strings |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
19 |
.. #. getting started with lists |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
20 |
.. #. basic datatypes |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
21 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
22 |
.. Author : Amit |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
23 |
Internal Reviewer : Anoop Jacob Thomas <anoop@fossee.in> |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
24 |
External Reviewer : |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
25 |
Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
26 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
27 |
.. #[[Anoop: Slides contain only outline and summary |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
28 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
29 |
Script |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
30 |
------ |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
31 |
{{{ Show the slide containing title }}} |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
32 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
33 |
Hello friends and welcome to the tutorial on getting started with |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
34 |
lists. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
35 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
36 |
{{{ Show the slide containing the outline slide }}} |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
37 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
38 |
In this tutorial we will be getting acquainted with a python data |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
39 |
structure called lists. We will learn :: |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
40 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
41 |
* How to create lists |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
42 |
* Structure of lists |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
43 |
* Access list elements |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
44 |
* Append elements to lists |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
45 |
* Delete elements from lists |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
46 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
47 |
List is a compound data type, it can contain data of other data |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
48 |
types. List is also a sequence data type, all the elements are in |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
49 |
order and the order has a meaning. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
50 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
51 |
.. #[[Anoop: "all the elements are in order and **there** order has a |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
52 |
meaning." - I guess something is wrong here, I am not able to |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
53 |
follow this.]] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
54 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
55 |
We will first create an empty list with no elements. On your IPython |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
56 |
shell type :: |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
57 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
58 |
empty = [] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
59 |
type(empty) |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
60 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
61 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
62 |
This is an empty list without any elements. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
63 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
64 |
.. #[[Anoop: the document has to be continous, without any |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
65 |
subheadings, removing * Filled lists]] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
66 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
67 |
Lets now see how to define a non-empty list. We do it as,:: |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
68 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
69 |
nonempty = ['spam', 'eggs', 100, 1.234] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
70 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
71 |
Thus the simplest way of creating a list is typing out a sequence |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
72 |
of comma-separated values (items) between square brackets. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
73 |
All the list items need not be of the same data type. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
74 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
75 |
As we can see lists can contain different kinds of data. In the |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
76 |
previous example 'spam' and 'eggs' are strings and 100 and 1.234 are |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
77 |
integer and float. Thus we can put elements of heterogenous types in |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
78 |
lists including list itself. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
79 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
80 |
.. #[[Anoop: the sentence "Thus list themselves can be one of the |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
81 |
element types possible in lists" is not clear, rephrase it.]] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
82 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
83 |
Example :: |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
84 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
85 |
listinlist=[[4,2,3,4],'and', 1, 2, 3, 4] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
86 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
87 |
We access list elements using the index. The index begins from 0. So |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
88 |
for list nonempty, nonempty[0] gives the first element, nonempty[1] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
89 |
the second element and so on and nonempty[3] the last element. :: |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
90 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
91 |
nonempty[0] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
92 |
nonempty[1] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
93 |
nonempty[3] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
94 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
95 |
Following is an exercise that you must do. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
96 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
97 |
%% %% What happens when you do nonempty[-1]. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
98 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
99 |
Please, pause the video here. Do the exercise and then continue. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
100 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
101 |
.. #[[Anoop: was negative indices introduced earlier, if not may be we |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
102 |
can ask them to try out nonempty[-1] and see what happens and then |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
103 |
tell that it gives the last element in the list.]] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
104 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
105 |
As you can see you get the last element which is 1.234. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
106 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
107 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
108 |
In python negative indices are used to access elements from the end:: |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
109 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
110 |
nonempty[-1] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
111 |
nonempty[-2] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
112 |
nonempty[-4] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
113 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
114 |
-1 gives the last element which is the 4th element , -2 second to last |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
115 |
and -4 gives the fourth from last element which is first element. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
116 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
117 |
We can append elements to the end of a list using append command. :: |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
118 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
119 |
nonempty.append('onemore') |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
120 |
nonempty |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
121 |
nonempty.append(6) |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
122 |
nonempty |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
123 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
124 |
Following are exercises that you must do. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
125 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
126 |
%% %% What is the syntax to get the element 'and' |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
127 |
in the list,listinlist ? |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
128 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
129 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
130 |
%% %% How would you get 'and' using negative indices? |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
131 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
132 |
Please, pause the video here. Do the exercise and then continue. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
133 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
134 |
The solution is on your screen |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
135 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
136 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
137 |
As we can see non empty appends 'onemore' and 6 at the end. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
138 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
139 |
Using len function we can check the number of elements in the list |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
140 |
nonempty. In this case it 6 :: |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
141 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
142 |
len(nonempty) |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
143 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
144 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
145 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
146 |
Just like we can append elements to a list we can also remove them. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
147 |
There are two ways of doing it. One is by using index. :: |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
148 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
149 |
del(nonempty[1]) |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
150 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
151 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
152 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
153 |
deletes the element at index 1, 'eggs' which is the second element of |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
154 |
the list. The other way is removing element by content. Lets say one |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
155 |
wishes to delete 100 from nonempty list the syntax of the command |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
156 |
should be |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
157 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
158 |
.. #[[Anoop: let x = [1,2,1,3] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
159 |
now x.remove(x[2]) |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
160 |
still x is [2,1,3] so that is not the way to remove |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
161 |
element by index, it removed first occurrence of 1(by |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
162 |
content) and not based on index, so make necessary |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
163 |
changes]] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
164 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
165 |
:: |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
166 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
167 |
nonempty.remove(100) |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
168 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
169 |
but what if there were two 100's. To check that lets do a small |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
170 |
experiment. :: |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
171 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
172 |
nonempty.append('spam') |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
173 |
nonempty |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
174 |
nonempty.remove('spam') |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
175 |
nonempty |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
176 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
177 |
If we check now we will see that the first occurence 'spam' is removed |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
178 |
thus remove removes the first occurence of the element in the sequence |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
179 |
and leaves others untouched. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
180 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
181 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
182 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
183 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
184 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
185 |
.. #[[Anoop: does it have two spams or two pythons?]] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
186 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
187 |
.. #[[Anoop: there are no exercises/solved problems in this script, |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
188 |
add them]] |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
189 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
190 |
Following are exercises that you must do. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
191 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
192 |
%% %% Remove the third element from the list, listinlist. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
193 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
194 |
%% %% Remove 'and' from the list, listinlist. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
195 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
196 |
Please, pause the video here. Do the exercise and then continue. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
197 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
198 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
199 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
200 |
{{{Slide for Summary }}} |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
201 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
202 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
203 |
In this tutorial we came across a sequence data type called lists. :: |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
204 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
205 |
* We learned how to create lists. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
206 |
* How to access lists. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
207 |
* Append elements to list. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
208 |
* Delete Element from list. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
209 |
* And Checking list length. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
210 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
211 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
212 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
213 |
{{{ show Sponsored by Fossee Slide }}} |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
214 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
215 |
This tutorial was created as a part of FOSSEE project. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
216 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
217 |
I hope you found this tutorial useful. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
218 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
219 |
Thank You |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
220 |
|
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
221 |
.. |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
222 |
* Author : Amit Sethi |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
223 |
* First Reviewer : |
d33698326409
Renamed all LOs to match with their names in progress.org.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
224 |
* Second Reviewer : Nishanth |