Note: We are using Python here, not Python 2. This means we are using Python 3!
Note: This lecture comprises only those parts of Python that are fundamental to Machine Learning purposes.
Note: Stop writing, listen to me.

Why do I have to learn Python?


In [ ]:
from IPython.display import Image
Image('images/mem0.jpg')

In [ ]:
Image('images/mem1.jpg')

In [ ]:
Image('images/C++_machine_learning.png')

In [ ]:
Image('images/Java_machine_learning.png')

In [ ]:
Image('images/Python_machine_learning.png')

In [ ]:
Image('images/R_machine_learning.png')

Python is an interpreted language

  • fire up an interpreter
  • assign a value to a variable
  • print something
  • switch to ipython

Fundamentals

  • Python uses whitespaces (tabs and spaces) to structure code rather than braces.
  • Terminating semicolons are not required.

In [ ]:
def minimum(x, y):
    if x < y:
        return x
    else:
        return y

Why should one do this when designing a language?

When putting several statements into one line you must use semicolons to seperate them.


In [ ]:
a = 42; b = 7; c = 23

In [ ]:
print(a, b)

But for the sake of readability you shouldn't do so.

Comments


In [ ]:
# This is a comment

#
# This is a multi line comment
#

Functions and methods

Functions are called using parentheses with zero or more arguments. A value may be returned.


In [ ]:
foo = min(2, 3)
foo

Methods are called using this syntax


In [ ]:
obj.method(parameters)

Positional and keyword arguments


In [ ]:
f(a, b, c=5, d='foo')

is equal to


In [ ]:
f(a, b, d='foo', c=5)

References


In [ ]:
a = [1, 2, 3]
b = a
b.append(4)

In [ ]:
b

In [ ]:
a

functions are also called by reference.

Dynamic references

In contrast to Java, Python references are typeless. There is no problem with this:


In [ ]:
a = 5
print(type(a))
a = 'foo'
print(type(a))

Strong typing


In [ ]:
5 + '5'

Types can be checked using isinstance


In [ ]:
isinstance(4, int)

In [ ]:
isinstance(4.5, int)

You can also pass it a list of types


In [ ]:
isinstance(4.5, (int, float))

In [ ]:
minimum.bar = lambda x: x
minimum.bar('foo')

Attributes and methods

Attributes and methods are accessed using the obj.attribute or the obj.methods() notation.


In [ ]:
a = 'foo'
a.count('o')

In [ ]:
a.<TAB>

Imports

Python modules are simple <name>.py files container functions, classes and variables.


In [1]:
%%bash
ls -la material/foo.py


-rw-rw-r-- 1 user user 49 Mär 24 20:25 material/foo.py

In [2]:
%%bash
cat material/foo.py


PI = 3.14159

def f():
    return 'Hello, world'

In [ ]:
from foo import PI as pi
from foo import f
import foo

In [ ]:
f()

In [ ]:
pi

In [ ]:
foo.f()

In [ ]:
foo.PI

Operators

Most operators work as you might expect


In [ ]:
7 + 4

In [ ]:
5 / 2

In [ ]:
4 < 3

In [ ]:
4 ** 2

In [ ]:
id('this is a somewhat long text')

In [ ]:
id('this is a somewhat long text')

Equality

  • == tests for equivalence
  • is tests for identity

In [ ]:
10000000 + 10000000 is 20000000

In [ ]:
10000000 + 10000000 == 20000000

Mutability of objects

In Python there are mutable objects like lists, dicts and user-defined types (classes).


In [ ]:
a_list = [0, 1, 2]
a_list

In [ ]:
a_list[1] = 3
a_list

But there are also immutable objects like 'tuples' and strings


In [ ]:
a_tuple = (0, 1, 2)
a_tuple

In [ ]:
a_tuple[0] = 4

In [ ]:
foo = 'bar'

In [ ]:
id(foo)

In [ ]:
foo = foo + 'bar'

In [ ]:
id(foo)

Scalar Types

Python has a couple of build in scalar types:

  • int (32-bit, with automatic conversion to 64-bit)
  • bool
  • None
  • float (64-bit)
  • str

In [ ]:
4

In [ ]:
4 / 2

In [ ]:
7.3e-4

In [ ]:
float(43)

In [ ]:
str('foobar')

In [ ]:
'foobar'

In [ ]:
"foobar"

In [ ]:
""" multiline
foobar
"""

In [ ]:
foo = 'bar'
foo[1]

In [ ]:
str(4)

In [ ]:
int('4')

In [ ]:
s = 'foo bar'

In [ ]:
s[0:3]

In [ ]:
list(s)

In [ ]:
s = 'bob\'s bar'
print(s)

In [ ]:
a = 'foo'
b = 'bar'
a + b

In [ ]:
pi = 3.14159
'pi is {:.2f}'.format(pi)