1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
|
\section{Strings}
\begin{frame}[fragile]
\frametitle{What are Strings?}
\begin{itemize}
\item Anything quoted is a string
\item Single quotes, double quotes or triple single/double quotes
\item Any length --- single character, null string, \ldots
\end{itemize}
\begin{lstlisting}
In[]: 'This is a string'
In[]: "This is a string too'
In[]: '''This is a string as well'''
In[]: """This is also a string"""
In[]: '' # empty string
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Why so many?}
\begin{itemize}
\item Reduce the need for escaping
\end{itemize}
\begin{lstlisting}
In[]: "Python's strings are powerful!"
In[]: 'He said, "I love Python!"'
\end{lstlisting}
\begin{itemize}
\item Triple quoted strings can be multi-line
\item Used for doc-strings
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Assignment \& Operations}
\begin{lstlisting}
In[]: a = 'Hello'
In[]: b = 'World'
In[]: c = a + ', ' + b + '!'
\end{lstlisting}
\begin{itemize}
\item Strings can be multiplied with numbers
\end{itemize}
\begin{lstlisting}
In[]: a = 'Hello'
In[]: a * 5
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Accessing Elements}
\begin{lstlisting}
In[]: print a[0], a[4], a[-1], a[-4]
\end{lstlisting}
\begin{itemize}
\item Can we change the elements?
\end{itemize}
\begin{lstlisting}
In[]: a[0] = 'H'
\end{lstlisting}
\begin{itemize}
\item Strings are immutable!
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Problem - Day of the Week?}
\begin{itemize}
\item Strings have methods to manipulate them
\end{itemize}
\begin{block}{Problem}
Given a list, \texttt{week}, containing names of the days of the
week and a string \texttt{s}, check if the string is a day of the
week. We should be able to check for any of the forms like,
\emph{sat, saturday, Sat, Saturday, SAT, SATURDAY}
\end{block}
\begin{itemize}
\item Get the first 3 characters of the string
\item Convert it all to lower case
\item Check for existence in the list, \texttt{week}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Slicing}
\begin{lstlisting}
In[]: q = "Hello World"
In[]: q[0:3]
In[]: q[:3]
In[]: q[3:]
In[]: q[:]
In[]: q[-1:1]
In[]: q[1:-1]
\end{lstlisting}
\begin{itemize}
\item One or both of the limits, is optional
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Striding}
\begin{lstlisting}
In[]: q[0:5:1]
In[]: q[0:5:2]
In[]: q[0:5:3]
In[]: q[0::2]
In[]: q[2::2]
In[]: q[::2]
In[]: q[5:0:-1]
In[]: q[::-1]
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{String Methods}
\begin{lstlisting}
In[]: s.lower()
In[]: s.upper()
s.<TAB>
\end{lstlisting}
\begin{itemize}
\item \alert{Strings are immutable!}
\item A new string is being returned
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Solution - Day of the Week?}
\begin{lstlisting}
In[]: s.lower()[:3] in week
\end{lstlisting}
OR
\begin{lstlisting}
In[]: s[:3].lower() in week
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{join} a list of strings}
\begin{itemize}
\item Given a list of strings
\item We wish to join them into a single string
\item Possibly, each string separated by a common token
\end{itemize}
\begin{lstlisting}
In[]: email_list = ["info@fossee.in",
"enquiries@fossee.in",
"help@fossee.in"]
\end{lstlisting}
\begin{lstlisting}
In[]: '; '.join(email_list)
In[]: ', '.join(email_list)
\end{lstlisting}
\end{frame}
\section{Conditionals}
\begin{frame}[fragile]
\frametitle{\texttt{if-else} block}
\begin{lstlisting}
In[]: a = 5
In[]: if a % 2 == 0:
....: print "Even"
....: else:
....: print "Odd"
\end{lstlisting}
\begin{itemize}
\item A code block -- \texttt{:} and indentation
\item Exactly one block gets executed in the \texttt{if-else}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{if-elif-else}}
\begin{lstlisting}
In[]: if a > 0:
....: print "positive"
....: elif a < 0:
....: print "negative"
....: else:
....: print "zero"
\end{lstlisting}
\begin{itemize}
\item Only one block gets executed, depending on \texttt{a}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{else} is optional}
\begin{lstlisting}
In[]: if user == 'admin':
....: admin_Operations()
....: elif user == 'moderator':
....: moderator_operations()
....: elif user == 'client':
....: customer_operations()
\end{lstlisting}
\begin{itemize}
\item Note that there is no \texttt{else} block
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Ternary operator}
\begin{itemize}
\item \texttt{score\_str} is either \texttt{'AA'} or a string of one
of the numbers in the range 0 to 100.
\item We wish to convert the string to a number using \texttt{int}
\item Convert it to 0, when it is \texttt{'AA'}
\item \texttt{if-else} construct or the ternary operator
\end{itemize}
\begin{lstlisting}
In[]: if score_str != 'AA':
....: score = int(score_str)
....: else:
....: score = 0
\end{lstlisting}
\begin{lstlisting}
In[]: ss = score_str
In[]: score = int(ss) if ss != 'AA' else 0
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{pass}}
\begin{itemize}
\item \texttt{pass} is a syntactic filler
\item When a certain block has no statements, a \texttt{pass} is
thrown in
\item Mostly, when you want to get back to that part, later.
\end{itemize}
\end{frame}
\section{Loops}
\begin{frame}[fragile]
\frametitle{\texttt{while}}
\begin{itemize}
\item Print squares of all odd numbers less than 10 using
\texttt{while}
\end{itemize}
\begin{lstlisting}
In[]: i = 1
In[]: while i<10:
....: print i*i
....: i += 2
\end{lstlisting}
\begin{itemize}
\item The loops runs as long as the condition is \texttt{True}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{for}}
\begin{itemize}
\item Print squares of all odd numbers less than 10 using
\texttt{for}
\end{itemize}
\begin{lstlisting}
In[]: for n in [1, 2, 3]:
....: print n
\end{lstlisting}
\begin{itemize}
\item \texttt{for} iterates over each element of a sequence
\end{itemize}
\begin{lstlisting}
In[]: for n in [1, 3, 5, 7, 9]:
....: print n*n
In[]: for n in range(1, 10, 2):
....: print n*n
\end{lstlisting}
\begin{itemize}
\item \alert{range([start,] stop[, step])}
\item Returns a list; Stop value is not included.
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{break}}
\begin{itemize}
\item breaks out of the innermost loop.
\item Squares of odd numbers below 10 using \texttt{while} \&
\texttt{break}
\end{itemize}
\begin{lstlisting}
In[]: i = 1
In[]: while True:
....: print i*i
....: i += 2
....: if i>10:
....: break
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{continue}}
\begin{itemize}
\item Skips execution of rest of the loop on current iteration
\item Jumps to the end of this iteration
\item Squares of all odd numbers below 10, not multiples of 3
\end{itemize}
\begin{lstlisting}
In[]: for n in range(1, 10, 2):
....: if n%3 == 0:
....: continue
....: print n*n
\end{lstlisting}
\end{frame}
\section{Lists}
\begin{frame}[fragile]
\frametitle{Creating Lists}
\begin{lstlisting}
In[]: empty = []
In[]: p = ['spam', 'eggs', 100, 1.234]
In[]: q = [[4, 2, 3, 4], 'and', 1, 2, 3, 4]
\end{lstlisting}
\begin{itemize}
\item Lists can be empty, with no elements in them
\item Lists can be heterogeneous -- every element of different kind
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Accessing Elements}
\begin{lstlisting}
In[]: print p[0], p[1], p[3]
In[]: print p[-1], p[-2], p[-4]
In[]: print p[10]
\end{lstlisting}
\begin{itemize}
\item Indexing starts from 0
\item Indexes can be negative
\item Indexes should be in the valid range
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Accessing Elements \& length}
\begin{lstlisting}
In[]: print p[0], p[1], p[3]
In[]: print p[-1], p[-2], p[-4]
In[]: print len(p)
In[]: print p[10]
\end{lstlisting}
\begin{itemize}
\item Indexing starts from 0
\item Indexes can be negative
\item Indexes should be within the \texttt{range(0, len(p))}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Adding \& Removing Elements}
\begin{itemize}
\item The append method adds elements to the end of the list
\end{itemize}
\begin{lstlisting}
In[]: p.append('onemore')
In[]: p
In[]: p.append([1, 6])
In[]: p
\end{lstlisting}
\begin{itemize}
\item Elements can be removed based on their index OR
\item based on the value of the element
\end{itemize}
\begin{lstlisting}
In[]: del p[1]
In[]: p.remove(100)
\end{lstlisting}
\begin{itemize}
\item \alert{When removing by value, first element is removed}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Concatenating lists}
\begin{lstlisting}
In[]: a = [1, 2, 3, 4]
In[]: b = [4, 5, 6, 7]
In[]: a + b
In[]: print a+b, a, b
\end{lstlisting}
\begin{itemize}
\item A new list is returned; None of the original lists change
\end{itemize}
\begin{lstlisting}
In[]: c = a + b
In[]: c
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Slicing \& Striding}
\begin{lstlisting}
In[]: primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
In[]: primes[4:8]
In[]: primes[:4]
In[]: num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
In[]: num[1:10:2]
In[]: num[:10]
In[]: num[10:]
In[]: num[::2]
In[]: num[::-1]
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Sorting}
\begin{lstlisting}
In[]: a = [5, 1, 6, 7, 7, 10]
In[]: a.sort()
In[]: a
\end{lstlisting}
\begin{itemize}
\item \texttt{sort} method sorts the list in-place
\item Use \texttt{sorted} if you require a new list
\end{itemize}
\begin{lstlisting}
In[]: a = [5, 1, 6, 7, 7, 10]
In[]: sorted(a)
In[]: a
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Reversing}
\begin{lstlisting}
In[]: a = [5, 1, 6, 7, 7, 10]
In[]: a.reverse()
In[]: a
\end{lstlisting}
\begin{itemize}
\item \texttt{reverse} method reverses the list in-place
\item Use \texttt{[::-1]} if you require a new list
\end{itemize}
\begin{lstlisting}
In[]: a = [5, 1, 6, 7, 7, 10]
In[]: a[::-1]
In[]: a
\end{lstlisting}
\end{frame}
|