This session is going to cover some basics of Python and Jupyter notebooks that I have found useful or wish I learned sooner.
I assume you know some programming, so I won't specifically cover loops, logic statements, or functions.
Stop me with questions as we go.
pip is the default python package manager, and is used to install packages from PyPI. Some packages are only available on PyPI, so you'll need to use pip to get them.
Unless you have to use pip, I recommend sticking with conda to manage packages.
Each can't manage packages installed by the other, so it's easy to end up with duplicate versions.
conda install ...
conda update ...
conda remove ...
Unfortunately pip has different syntax
It's also worth reading this blog that talks about the history and differences between conda and pip.
This is just to get you started.
Read the Jupyter documentation (Quick Start or Main) for more information.
Open a command prompt or terminal window and navigate to the parent folder where you are working
Type jupyter notebook to launch the notebook server
esc exits out of edit mode (green box) and puts the cell in command mode (blue box)shift-enter runs a cell and moves to the next cellm changes a cell to markdown, 1-6 makes it markdown as a section headerdd deletes a cellc copies a cell, v pastes it below the currently selected cellAssigning variables in Python is as easy as putting a variable name to the left of the equals (=) sign.
In [1]:
x = 4
print x, type(x)
In [3]:
x = 'hello'
print x, type(x)
In [4]:
x = 1 # x is an integer
x = 'hello' # now x is a string
x = [1, 2, 3] # now x is a list
print x
print type(x)
print len(x)
Two variables can point to the same object. Be careful, as this can lead to unanticipated consequences!
In [5]:
x = [1, 2, 3]
y = x
print y
In [6]:
x.append(4)
print y
Fortunately, simple objects are immutable - you can't change the value of "10" in memory, but you can point a variable to a different value
In [7]:
x = 10
y = x
# add 5 to x's value, and assign it to x
x += 5
print "x =", x
print "y =", y
In [10]:
# 3/2
3/2.
Out[10]:
In [13]:
from __future__ import division
3/2
Out[13]:
Common basic Python objects include:
| Type | Example | Description |
|---|---|---|
| list | [1, 2, 3] | Ordered collection |
| tuple | (1, 2, 3) | Immutable ordered collection |
| dict | {'a':1, 'b':2, 'c':3} | Unordered (key,value) mapping |
| set | {1, 2, 3} | Unordered collection of unique values |
In [14]:
L = [2, 3, 5, 7] # Define a list
print L
print len(L)
Addition concatenates lists.
Lists can be a combination of different object types, including other lists
In [18]:
# L = L + [13, 17, 19, ['a', 'b']]
print L
In [19]:
L = [2, 3, 5, 7, 11]
In [20]:
L[0] # Python is 0 indexed
Out[20]:
In [24]:
L[:-2]
Out[24]:
In [25]:
L[0:3] # element 3 is not included [)
Out[25]:
A few quick examples of easy logic statements
In [26]:
print L
In [27]:
2 in L
Out[27]:
In [28]:
4 in L
Out[28]:
In [29]:
(2 in L) and (4 not in L)
Out[29]:
In [36]:
# range generates a list from 0 to n-1
# Behavior is different in Python 3
range(5)
Out[36]:
In [37]:
%%timeit
l = [] # Iniitalize an empty list
for value in range(500):
l.append(value**2)
In [38]:
%%timeit
[value**2 for value in range(500)]
In [40]:
import numpy as np
np.array(range(5))
Out[40]:
In [41]:
%%timeit
np.array(range(500))**2
In [42]:
t = (1, 2, 3)
In [43]:
t = 1, 2, 3
print t
print t[0]
In [44]:
t[1] = 4
In [45]:
def f(x):
a = x**2
b = x**3
return a, b
f(2)
Out[45]:
In [46]:
a, b = f(2)
print 'a =', a
print 'b =', b
I defined a function above. Functions start with def, end the first line with a colon, and use return if you are returning values
In [47]:
numbers = {'one':1, 'two':2, 'three':3}
numbers
Out[47]:
In [48]:
numbers['two'] # Use a key to return the matching value
Out[48]:
In [49]:
numbers.keys() # Keys don't stay ordered
Out[49]:
In [54]:
l = [1, 2, 1, 4, 5, 2]
set(l)
Out[54]:
I said list-like objects, which points to an important feature of Python: types don't matter so long as they behave correctly (duck typing)
In [56]:
l_array = np.array(l) # make a numpy array from the list
l_array
# l_array.reshape((2,3))
Out[56]:
In [52]:
set(l_array)
Out[52]:
In [57]:
for idx, value in enumerate(['dog', 'cat', 'pig', 'sheep']):
print idx, value
In [61]:
L = [2, 4, 6, 8, 10]
R = [3, 6, 9, 12, 15]
for lval, rval in zip(L, R):
print (lval, rval)
# zip(L, R)[0]
# Doing the same thing with indexing
# for i in range(len(L)):
# print (L[i], R[i])
Out[61]:
Check out this dramatic tour through plotting with several different libraries