This slide deck is a notebook!
More resources linked in the class notes.
Python supports numbers. We can write integers:
7
7
Floating-point numbers:
3.5
3.5
Numbers with scientific notation:
6.02e23
6.02e+23
The usual arithmetic operations (+
, -
, /
, *
) work as expected from math and other programming languages:
5 + 2
7
2 + 3 * 6
20
As do parentheses:
(2 + 3) * 6
30
The **
operator computes powers:
2 ** 5
32
import numpy as np
np.log(20)
2.995732273553991
A variable lets us give a name to a value:
x = 7
Just assign - no declaration necessary.
Then we can use them:
x + 5
7
Let's seen an example of changing the variable:
x = 2
Python strings are written in double ("
) or single ('
) quotes — there is no difference.
"Hello, world"
'Hello, world'
Within a string, \
is an escape character, e.g. to include a quote:
"Hello, \"world\""
'Hello, "world"'
+
concatenates strings:
'hello' + 'world'
'helloworld'
The split
method separates a string into a list, by default on whitespace:
'hello world'.split()
['hello', 'world']
Python is strict about types - it won't auto-convert:
'maroon' + 5
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-17-bdfad87c9cc7> in <module> ----> 1 'maroon' + 5 TypeError: can only concatenate str (not "int") to str
We can convert to a string with the str
function:
'maroon' + str(5)
'maroon5'
6 + 7
13
A function, in this case from a module:
np.log(10)
2.302585092994046
A method, a function attached to an object:
'hello world'.split()
['hello', 'world']
The split()
method returned a list:
'hello world'.split()
['hello', 'world']
We can write lists:
['martin', 'cross', 'gripps']
['martin', 'cross', 'gripps']
Let's save a list in a variable:
rowdy3 = ['martin', 'cross', 'gripps']
We can add to the list:
rowdy3.append('vogel')
rowdy3
['martin', 'cross', 'gripps', 'vogel']
Lists are indexed, starting with 0:
rowdy3[0]
'martin'
They can be indexed backwards from the end:
rowdy3[-1]
'vogel'
A slice takes multiple elements from a list:
rowdy3[1:3]
['cross', 'gripps']
len(rowdy3)
4
We can loop over a list:
for person in rowdy3:
print(person)
martin cross gripps vogel
Let's see 2 more tricks - including the position in our loop, and including variables in a string (an f-string):
for i, person in enumerate(rowdy3):
print(f'Member {i}: {person}')
Member 0: martin Member 1: cross Member 2: gripps Member 3: vogel
Python for
loops iterate over an iterable, like a list.
To loop over a sequence of numbers, use a range
:
for i in range(3):
print(i)
0 1 2
A Python tuple is like a list, except its size cannot be changed. Used for representing pairs, etc.:
coords = (3, 5)
coords[0]
3
coords[1]
5
A tuple can be unpacked:
x, y, z = coords
x
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-36-4ddec07835a7> in <module> ----> 1 x, y, z = coords 2 x ValueError: not enough values to unpack (expected 3, got 2)
A dictionary maps keys — often strings — to values:
diets = {
'rabbit': 'plants',
'dog': 'meat',
'vulture': 'carrion'
}
We can look up a value by its key:
diets['rabbit']
'plants'
Everything in Python is an object, which has a type.
We've seen these types:
int
str
(string)list
tuple
dict
(dictionary)There is a lot more to do with these - see the readings!
Variables store references to objects. This matters for mutable objects:
rowdy3
['martin', 'cross', 'gripps', 'vogel']
rowdy5 = rowdy3
rowdy5.append('amanda')
rowdy5
['martin', 'cross', 'gripps', 'vogel', 'amanda']
But rowdy5
and rowdy3
are references to the same list object:
rowdy3
['martin', 'cross', 'gripps', 'vogel', 'amanda']