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.
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')
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.
In [ ]:
# This is a comment
#
# This is a multi line comment
#
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)
In [ ]:
f(a, b, c=5, d='foo')
is equal to
In [ ]:
f(a, b, d='foo', c=5)
In [ ]:
a = [1, 2, 3]
b = a
b.append(4)
In [ ]:
b
In [ ]:
a
functions are also called by reference.
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))
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 are accessed using the obj.attribute or the obj.methods() notation.
In [ ]:
a = 'foo'
a.count('o')
In [ ]:
a.<TAB>
Python modules are simple <name>.py files container functions, classes and variables.
In [1]:
%%bash
ls -la material/foo.py
In [2]:
%%bash
cat material/foo.py
In [ ]:
from foo import PI as pi
from foo import f
import foo
In [ ]:
f()
In [ ]:
pi
In [ ]:
foo.f()
In [ ]:
foo.PI
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')
In [ ]:
10000000 + 10000000 is 20000000
In [ ]:
10000000 + 10000000 == 20000000
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)
Python has a couple of build in scalar types:
int (32-bit, with automatic conversion to 64-bit)boolNonefloat (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)