In [1]:
def addme(x, y):
return x + y
In [2]:
addme(4)
In [3]:
addme(2,3)
Out[3]:
In [4]:
def addme2(x, y=2):
return x + y
In [5]:
addme2(5)
Out[5]:
In [6]:
addme2(5, y=10)
Out[6]:
In [7]:
addme2(x=6, y=10)
Out[7]:
In [8]:
addme2(y=10, x=6)
Out[8]:
In [41]:
d = dict(x=20, y=30)
addme2(**d)
Out[41]:
In [9]:
addme2(4,5)
Out[9]:
In [10]:
def myfunc(*args, **kwargs):
print(args)
print(kwargs)
In [11]:
myfunc(1,2,3,4,5)
In [12]:
myfunc(1,2,3, a=10, b=20)
In [37]:
def myplot(x, y, **kwargs):
# Do some stuff
# ...
newx = x*10
newy = y*100
print(kwargs)
plt.plot(newx, newy, **kwargs)
In [38]:
# Don't try the following line in python
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(10)
y = np.arange(10)
In [39]:
x, y
Out[39]:
In [40]:
myplot(x, y, color='red', lw=5)
In [31]:
x
Out[31]:
In [32]:
y
Out[32]:
In [42]:
s = 'timothy d. morton'
type(s)
Out[42]:
In [43]:
s.split()
Out[43]:
In [44]:
s.upper()
Out[44]:
In [67]:
from math import sqrt
class Coord(object):
def __init__(self, x, y):
print('I am running the constructor!')
self.x = x
self.y = y
def distance_from_origin(self):
return sqrt(self.x**2 + self.y**2)
def __add__(self, other):
assert type(other) is Coord, 'Must add another Coord'
newx = self.x + other.x
newy = self.y + other.y
return Coord(newx, newy)
In [68]:
c = Coord(4,5)
In [69]:
c + 2
In [71]:
c2 = Coord(10,20)
c3 = c + c2
In [73]:
c3.x, c3.y
Out[73]:
In [55]:
c.x, c.y
Out[55]:
In [56]:
type(c)
Out[56]:
In [57]:
c.distance_from_origin()
Out[57]:
In [58]:
c + 2
In [74]:
pi
In [75]:
sin(1)
In [76]:
import math
math.pi
Out[76]:
In [77]:
math.sin(math.pi)
Out[77]:
In [78]:
from math import pi
pi
Out[78]:
In [79]:
from math import *
In [80]:
log
Out[80]:
In [81]:
asin
Out[81]:
In [82]:
sin = 7
In [83]:
sin(0.5)
In [84]:
mylist = [1,2,3,4,5]
In [86]:
mylist*2
Out[86]:
In [87]:
list2 = ['a', 'b', 'c']
mylist + list2
Out[87]:
In [89]:
import numpy as np
In [90]:
myarr = np.array(mylist)
In [91]:
myarr
Out[91]:
In [92]:
myarr + 3
Out[92]:
In [93]:
myarr * 100
Out[93]:
In [94]:
myarr**2
Out[94]:
In [95]:
np.arange(10)
Out[95]:
In [96]:
np.arange(10, 20, 2)
Out[96]:
In [97]:
np.linspace(50, 60, 100)
Out[97]:
In [99]:
np.arange(50, 60, 0.05)
Out[99]:
In [100]:
np.zeros(5)
Out[100]:
In [101]:
np.ones(5)
Out[101]:
In [102]:
10*np.ones(5)
Out[102]:
In [105]:
np.zeros((3,4))
Out[105]:
In [106]:
np.arange(12).reshape((3,4))
Out[106]:
In [108]:
x = np.arange(12)
x.reshape((3,4))
Out[108]:
Here's a little "gotcha":
In [114]:
x = np.arange(10)
y = x # be sure you know what this line does!
x, y
Out[114]:
In [115]:
y[5] = 100
In [116]:
x
Out[116]:
In [117]:
x = np.arange(10)
y = x.copy()
y[5] = 100
x, y
Out[117]:
In [118]:
type(x)
Out[118]:
In [119]:
x = [1,2,3,4,5]
y = x
y[2] = 100
x, y
Out[119]:
Let's discuss indexing!
In [123]:
x = np.random.random(10)
In [124]:
x
Out[124]:
In [125]:
x[2]
Out[125]:
In [126]:
x[2:5]
Out[126]:
In [127]:
x[-1]
Out[127]:
In [129]:
x[1:-1]
Out[129]:
In [130]:
trim = 2
x[trim: -1*trim]
Out[130]:
In [131]:
x = np.random.random((4,5))
In [132]:
x
Out[132]:
In [133]:
x[1, 3]
Out[133]:
In [135]:
x[0:2, 0:2]
Out[135]:
In [136]:
x[:, 1]
Out[136]:
In [137]:
y = x[0:2, 0:2] # this does not copy data! just returns a "view"
y
Out[137]:
In [138]:
y[0,0] = 100
y
Out[138]:
In [139]:
x
Out[139]:
In [140]:
np.shares_memory(x, y)
Out[140]:
In [141]:
x = np.random.random(10)
x
Out[141]:
In [143]:
x > 0.5
Out[143]:
In [144]:
(x > 0.5).sum()
Out[144]:
In [145]:
sum(x > 0.5)
Out[145]:
In [146]:
x[x < 0.3]
Out[146]:
In [147]:
mask = x < 0.3
x[mask]
Out[147]:
In [150]:
np.where(x < 0.3)
Out[150]:
In [151]:
w = np.where(x < 0.3)
x[w]
Out[151]:
In [149]:
x[[1,3,5,6]]
Out[149]:
This command allows the plots to show up in the notebook (not a python command!):
In [110]:
%matplotlib inline
In [111]:
import matplotlib.pyplot as plt
In [112]:
x = np.linspace(0, 1, 100)
y = x**2
plt.plot(x, y)
Out[112]:
In [122]:
x = 5
y = x
y = 6
x, y
Out[122]:
In [152]:
import re
In [153]:
s = 'hello, how are you?'
In [154]:
words = s.split()
In [155]:
words
Out[155]:
In [161]:
m = re.search('\w+', words[0])
In [162]:
m.group(0)
Out[162]:
In [158]:
s = 'abcbca'
m = re.search('b(cb)', s)
In [159]:
m.group(0)
Out[159]:
In [160]:
m.group(1)
Out[160]:
In [165]:
m = re.search('\w+', "it's")
m.group(0)
Out[165]:
In [ ]:
^ $