In [ ]:
a = [1,2,3]
b = [4,5,6]
c = a+b
print(c)
You can see, adding two lists just results in a longer list, catenation of the two.
In [ ]:
a.append(b)
print(a)
In [ ]:
In [ ]:
def sum(data):
""" sum the elements of an array
"""
asum = 0.0
for i in data:
asum = asum + i
return asum
In [ ]:
In [ ]:
# the length of the array is defined here, and re-used below
# to test performance, we can make this number very large
# 1000, 1000000 etc.
n = 10
In [ ]:
%%time
a = list(range(n))
In [ ]:
In [ ]:
%%time
print(sum(a))
In [ ]:
In [ ]:
import numpy as np
In [ ]:
%%time
a=np.arange(n)
In [ ]:
%%time
print(sum(a))
In [ ]:
#%%time
%time print(a.sum())
%time print(a.sum())
In [ ]:
a=np.arange(10)
b=np.arange(10)
c = a + b
d = 3*a*a + b + 2.0
print(c)
print(d)
In [ ]:
c.shape
In [ ]:
c2=c.reshape(5,2)
c3=c.reshape(2,5)
In [ ]:
print(c)
print(c2)
print(c3)
type(c)
In [ ]:
c[0]=999
In [ ]:
print(c2)
In [ ]:
d2=c.reshape(5,2)[1:3,:]
In [ ]:
print(d2)
In [ ]:
d2[1,1]=888
In [ ]:
print(c)
In [ ]:
print(c2)
Finally, this notebook can also be saved in python, and run through ipython (or python if no cell_magic was used). Try this out!
Notice the difference in running it via
python 03-arrays.py
and
ipython 03-arrays.py