In [1]:
def times_tables():
lst = []
for i in range(10):
for j in range (10):
lst.append(i*j)
return lst
In [2]:
times_tables() == [i*j for i in range(10) for j in range(10)]
Out[2]:
In [4]:
# getting all the possible combinations e.g, aa01, i.e two letters then 2 nos
lowercase = 'abcdefghijklmnopqrstuvwxyz'
digits = '0123456789'
answer = [a+b+c+d for a in lowercase for b in lowercase
for c in digits for d in digits]
In [6]:
answer[:6]
Out[6]:
In [7]:
import numpy as np
In [11]:
np.diag([2,3])
Out[11]:
In [12]:
np.array([1,2,3]*3)
Out[12]:
In [13]:
np.repeat([1,2,3],2)
Out[13]:
In [15]:
p = np.ones([2,3],int)
p
Out[15]:
In [16]:
np.vstack([p, 2*p])
Out[16]:
In [17]:
np.hstack([p, 2*p])
Out[17]:
In [19]:
p.dtype
Out[19]:
In [20]:
p.T
Out[20]:
In [21]:
p.T.shape
Out[21]:
In [24]:
p.astype('f')
Out[24]:
In [25]:
a = np.array([-4,-2,1,3])
In [26]:
a.sum()
Out[26]:
In [28]:
a.argmax() # index of the maximum value
Out[28]:
In [29]:
a.argmin()
Out[29]:
In [33]:
s = np.arange(13)**2
s
Out[33]:
In [34]:
s[0], s[4], s[0:3]
Out[34]:
In [35]:
s[-4:]
Out[35]:
In [37]:
s[-5::-1]
Out[37]:
In [38]:
r = np.arange(36)
r.resize(6,6)
r
Out[38]:
In [40]:
r[2,3]
Out[40]:
In [41]:
r[3, 3:6]
Out[41]:
In [42]:
r[:2, :-1]
Out[42]:
In [43]:
r[-1, ::2]
Out[43]:
In [44]:
r[r > 30]
Out[44]:
In [46]:
r[r>30] = 30
r
Out[46]:
In [48]:
r2 = r[:3,:3]
r2
Out[48]:
In [50]:
r2[:] = 0
r2
Out[50]:
In [51]:
r # the elements in r are affected by the operation performed on r2
Out[51]:
In [56]:
r_copy = r.copy()
r_copy
Out[56]:
In [57]:
r_copy[:] = 10
r_copy
Out[57]:
In [58]:
r
Out[58]:
In [59]:
test = np.random.randint(0,10, (4,3))
test
Out[59]:
In [60]:
for row in test:
print(row)
In [61]:
for i in range(len(test)):
print(test[i])
In [62]:
for i, row in enumerate(test):
print('row', i, 'is', row)
In [63]:
test2 = test**2
test2
Out[63]:
In [64]:
for i,j in zip(test, test2):
print(i, '+', j, '=', i+j)
In [65]:
type(lambda x: x+1)
Out[65]:
In [67]:
m = np.arange(36)
m.resize(6,6)
m
Out[67]:
In [75]:
m.reshape(36)[::7]
Out[75]:
In [76]:
m[2:4,2:4]
Out[76]:
In [ ]: