|
1 Hello friends. Welcome to this spoken tutorial on Getting started with |
|
2 strings. |
|
3 |
|
4 {{{ Show the slide containing the title }}} |
|
5 |
|
6 {{{ Show the slide containing the outline }}} |
|
7 |
|
8 We have already learnt a lot about Lists in Python. In this tutorial, |
|
9 we will learn more about advanced features of Lists in Python. We will |
|
10 see in detail how to concatenate two lists, slicing and striding of |
|
11 lists, methods to sort and reverse the list. |
|
12 |
|
13 {{{ Shift to terminal and start ipython }}} |
|
14 |
|
15 To begin with let us start ipython, by typing:: |
|
16 |
|
17 ipython |
|
18 |
|
19 on the terminal |
|
20 |
|
21 We already know what Lists are in Python, how to access individual |
|
22 elements in the list and some of the functions that can be run on the |
|
23 lists like max, min, sum len and so on. Now let us learn some of the |
|
24 basic operations that can be performed on Lists. |
|
25 |
|
26 We already know how to access individual elements in a List. But what |
|
27 if we have a scenario where we need to get a part of the entire list |
|
28 or what we call as a slice of the list? Python supports slicing on |
|
29 lists. Let us say I have the list:: |
|
30 |
|
31 primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] |
|
32 |
|
33 To obtain the all the primes between 10 and 20 from the above list of |
|
34 primes we say:: |
|
35 |
|
36 primes[4:8] |
|
37 |
|
38 This gives us all the elements in the list starting from the element |
|
39 with the index 4 which is 11 in our list upto the element with index 8 |
|
40 in the list but not including the eigth element. So we obtain a slice |
|
41 starting from 11 upto 19th. It is a very important to remember that |
|
42 when ever we specify a range of elements in Python the start index is |
|
43 included and end index is not included. So in the above case, 11 which |
|
44 was the element with the index 4 was included but 23 which was the |
|
45 element with index 8 was exluded. |
|
46 |
|
47 Generalizing, we can obtain a slice of the list "p" from the index |
|
48 "start" upto the index "end" but excluding "end" with the following |
|
49 syntax |
|
50 |
|
51 {{{ Show the slide containing p[start:stop] }}} |
|
52 |
|
53 By default the slice fetches all the elements between start and stop |
|
54 including start but not stop. So as to say we obtain all the elements |
|
55 between start and stop in steps of one. Python also provides us the |
|
56 functionality to specify the steps in which the slice must be |
|
57 obtained. Say we have:: |
|
58 |
|
59 num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13] |
|
60 |
|
61 If we want to obtain all the odd numbers less than 10 from the list |
|
62 "num" we have to start from element with index 1 upto the index 10 in |
|
63 steps of 2:: |
|
64 |
|
65 num[1:10:2] |
|
66 |
|
67 So if we don't specify the step it is by default 1. Similary there are |
|
68 default values for start and stop indices as well. If we don't specify |
|
69 the start index it is implicitly taken as the first element of the |
|
70 list:: |
|
71 |
|
72 num[:10] |
|
73 |
|
74 This gives us all the elements from the beginning upto the 10th |
|
75 element but not including the 10th element in the list "num". Similary |
|
76 if the stop index is not specified it is implicitly assumed to be the |
|
77 end of the list, including the last element of the list:: |
|
78 |
|
79 num[10:] |
|
80 |
|
81 gives all the elements starting from the 10th element in the list |
|
82 "num" upto the final element including that last element. Now:: |
|
83 |
|
84 num[::2] |
|
85 |
|
86 gives us all the even numbers in the list "num". |
|
87 |
|
88 The other basic operation that we can perform on list is concatenation |
|
89 of two or more lists. We can combine two lists by using the "plus" |
|
90 operator. Say we have |
|
91 |
|
92 {{{ Read as you type }}}:: |
|
93 |
|
94 a = [1, 2, 3, 4] |
|
95 b = [4, 5, 6, 7] |
|
96 a + b |
|
97 |
|
98 When we concatenate lists using the "plus" operator we get a new |
|
99 list. We can store this list in a new variable:: |
|
100 |
|
101 c = a + b |
|
102 c |
|
103 |
|
104 It is important to observe that the "plus" operator always returns a |
|
105 new list without touching anything in the existing lists which are the |
|
106 operands of the concatenation operation. |
|
107 |
|
108 We know that list is a collection of data. Whenever we have a |
|
109 collection we run into situations where we want to start the |
|
110 collection. Lists support sort method which sorts the list inplace:: |
|
111 |
|
112 a = [5, 1, 6, 7, 7, 10] |
|
113 a.sort() |
|
114 |
|
115 Now the contents of the list "a" will be:: |
|
116 |
|
117 a |
|
118 [1, 5, 6, 7, 7, 10] |
|
119 |
|
120 Since the sort method sorts the list inplace the original list we had |
|
121 is overwritten or replaced. We have no way to obtain the original list |
|
122 back. One way to avoid this is to keep a copy of the original list in |
|
123 another variable and run the sort method on the list. However Python |
|
124 also provides a built-in function called sorted which sorts the list |
|
125 which is passed as an argument to it and returns a new sorted list:: |
|
126 |
|
127 a = [5, 1, 6, 7, 7, 10] |
|
128 sorted(a) |
|
129 |
|
130 We can store this sorted list another list variable:: |
|
131 |
|
132 sa = sorted(a) |
|
133 |
|
134 Similarly to perform certain operations on the list we would like to |
|
135 reverse the list. Python provides reverse method which again reverses |
|
136 the list inplace:: |
|
137 |
|
138 a = [1, 2, 3, 4, 5] |
|
139 a.reverse() |
|
140 |
|
141 reverses the list "a" and stores the reversed list inplace i.e. in "a" |
|
142 itself. Lets see the list "a":: |
|
143 |
|
144 a |
|
145 [5, 4, 3, 2, 1] |
|
146 |
|
147 But again the original list is lost. If we want to obtain the reverse |
|
148 of a list keeping the original list intact we can use the Python |
|
149 built-in function reversed. reversed function returns a new list which |
|
150 is the reverse of the list which was passed as the argument to the |
|
151 reversed function:: |
|
152 |
|
153 a = [1, 2, 3, 4, 5] |
|
154 reversed(a) |
|
155 |
|
156 We can also store this new reversed list in another list variable. |
|
157 |
|
158 {{{ Show summary slide }}} |
|
159 |
|
160 This brings us to the end of another session. In this tutorial session |
|
161 we learnt |
|
162 |
|
163 * How to define strings |
|
164 * Different types of defining a string |
|
165 * String concatenation and repeatition |
|
166 * Accessing individual elements of the string |
|
167 * Immutability of strings |
|
168 |
|
169 {{{ Show the "sponsored by FOSSEE" slide }}} |
|
170 |
|
171 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
172 |
|
173 Hope you have enjoyed and found it useful. |
|
174 Thankyou |
|
175 |
|
176 .. Author : Madhu |
|
177 Internal Reviewer 1 : [potential reviewer: Nishanth] |
|
178 Internal Reviewer 2 : [potential reviewer: Amit] |
|
179 External Reviewer : |
|
180 |