-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec2.tex
executable file
·379 lines (323 loc) · 6.63 KB
/
lec2.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
369
370
371
372
373
374
375
376
377
378
379
\documentclass{beamer}
\usepackage{helvet}
\renewcommand{\sfdefault}{phv}
\renewcommand{\rmdefault}{phv}
\usepackage{listings}
\usepackage{color}
\usepackage{hyperref}
\usepackage{upquote}
\usepackage[T1]{fontenc}
% 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}
\usepackage{listings}
% 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 %
}}
% Python environment
\lstnewenvironment{code}[1][]
{
%\begin{small}
\pythonstyle
\lstset{#1}
%\end{small}
}
{}
\begin{document}
% Python basics hands on lecture. Types and variables. if-then.
\begin{frame}
\frametitle{CS24420 \& MA25220 \& MT25220 \& MX35220 \& CSM0120}
\begin{center}
\begin{huge}
Lecture 2: Hands-on Types, Variables, if-else
\end{huge}
\bigskip
Amanda Clare ([email protected])
\end{center}
\end{frame}
\begin{frame}[fragile]
\frametitle{Try it in a new file}
Open Spyder, enter the following code and save it to your M: drive as lec2.py
\begin{code}
x = 1 + 5
y = 6/4.0
print(x)
print(y)
print(x + y)
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Try it}
\begin{code}
x = "Hello"
y = "world"
print(x)
print(y)
print(x + y)
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Try it}
\begin{code}
x = 4
y = "world"
print(x)
print(y)
print(x + y)
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Try it}
\begin{code}
x = "Mary"
print(Mary)
\end{code}
\end{frame}
\begin{frame}
\frametitle{Basic types}
\begin{itemize}
\item {\tt NoneType}
\item {\tt bool}
\item {\tt int} (also {\tt long}, {\tt bignum})
\item {\tt float}
\item {\tt str}
\end{itemize}
(also {\tt complex}, {\tt unicode}, and other less basic types)
\end{frame}
\begin{frame}[fragile]
\frametitle{Try it}
\begin{code}
>>> type(5)
>>> type(None)
>>> type(5.5)
>>> type(False)
>>> type("hello")
>>> type('hello')
>>> type('5.5')
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Convert types}
\begin{code}
>>> int("5")
>>> bool(2)
>>> str(7.5)
>>> int(4.6)
>>> int("Wednesday")
>>> float(4)
>>> bool("hello")
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Try it}
\begin{code}
>>> "a" + "b"
>>> 1 + 2
>>> "a" + 2
>>> "a" + str(2)
>>> 1 + "2"
>>> "6" / "4"
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Try it}
\begin{code}
>>> "a" * 5
>>> True and "hello"
>>> "hello" and True
>>> 1 < 4
>>> type (1 < 4)
\end{code}
\end{frame}
\begin{frame}
\frametitle{Types}
\begin{itemize}
\item The {\tt NoneType} type has only one value, {\tt None}
\item The {\tt bool} type has two values, {\tt True} and {\tt False}
\item int, float, str have many possible values
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Strings}
Single or double quotes:
\begin{code}
>>> x = "hello"
>>> y = 'hello'
>>> x == y
\end{code}
Triple quotes:
\begin{code}
x = """this string
spans many
lines
"""
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Strings}
Strings can behave as if they were an immutable list of characters, so
you can use {\tt len} and {\tt in}:
\begin{code}
>>> x = "hello world"
>>> len(x)
>>> "o" in x
>>> "wo" in x
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Strings}
More things we can do with strings:
\begin{code}
>>> s = "ATGCTTATACA"
>>> s.count('A')
>>> s.startswith("AT")
>>> s.endswith("GC")
>>> s.find("G")
>>> s.find("N")
>>> s = s + "NNNNN"
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Importing useful stuff from the maths library}
\begin{code}
>>> import math
>>> math.pi
>>> math.sin
>>> math.sin(0.5)
>>> help(math.log)
>>> math.log(1024, 2)
>>> help(math)
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{if - else}
\begin{code}
import math
temp = 40 * math.sin(2.5)
if temp < 10:
msg = "wear trousers"
else:
msg = "wear shorts"
print(msg)
\end{code}
We need to be able to make decisions and change the
outcome based on the decisions.
\begin{itemize}
\item Note the indentation and the colons ( {\tt :} )
\item {\tt if} needs a condition to test ({\tt temp < 10})
\item The condition should evaluate to {\tt True} or {\tt False}
\item {\tt else} doesn't have a condition
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{if - else}
\begin{code}
import math
temp = 40 * math.sin(2.5)
if temp < 10:
msg = "Feeling cold! Only " + str(temp) + "."
msg = msg + "Wear trousers"
else:
msg = "We're enjoying summer."
msg = msg + "Wear shorts"
print(msg)
\end{code}
We can have many lines of code within each indented
block.
\end{frame}
\begin{frame}[fragile]
\frametitle{if - elif - else}
\begin{code}
import math
temp = 40 * math.sin(2.5)
if temp < 3:
msg = "wear wool trousers"
elif temp < 10:
msg = "wear trousers"
else:
msg = "wear shorts"
print(msg)
\end{code}
\begin{itemize}
\item if and elif need a condition to test
\item else doesn't have a condition
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{if - else}
\begin{code}
import math
temp = 40 * math.sin(2.5)
rain = 10
if temp < 10:
msg = "Feeling cold! Only " + str(temp)
if rain > 5:
msg = msg + "Wear waterproof trousers"
else:
msg = msg + "Wear trousers"
else:
msg = "We're enjoying summer"
msg = msg + "wear shorts"
\end{code}
\begin{itemize}
\item We can have {\tt if} statements within other {\tt if}
statements.
\item They {\bf need} good indentation
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Conditions}
What kinds of condition can we have?
Generally want something that evaluates to {\tt True} or {\tt False}.
\begin{code}
x < y
x <= y
x >= y
x == y # equality test uses a double equals sign!
x != y # not x == y
x == 5
x < 3.5/2
x < "banana"
"apple" < "banana"
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Conditions}
We can use {\tt and} and {\tt or} and {\tt not}.
\begin{code}
x = "Aberystwyth"
y = 3.0/5
if len(x) < 4 and y > 7:
print("success")
x = len("Aberystwyth") > 5
y = True
if x or y:
print("hooray!")
x = len("Aberystwyth") > 5
if not x:
print("not long enough")
\end{code}
\end{frame}
\begin{frame}
\frametitle{Summary}
\begin{itemize}
\item Types
\item Saving values in variables
\item if-elif-else
\end{itemize}
\end{frame}
\end{document}