Crash course in Jupyter and Python¶
Introduction to Jupyter
Using Markdown
Magic functions
REPL
Saving and exporting Jupyter notebooks
Python
Data types
Operators
Collections
Functions and methods
Control flow
Loops, comprehension
Packages and namespace
Coding style
Understanding error messages
Getting help
Class Repository¶
Course material will be posted here. Please make any personal modifications to a copy of the notebook to avoid merge conflicts.
Introduction to Jupyter¶
User interface and kernels
Notebook, editor, terminal
Literate programming
Code and markdown cells
Menu and toolbar
Key bindings
Polyglot programming
In [1]:
%load_ext rpy2.ipython
In [2]:
import warnings
warnings.simplefilter('ignore', FutureWarning)
In [3]:
df = %R iris
In [4]:
df.head()
Out[4]:
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species | |
---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
In [5]:
%%R -i df -o res
library(tidyverse)
res <- df %>% group_by(Species) %>% summarize_all(mean)
/usr/local/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: ── Attaching packages ─────────────────────────────────────── tidyverse 1.2.1 ──
warnings.warn(x, RRuntimeWarning)
/usr/local/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: ✔ ggplot2 3.1.0 ✔ purrr 0.2.5
✔ tibble 1.4.2 ✔ dplyr 0.7.8
✔ tidyr 0.8.2 ✔ stringr 1.3.1
✔ readr 1.2.1 ✔ forcats 0.3.0
warnings.warn(x, RRuntimeWarning)
/usr/local/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
warnings.warn(x, RRuntimeWarning)
In [6]:
res
Out[6]:
Species | Sepal.Length | Sepal.Width | Petal.Length | Petal.Width |
---|---|---|---|---|
setosa | 5.006000 | 3.428000 | 1.462000 | 0.246000 |
versicolor | 5.936000 | 2.770000 | 4.260000 | 1.326000 |
virginica | 6.588000 | 2.974000 | 5.552000 | 2.026000 |
Using Markdown¶
What is markdown?
Headers
Formatting text
Syntax-highlighted code
Lists
Hyperlinks and images
LaTeX
See Help | Markdown
Magic functions¶
%magic
Shell access
Convenience functions
Quick and dirty text files
In [7]:
%magic
Saving and exporting Jupyter notebooks¶
The File menu item
Save and Checkpoint
Exporting
Close and Halt
Cleaning up with the Running tab
In [ ]:
Introduction to Python¶
General purpose language (web, databases, introductory programming classes)
Language for scientific computation (physics, engineering, statistics, ML, AI)
Human readable
Interpreted
Dynamic typing
Strong typing
Multi-paradigm
Implementations (CPython, PyPy, Jython, IronPython)
In [ ]:
Data types¶
boolean
int, double, complex
strings
None
In [9]:
True, False
Out[9]:
(True, False)
In [10]:
1, 2, 3
Out[10]:
(1, 2, 3)
In [11]:
import numpy as np
np.pi, np.e
Out[11]:
(3.141592653589793, 2.718281828459045)
In [12]:
3 + 4j
Out[12]:
(3+4j)
In [13]:
'hello, world'
Out[13]:
'hello, world'
In [14]:
"hell's bells"
Out[14]:
"hell's bells"
In [15]:
"""三轮车跑的快
上面坐个老太太
要五毛给一块
你说奇怪不奇怪"""
Out[15]:
'三轮车跑的快\n上面坐个老太太\n要五毛给一块\n你说奇怪不奇怪'
In [16]:
None
In [17]:
None is None
Out[17]:
True
Operators¶
mathematical
logical
bitwise
membership
identity
assignment and in-place operators
operator precedence
Arithmetic¶
In [18]:
2 ** 3
Out[18]:
8
In [19]:
11 / 3
Out[19]:
3.6666666666666665
In [20]:
11 // 3
Out[20]:
3
In [21]:
11 % 3
Out[21]:
2
Logical¶
In [22]:
True and False
Out[22]:
False
In [23]:
True or False
Out[23]:
True
In [24]:
not (True or False)
Out[24]:
False
Relational¶
In [25]:
2 == 2, 2 == 3, 2 != 3, 2 < 3, 2 <= 3, 2 > 3, 2 >= 3
Out[25]:
(True, False, True, True, True, False, False)
Bitwise¶
In [26]:
format(10, '04b')
Out[26]:
'1010'
In [27]:
format(7, '04b')
Out[27]:
'0111'
In [28]:
x = 10 & 7
x, format(x, '04b')
Out[28]:
(2, '0010')
In [29]:
x = 10 | 7
x, format(x, '04b')
Out[29]:
(15, '1111')
In [30]:
x = 10 ^ 7
x, format(x, '04b')
Out[30]:
(13, '1101')
Membership¶
In [31]:
'hell' in 'hello'
Out[31]:
True
In [32]:
3 in range(5), 7 in range(5)
Out[32]:
(True, False)
In [33]:
'a' in dict(zip('abc', range(3)))
Out[33]:
True
Identity¶
In [34]:
x = [2,3]
y = [2,3]
x == y, x is y
Out[34]:
(True, False)
In [35]:
id(x), id(y)
Out[35]:
(5142288200, 5142289032)
In [36]:
x = 'hello'
y = 'hello'
In [37]:
x == y, x is y
Out[37]:
(True, True)
In [38]:
id(x), id(y)
Out[38]:
(5142210240, 5142210240)
Assignment¶
In [39]:
x = 2
In [40]:
x = x + 2
In [41]:
x
Out[41]:
4
In [42]:
x *= 2
In [43]:
x
Out[43]:
8
Collections¶
Sequence containers - list, tuple
Mapping containers - set, dict
The
`collections
<https://docs.python.org/2/library/collections.html>`__ module
Tuples¶
In [46]:
ys = (1,2,3)
ys[0], ys[-1]
Out[46]:
(1, 3)
In [47]:
try:
ys[1] = 9
except TypeError as e:
print(e)
'tuple' object does not support item assignment
Dictionaries¶
In [50]:
{'a': 0, 'b': 1, 'c': 2}
Out[50]:
{'a': 0, 'b': 1, 'c': 2}
In [51]:
dict(a=0, b=1, c=2)
Out[51]:
{'a': 0, 'b': 1, 'c': 2}
In [52]:
dict(zip('abc', range(3)))
Out[52]:
{'a': 0, 'b': 1, 'c': 2}
Functions and methods¶
Anatomy of a function
Docstrings
Class methods
In [53]:
list(range(10))
Out[53]:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [54]:
[item for item in dir() if not item.startswith('_')]
Out[54]:
['In',
'Out',
'df',
'exit',
'get_ipython',
'np',
'quit',
'res',
'warnings',
'x',
'xs',
'y',
'ys',
'zs']
In [55]:
def f(a, b):
"""Do something with a and b.
Assume that the + and * operatores are defined for a and b.
"""
return 2*a + 3*b
In [56]:
f(2, 3)
Out[56]:
13
In [57]:
f(3, 2)
Out[57]:
12
In [58]:
f(b=3, a=2)
Out[58]:
13
In [59]:
f(*(2,3))
Out[59]:
13
In [60]:
f(**dict(a=2, b=3))
Out[60]:
13
In [61]:
f('hello', 'world')
Out[61]:
'hellohelloworldworldworld'
In [62]:
f([1,2,3], ['a', 'b', 'c'])
Out[62]:
[1, 2, 3, 1, 2, 3, 'a', 'b', 'c', 'a', 'b', 'c', 'a', 'b', 'c']
Control flow¶
if and the ternary operator
Checking conditions - what evaluates as true/false?
if-elif-else
while
break, continue
pass
In [63]:
if 1 + 1 == 2:
print("Phew!")
Phew!
In [64]:
'vegan' if 1 + 1 == 2 else 'carnivore'
Out[64]:
'vegan'
In [65]:
'vegan' if 1 + 1 == 3 else 'carnivore'
Out[65]:
'carnivore'
In [66]:
if 1+1 == 3:
print("oops")
else:
print("Phew!")
Phew!
In [67]:
for grade in [94, 79, 81, 57]:
if grade > 90:
print('A')
elif grade > 80:
print('B')
elif grade > 70:
print('C')
else:
print('Are you in the right class?')
A
C
B
Are you in the right class?
In [68]:
i = 10
while i > 0:
print(i)
i -= 1
10
9
8
7
6
5
4
3
2
1
In [69]:
for i in range(1, 10):
if i % 2 == 0:
continue
print(i)
1
3
5
7
9
In [70]:
for i in range(1, 10):
if i % 2 == 0:
break
print(i)
1
In [71]:
for i in range(1, 10):
if i % 2 == 0:
pass
else:
print(i)
1
3
5
7
9
Loops and comprehensions¶
for, range, enumerate
lazy and eager evaluation
list, set, dict comprehensions
generator expression
In [72]:
for i in range(1,5):
print(i**2, end=',')
1,4,9,16,
In [73]:
for i, x in enumerate(range(1,5)):
print(i, x**2)
0 1
1 4
2 9
3 16
In [74]:
for i, x in enumerate(range(1,5), start=10):
print(i, x**2)
10 1
11 4
12 9
13 16
In [75]:
range(5)
Out[75]:
range(0, 5)
In [76]:
list(range(5))
Out[76]:
[0, 1, 2, 3, 4]
Comprehensions¶
In [77]:
[x**3 % 3 for x in range(10)]
Out[77]:
[0, 1, 2, 0, 1, 2, 0, 1, 2, 0]
In [78]:
{x**3 % 3 for x in range(10)}
Out[78]:
{0, 1, 2}
In [79]:
{k: v for k, v in enumerate('abcde')}
Out[79]:
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e'}
In [80]:
(x**3 for x in range(10))
Out[80]:
<generator object <genexpr> at 0x12edc1de0>
In [81]:
list(x**3 for x in range(10))
Out[81]:
[0, 1, 8, 27, 64, 125, 216, 343, 512, 729]
Packages and namespace¶
Modules (file)
Package (hierarchical modules)
Namespace and naming conflicts
Using
import
In [82]:
%%file foo.py
def foo(x):
return f"And FOO you too, {x}"
Overwriting foo.py
In [83]:
import foo
In [84]:
foo.foo("Winnie the Pooh")
Out[84]:
'And FOO you too, Winnie the Pooh'
In [85]:
from foo import foo
In [86]:
foo("Winnie the Pooh")
Out[86]:
'And FOO you too, Winnie the Pooh'
In [87]:
import numpy as np
In [88]:
np.random.randint(0, 10, (5,5))
Out[88]:
array([[1, 8, 6, 7, 0],
[9, 5, 5, 6, 5],
[0, 5, 1, 2, 6],
[3, 3, 9, 8, 9],
[8, 6, 5, 0, 8]])
Coding style¶
Many code editors can be used with linters to check if your code conforms to PEP 8 style guidelines.
E.g. see jupyter-autopep8
Understanding error messages¶
In [89]:
try:
1 / 0
except ZeroDivisionError as e:
print(e)
division by zero
Getting help¶
?foo
,foo?
,help(foo)
Use a search engine
Use
StackOverflow
Ask your TA
In [90]:
help(help)
Help on _Helper in module _sitebuiltins object:
class _Helper(builtins.object)
| Define the builtin 'help'.
|
| This is a wrapper around pydoc.help that provides a helpful message
| when 'help' is typed at the Python interactive prompt.
|
| Calling help() at the Python prompt starts an interactive help session.
| Calling help(thing) prints help for the python object 'thing'.
|
| Methods defined here:
|
| __call__(self, *args, **kwds)
| Call self as a function.
|
| __repr__(self)
| Return repr(self).
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
In [ ]: