-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec8.tex
executable file
·368 lines (314 loc) · 7.68 KB
/
lec8.tex
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
\documentclass{beamer}
\usepackage{listings}
\usepackage{color}
\usepackage{hyperref}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{upquote}
% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{10} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{10} % for normal
% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\definecolor{shadecolor}{rgb}{1, 0.9, 0.3}
\usepackage{listings}
\usepackage{framed}
% Python style for highlighting
\newcommand\pythonstyle{\lstset{
language=Python,
basicstyle=\ttm,
otherkeywords={self}, % Add keywords here
keywordstyle=\ttb\color{deepblue},
emph={MyClass,__init__}, % Custom highlighting
emphstyle=\ttb\color{deepred}, % Custom highlighting style
stringstyle=\color{deepgreen},
frame=tb, % Any extra options here
showstringspaces=false, %
upquote=True,
columns=fullflexible,
basicstyle=\ttfamily
}}
% Python environment
\lstnewenvironment{code}[1][]
{
%\begin{small}
\pythonstyle
\lstset{#1}
%\end{small}
}
{}
\begin{document}
\begin{frame}
\frametitle{CS24420 \& MA25220 \& MT25220 \& MX35220 \& CSM0120}
\begin{center}
\begin{huge}
Lecture 8: Hands-on Using functions
\end{huge}
\bigskip
Amanda Clare ([email protected])
\end{center}
\end{frame}
\begin{frame}[fragile]
\frametitle{Functions}
Functions allow us to group useful code statements
together for reusability.
\bigskip
You can use Python's built in functions, such
as len and np.random.random:
\begin{code}
>>> len( [1,2,5,6,7,3] )
>>> len("hello world")
>>> sorted( [4,5,7,2,6] )
>>> import numpy as np
>>> x = np.random.random((5, 10))
\end{code}
But you can also write your own.
\end{frame}
\begin{frame}[fragile]
\frametitle{weather\_message}
For example, here's a function called \texttt{weather\_message}
\begin{code}
import math
def weather_message():
temp = 40 * math.sin(2.5)
if temp < 3:
msg = "wear wool trousers"
elif temp < 10:
msg = "wear trousers"
else:
msg = "wear shorts"
return msg
\end{code}
We write \textbf{def}, the function name, two brackets and a
colon.
The body of the function is indented.
\end{frame}
\begin{frame}[fragile]
\frametitle{Save the code in a file called weather.py}
\begin{shaded}
\begin{code}
import math
def weather_message():
temp = 40 * math.sin(2.5)
if temp < 3:
msg = "wear wool trousers"
elif temp < 10:
msg = "wear trousers"
else:
msg = "wear shorts"
return msg
\end{code}
\end{shaded}
\begin{code}
>>> import weather
>>> w = weather.weather_message()
>>> print(w)
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Your function can take a parameter}
\begin{shaded}
\begin{code}
def weather_message(temp):
if temp < 3:
msg = "wear wool trousers"
elif temp < 10:
msg = "wear trousers"
else:
msg = "wear shorts"
return msg
\end{code}
\end{shaded}
\begin{code}
>>> import weather
>>> w = weather.weather_message(15)
>>> print(w)
>>> w = weather.weather_message(8)
>>> print(w)
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Many parameters}
\begin{shaded}
\begin{code}
def weather_message(temp, low, med):
if temp < low:
msg = "wear wool trousers"
elif temp < med:
msg = "wear trousers"
else:
msg = "wear shorts"
return msg
\end{code}
\end{shaded}
\begin{code}
>>> import weather
>>> w = weather.weather_message(15,5,20)
>>> print(w)
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Reusable code}
We can then call that function many times and reuse it.
\begin{code}
>>> import weather
>>> w = weather.weather_message(6, 3, 10)
>>> w = weather.weather_message(100, 0, 45)
>>> w = weather.weather_message(4, 5, 6)
\end{code}
Writing code that is reusable is a fundamental aim for
programmers.
\bigskip
Others can use your code.
\bigskip
You can reuse your code in future projects.
\end{frame}
\begin{frame}[fragile]
\frametitle{Functions in a script}
\begin{shaded}
\begin{code}
def weather_message(temp, low, med):
if temp < low:
msg = "wear wool trousers"
elif temp < med:
msg = "wear trousers"
else:
msg = "wear shorts"
return msg
w = weather_message(15, 6, 20)
print(w)
\end{code}
\end{shaded}
We can write functions and then call them, all in the
same file. We call this a ``script''. Note how the last two lines are
not indented.
\bigskip
We can execute this code using Spyder.
\end{frame}
\begin{frame}[fragile]
\frametitle{Exercise - calling a function}
Write a for-loop to go through temperatures from 0 to
100 and call your \texttt{weather\_message} function for
each temperature. Always call it with a \texttt{low} of 7 and a
\texttt{med} of 15.
\bigskip
Just start writing your for-loop at the bottom of your
weather.py file.
\bigskip
Run the code.
\end{frame}
\begin{frame}[fragile]
\frametitle{Adding randomness}
Let's add another function to your weather.py file
\begin{shaded}
\begin{code}
import random
def predict_tomorrow_temp():
return random.choice(range(-3, 25))
\end{code}
\end{shaded}
This function returns a value using the keyword return.\\
This function takes no parameters.\\
The value returned will be a random number in the range -3
up to 25 (but not including 25).\\
\begin{code}
>>> weather.predict_tomorrow_temp()
>>> t = weather.predict_tomorrow_temp()
>>> t
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Using that result}
\begin{code}
>>> weather.predict_tomorrow_temp()
2
>>> weather.predict_tomorrow_temp()
9
>>> t = weather.predict_tomorrow_temp()
>>> print(t)
23
>>> t = weather.predict_tomorrow_temp()
>>> print(t)
-2
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Taking a parameter}
\begin{shaded}
\begin{code}
import random
def predict_tomorrow_temp(current_temp):
change = random.choice(range(-1, 2))
new_temp = current_temp + change
return new_temp
\end{code}
\end{shaded}
This function takes one parameter (\texttt{current\_temp})
and returns a result (\texttt{new\_temp}).
\end{frame}
\begin{frame}[fragile]
\frametitle{Using a parameter}
\begin{shaded}
\begin{code}
import random
def predict_tomorrow_temp(current_temp):
change = random.choice(range(-1, 2))
new_temp = current_temp + change
return new_temp
temp_today = 10
temp_tomorrow = predict_tomorrow_temp(temp_today)
temp_next_day = predict_tomorrow_temp(temp_tomorrow)
print(temp_today)
print(temp_tomorrow)
print(temp_next_day)
\end{code}
\end{shaded}
\end{frame}
\begin{frame}[fragile]
\frametitle{Predict a whole week}
\begin{shaded}
\begin{code}
import random
def predict_tomorrow_temp(current_temp):
change = random.choice(range(-1, 2))
new_temp = current_temp + change
return new_temp
temp = 10
for day in range(1, 8):
print("Day " + str(day))
print("Today's temp is:" + str(temp))
temp = predict_tomorrow_temp(temp)
print("Tomorrow will be:" + str(temp))
\end{code}
\end{shaded}
\end{frame}
\begin{frame}[fragile]
\frametitle{Exercise}
\begin{shaded}
\begin{code}
import random
def predict_tomorrows_temp(current_temp):
change = random.choice(range(-1, 2))
new_temp = current_temp + change
return new_temp
\end{code}
\end{shaded}
How would you fix this function so that it never returned
temperatures lower than -3 or higher than 24? (Hint, you'll
need to use an if-statement).
\end{frame}
\begin{frame}
\frametitle{Summary}
\begin{itemize}
\item Writing functions (use \texttt{def})
\item Calling functions (use the function name, brackets, and some
appropriate values if takes parameters)
\item Functions can take parameters
\item Functions often return values that can be saved into variables
\item Functions allow you to write reusable code
\end{itemize}
\end{frame}
\end{document}