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
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Tutorial slides on Python.
%
% Author: FOSSEE
% Copyright (c) 2009-2017, FOSSEE, IIT Bombay
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[14pt,compress]{beamer}
\input{macros.tex}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title page
\title[Basic Python]{Practice exercises: Control flow}
\author[FOSSEE Team] {The FOSSEE Group}
\institute[FOSSEE -- IITB] {Department of Aerospace Engineering\\IIT Bombay}
\date[] {Mumbai, India}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DOCUMENT STARTS
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\begin{frame}[fragile,plain]
\frametitle{Note: Python 2.x and 3.x}
If you are using Python 2.x
\begin{itemize}
\item Use \typ{raw\_input} instead of \typ{input}
\item Use the following for \typ{print}
\end{itemize}
\begin{lstlisting}
from __future__ import print_function
\end{lstlisting}
\end{frame}
\begin{frame}[plain]
\frametitle{Exercise: simple \typ{if}}
\begin{enumerate}
\item Ask the user to enter an integer
\item If the number is positive, print "Positive"
\item If the number is negative, say -10 print:\\ "Negative of 10"
\item If the number is zero, print "Zero"
\item Finally, print "Done"
\end{enumerate}
\end{frame}
\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
x = int(input())
if x < 0:
print("Negative of", abs(x))
elif x > 0:
print("Positive")
else:
print("Zero")
print("Done")
\end{lstlisting}
\end{frame}
\begin{frame}[plain]
\frametitle{Exercise: simple \typ{if} 2}
\begin{enumerate}
\item Ask the user to enter two integers, one on each line say \typ{x} and \typ{y}
\item If \typ{x} is larger than \typ{y} print, "first", store the value of \typ{x} in a
variable \typ{highest}
\item If \typ{y} is larger than \typ{x} print, "second", store the value of \typ{y} in a
variable \typ{highest}
\item If they are equal, print "same", store either as the \typ{highest}
\item Finally, print the square of the \typ{highest}
\end{enumerate}
\end{frame}
\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
x = int(input())
y = int(input())
if x > y:
print("first")
highest = x
elif y > x:
print("second")
highest = y
else:
print("same")
highest = x
print(highest*highest)
\end{lstlisting}
\end{frame}
\begin{frame}[plain]
\frametitle{Simple \typ{while} loop 1}
\begin{enumerate}
\item Use a while loop to print the numbers from 1 to 10
\end{enumerate}
\end{frame}
\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
x = 1
while x < 11:
print(x)
x += 1
\end{lstlisting}
\end{frame}
\begin{frame}[plain]
\frametitle{Simple \typ{while} loop 2}
\begin{enumerate}
\item Use a while loop to print the odd numbers from 1 to 10
\end{enumerate}
\end{frame}
\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
x = 1
while x < 11:
print(x)
x += 2
\end{lstlisting}
\end{frame}
\begin{frame}[plain]
\frametitle{Simple \typ{while} loop 2}
\begin{enumerate}
\item Use a while loop to generate the numbers from 1 to 10
\item If a number divisible by 3 is found, print ***
\item If a number divisible by 5 is found, print *****
\end{enumerate}
\end{frame}
\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
x = 1
while x < 11:
if x%3 == 0:
print('***')
elif x%5 == 0:
print('*****')
else:
print(x)
x += 1
\end{lstlisting}
\end{frame}
\begin{frame}[plain]
\frametitle{Simple \typ{while} loop 3}
\begin{enumerate}
\item Use a while loop to generate the numbers from 10 to 1
\item For each number, print that many * characters
\item That is, if the number is 1 print *, if the number is 3, print *** etc.
\end{enumerate}
\end{frame}
\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
x = 10
while x > 0:
print('*'*x)
x -= 1
\end{lstlisting}
\end{frame}
\begin{frame}[plain,fragile]
\frametitle{Simple \typ{while} loop 4}
\begin{enumerate}
\item Ask the user for an integer, \typ{n} (>=2)
\item Use a while loop to print \typ{n} numbers uniformly spaced between 0 and 1
\item Include both 0 and 1
\end{enumerate}
For example if the user entered 3, print
\begin{lstlisting}
0.0
0.5
1.0
\end{lstlisting}
\end{frame}
\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
n = int(input())
dx = 1.0/(n-1)
x = 0.0
while x < (1.0-dx/2):
print(x)
x += dx
print(1.0)
\end{lstlisting}
\end{frame}
\begin{frame}[plain,fragile]
\frametitle{Simple \typ{for} loop 1}
\begin{enumerate}
\item Ask the user for an integer
\item Use a for loop to generate the multiplication table for the number (up
to 10)
\end{enumerate}
If the user entered 5, print the following
\begin{lstlisting}
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
\end{lstlisting}
\end{frame}
\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
n = int(input())
for i in range(1, 11):
print(n, 'x', i, '=', n*i)
\end{lstlisting}
\end{frame}
\begin{frame}[plain,fragile]
\frametitle{\typ{for} loop Fibonacci}
\begin{enumerate}
\item Ask the user for an integer, \typ{n} (>=1)
\item Print the first \typ{n} numbers of the Fibonacci sequence
\end{enumerate}
\end{frame}
\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
n = int(input())
a, b = 0, 1
print(a)
for i in range(n-1):
print(b)
a, b = b, a+b
\end{lstlisting}
\end{frame}
\begin{frame}[plain,fragile]
\frametitle{\typ{for} loop over list 1}
\begin{enumerate}
\item Ask the user for an integer, \typ{n}
\item Use the range to create numbers from 1 to \typ{n}
\item Print each value in this in a separate line
\end{enumerate}
For example let us say user enters \typ{3}, print
\begin{lstlisting}
1
2
3
\end{lstlisting}
\end{frame}
\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
n = int(input())
for x in range(1, n+1):
print(x)
\end{lstlisting}
\end{frame}
\begin{frame}[plain,fragile]
\frametitle{\typ{for} loop over array}
\begin{enumerate}
\item Ask the user for an integer, \typ{n}
\item Use numpy's \typ{linspace} to create n points from 1 to 2
\item Print each value in this in a separate line
\end{enumerate}
For example let us say user enters \typ{3}, print
\begin{lstlisting}
1.0
1.5
2.0
\end{lstlisting}
\end{frame}
\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
from numpy import linspace
n = int(input())
data = linspace(0, 1, n)
for x in data:
print(x)
\end{lstlisting}
\end{frame}
\begin{frame}[plain,fragile]
\frametitle{\typ{for} loop over list 2}
\begin{enumerate}
\item Ask the user for an list of fruits separated by a space
\item Print each fruit in a separate line
\end{enumerate}
For example let us say user enters \typ{apple, pear}, print
\begin{lstlisting}
apple
pear
\end{lstlisting}
\end{frame}
\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
fruits = input()
fruits = fruits.split(',')
for fruit in fruits:
print(fruit)
\end{lstlisting}
\end{frame}
\begin{frame}[plain,fragile]
\frametitle{\typ{for} loop over string}
\begin{enumerate}
\item Ask the user for a string
\item Print each character of the string using a for loop
\end{enumerate}
For example let us say user enters \typ{box}, print
\begin{lstlisting}
b
o
x
\end{lstlisting}
\end{frame}
\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
s = input()
for char in s:
print(char)
\end{lstlisting}
\end{frame}
\begin{frame}[plain,fragile]
\frametitle{Nested \typ{for} loops}
\begin{enumerate}
\item Let us say the user supplies an integer, \typ{n} (empty prompt)
\item Print an n x n matrix where each entry is the sum of the row + column
\end{enumerate}
For example let us say user enters \typ{3}, print
\begin{lstlisting}
0 1 2
1 2 3
2 3 4
\end{lstlisting}
\end{frame}
\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
n = input()
for i in range(n):
for j in range(n):
print(i+j, end=' ')
print()
\end{lstlisting}
\end{frame}
\begin{frame}[plain,fragile]
\frametitle{Exercise: Fibonacci divisible by 4}
\begin{enumerate}
\item Find the first number in the Fibonacci sequence divisible by 4 but > 8
and less than 500.
\end{enumerate}
\end{frame}
\begin{frame}[fragile,plain]
\frametitle{Solution}
\begin{lstlisting}
a, b = 0, 1
while b < 500:
if b % 4 == 0 and b > 8:
print(b)
break
a, b = b, a+b
\end{lstlisting}
\end{frame}
\end{document}
|