<space>
: go to the next slide<shift+space>
: go back<left right up down arrows>
: navigate through the presentation structure<esc>
: toggle overview mode
These lecture notes and exercises are licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Feel free to use / adapt them, but don't sell them, and share them under the same licence.
Predict the output of the following code:
a = 2
b = 3
c = a < b
print(c, type(c))
Write this code in a more pythonic way:
a = 1
if a == 1:
a *= 3
elif a == 2:
a *= 3
else:
a *= 2
Predict the output of the following code:
range(4) == [0, 1, 2, 3]
Write this code in a more pythonic way:
aa = [1, 2, 3]
bb = ['a', 'b', 'c']
for i in range(len(aa)):
print(i, aa[i], bb[i])
Predict the output of the following code:
sum([i for i in range(5)])
How to write this better?
d = {'temp':2, 'unit':'°C'}
if 'temp' in d:
print(d['temp'])
else:
print('Default temp: 5')
Name each of the statements / concepts in the code below:
def get_acinn(stat, var, days=3):
"""Some info"""
return stat
What is printed by this script?
x = 2
y = 1
def func(x):
x = x + y
return x
print(func(x))
print(x)
print(y)
What is a "tuple"?
Explain the code below:
while True:
try:
x = int(input("Please enter a number: "))
break
except ValueError:
print("Oops! That was no valid number. Try again...")
What is the difference between python
, cpython
and jpython
?
What is printed by this script?
def f(this):
"""That"""
pass
print(type(f), f.__doc__)
What is the "standard library"?
What is a "built-in"?
What is pytest
good for again?
What happens when I do: import matplotlib
?
What is the shape of b
?
import numpy as np
a = np.arange(6).reshape(2, 3)
b = a[[0, 1], [1, 2]]
Is this valid code?
a = np.arange(16).reshape(4, 4)
a[1:3, slice(1, 3)] = 1
And this?
a = np.arange(16).reshape(4, 4)
a[1:3, slice(1, 3)] = [101, 102]
And this?
a = np.arange(16).reshape(4, 4)
a[1:3, slice(1, 3)] = [[101, 102], [103, 104]]
And this?
j, i = a.shape
Predict the output of the following code:
a = np.arange(6)
b = a[2:5]
b *= b
print(a)
Predict the output of the following code:
a = [1/i for i in range(1, 1000)]
sum(a) == sum(a[::-1])
np.allclose(sum(a), sum(a[::-1]))
Debugging with Spyder: https://wiki.math.ntnu.no/anaconda/debugging
What happens when you do:
import antigravity
import this