In [1]:
xs = [5, 6, 2, 3]
xs
Out[1]:
In [2]:
xs[0]
Out[2]:
In [3]:
xs[-1]
Out[3]:
In [4]:
xs[2] = 10
xs
Out[4]:
In [5]:
xs[0] = "a book"
xs
Out[5]:
In [6]:
xs[1] = [3, 4]
xs
Out[6]:
In [7]:
xs += [99, 100]
xs
Out[7]:
In [8]:
xs.extend([22, 33])
xs
Out[8]:
In [9]:
xs[-1]
Out[9]:
In [10]:
xs.pop()
Out[10]:
In [11]:
xs
Out[11]:
In [12]:
len(xs)
Out[12]:
In [13]:
xs[0:2]
Out[13]:
In [14]:
xs[2:]
Out[14]:
In [15]:
xs[:3]
Out[15]:
In [16]:
xs[:-2]
Out[16]:
In [17]:
for el in xs:
print(el)
In [18]:
for idx, el in enumerate(xs):
print(idx, el)
In [19]:
for idx in range(len(xs)):
print(idx)
In [20]:
for idx in range(2, len(xs)):
print(idx)
In [21]:
for idx in range(0, len(xs), 2):
print(idx)
In [22]:
xs = []
for x in range(10):
xs.append(x ** 2)
xs
Out[22]:
In [23]:
[x ** 2 for x in range(10)]
Out[23]:
In [24]:
[x ** 2 for x in range(10) if x % 2 == 0]
Out[24]:
In [25]:
[x ** 2 if x % 2 == 0 else 512 for x in range(10)]
Out[25]:
In [26]:
for a, b in zip([1, 2, 3], [4, 5, 6]):
print(a, b)
In [27]:
for a, b in zip([1, 2, 3], [4, 5, 6, 7]):
print(a, b)
In [28]:
a_list, b_list = zip(*[(1, 4), (2, 3), (5, 6)])
print(a_list)
print(b_list)
In [29]:
cool_name = "Miyazaki"
cool_name
Out[29]:
In [30]:
cool_name + " is " + "great"
Out[30]:
In [31]:
num_people = 200000
In [32]:
cool_name + " has " + num_people + " citizens"
In [33]:
cool_name + " has " + str(num_people) + " citizens"
Out[33]:
In [34]:
len(cool_name)
Out[34]:
In [35]:
print("{0} has {1} citizens".format(cool_name, num_people))
In [36]:
class Product:
def __init__(self, product_name=None, tags=None, price=0.0):
self.product_name = product_name
self.tags = [] if tags is None else tags
self.price = price
vat = 0.25
def product_price(self, with_pdv=False):
if with_pdv:
return self.price * (1 + self.vat)
else:
return self.price
def contains_tag(self, tag):
return (tag in self.tags)
In [37]:
prod = Product(product_name="toilet paper", tags=["health", "toilet", "fresh"], price=10)
print(prod.product_price(with_pdv=True))
print(prod.product_price(with_pdv=False))
In [38]:
prod.contains_tag("fresh")
Out[38]:
In [39]:
prod.contains_tag("money")
Out[39]:
In [40]:
prod1 = Product(product_name="toilet paper", price=10)
prod2 = Product(product_name="toothbrush", price=10)
Product.vat = 0.5
prod1.vat = 0.3
In [41]:
prod1.product_price(with_pdv=True)
Out[41]:
In [42]:
prod2.product_price(with_pdv=True)
Out[42]:
In [43]:
import numpy as np
In [44]:
a = np.array([1, 2, 3])
a
Out[44]:
In [45]:
c = np.array([1, 2, 3], dtype=np.float64)
c
Out[45]:
In [46]:
a.shape
Out[46]:
In [47]:
b = np.array([[1, 3, 4], [2, 3, 5]])
b
Out[47]:
In [48]:
b.shape
Out[48]:
In [49]:
b[1, 2]
Out[49]:
In [50]:
b[0:1, 2] # Notice the difference
Out[50]:
In [51]:
b[:, 2]
Out[51]:
In [52]:
b[0:2, 2]
Out[52]:
In [53]:
d = np.array([[1, 2, 3], [4, 5, 6]])
d
Out[53]:
In [54]:
np.append(d, 1)
Out[54]:
In [55]:
np.append(d, [1, 2])
Out[55]:
In [56]:
d
Out[56]:
In [57]:
to_add = np.array([[1], [2]])
to_add
Out[57]:
In [58]:
d + to_add
Out[58]:
In [59]:
np.hstack([d, to_add])
Out[59]:
In [60]:
np.vstack([d, to_add])
In [61]:
to_add2 = np.array([2, 3, 4])
to_add2
Out[61]:
In [62]:
result = np.vstack([d, to_add2])
result
Out[62]:
In [63]:
np.sqrt(result)
Out[63]:
In [64]:
result * 2
Out[64]:
In [65]:
result + result
Out[65]:
In [66]:
x = np.array([[1,2],[3,4]])
y = np.array([[5,6],[7,8]])
v = np.array([1,2])
w = np.array([5,3])
print(x)
print(y)
print(v)
print(w)
In [67]:
v.dot(w)
Out[67]:
In [68]:
np.dot(v, w)
Out[68]:
In [69]:
x.dot(v)
Out[69]:
In [70]:
np.linalg.inv(x)
Out[70]:
In [71]:
np.linalg.det(x)
Out[71]:
In [72]:
np.linalg.norm(x)
Out[72]:
In [73]:
x
Out[73]:
In [74]:
np.max(x)
Out[74]:
In [75]:
np.max(x, axis=0)
Out[75]:
In [76]:
np.max(x, axis=1)
Out[76]:
In [77]:
p = np.array([2, 3, 1, 6, 4, 5])
b = ["a", "b", "c", "d", "e", "f"]
In [78]:
np.max(p)
Out[78]:
In [79]:
np.argmax(p)
Out[79]:
In [80]:
b[np.argmax(p)]
Out[80]:
In [81]:
np.mean(p)
Out[81]:
In [82]:
np.var(p)
Out[82]:
In [83]:
np.linspace(1, 10, 100)
Out[83]:
In [84]:
w = np.array([[1, 2, 3], [4, 5, 6]])
In [85]:
w > 2
Out[85]:
In [86]:
w[w>2]
Out[86]:
In [87]:
w = np.array([1, 2, 3, 4, 5, 6, 7, 8])
y = np.array([0, 0, 0, 1, 0, 0, 1, 0])
In [88]:
w[y==1]
Out[88]:
In [89]:
list(p)
Out[89]:
In [90]:
p.tolist()
Out[90]:
In [92]:
import matplotlib.pyplot as plt
%pylab inline
In [93]:
plt.plot([1,2,3,4,5], [4,5,5,7,3])
Out[93]:
In [94]:
plt.plot(np.array([1,2,3,4,5]), np.array([4,5,5,7,3]))
Out[94]:
In [95]:
plt.plot([4,5,5,7,3])
Out[95]:
In [96]:
plt.plot([4,5,5,7,3], 'x');
In [97]:
def f(x): return x**2
In [98]:
xs = linspace(0,100); xs
Out[98]:
In [99]:
f(xs)
Out[99]:
In [100]:
plt.plot(xs, f(xs))
Out[100]:
In [101]:
plt.plot(xs, f(xs), 'r+');
In [102]:
plt.plot(xs, 1 - f(xs), 'b', xs, f(xs)/2 - 1000, 'r--');
In [103]:
plt.plot(xs, f(xs), label='f(x)')
plt.plot(xs, 1 - f(xs), label='1-f(x)')
plt.legend(loc="center right")
plt.show()
In [104]:
plt.plot(xs, f(xs), label='f(x)')
plt.plot(xs, 1 - f(xs), label='1-f(x)')
plt.legend(loc="best")
plt.show()
In [105]:
plt.plot(xs, f(xs), label='f(x)')
plt.plot(xs, 1 - f(xs), label='1-f(x)')
plt.legend(loc="best")
plt.xlim([20, 80])
plt.show()
In [106]:
plt.plot(xs, f(xs), label=r'$\lambda^{3}$')
plt.plot(xs, 1 - f(xs), label=r'$\mathcal{N}$')
plt.legend(loc="best")
plt.xlim([20, 80])
plt.xlabel("hello")
plt.title("woohoo")
plt.show()
In [107]:
def f_1(x):
return np.sqrt((1 - (np.abs(x) - 1)**2))
def f_2(x):
return -3 * np.sqrt(1 - np.sqrt((np.abs(x)/2)))
x = np.linspace(-2, 2, 1000)
plt.plot(x, f_1(x), 'b-', label="bolja polovica")
plt.plot(x, f_2(x), 'r-', label="dobra polovica")
plt.xlim([-3, 3])
plt.ylim([-3, 1.5])
plt.xlabel("x")
plt.ylabel("y")
plt.legend(loc="lower right")
plt.show()
In [108]:
plt.scatter([0, 1, 2, 0], [4, 5, 2, 1])
plt.show()
In [109]:
plt.scatter([0,1,2,0], [4, 5, 2, 1], s=200, marker='s');
In [110]:
for c in 'rgb':
plt.scatter(np.random.random(100), np.random.random(100), s=200, alpha=0.5, marker='o', c=c)
In [111]:
plt.subplot(2,1,1)
plt.plot(xs, f(xs), label='f(x)', c="g")
plt.legend(loc="center right")
plt.subplot(2,1,2)
plt.plot(xs, f(xs), label='f(x)', c="r")
plt.legend(loc="center right")
plt.show()
In [112]:
plt.subplot(2,3,1)
plt.plot(xs, f(xs), label='f(x)', c="g")
plt.legend(loc="center right")
plt.subplot(2,3,2)
plt.plot(xs, f(xs), label='f(x)', c="r")
plt.legend(loc="center right")
plt.subplot(2,3,3)
plt.plot(xs, f(xs), label='f(x)', c="m")
plt.legend(loc="center right")
plt.subplot(2,3,4)
plt.plot(xs, f(xs), label='f(x)', c="c")
plt.legend(loc="center right")
plt.subplot(2,3,5)
plt.plot(xs, f(xs), label='f(x)', c="y")
plt.legend(loc="center right")
plt.subplot(2,3,6)
plt.plot(xs, f(xs), label='f(x)', c="w")
plt.legend(loc="center right")
plt.show()