import numpy as n
x = n.array([1,2,3,4,5])
x.dtype
dtype('int64')
x = n.array([1.,2.,3.,4.,5.])
x.dtype
dtype('float64')
x = n.array([1,2,3,4,5], dtype=float)
x.dtype
dtype('float64')
x
array([ 1., 2., 3., 4., 5.])
n.linspace(0,5,11)
array([ 0. , 0.5, 1. , 1.5, 2. , 2.5, 3. , 3.5, 4. , 4.5, 5. ])
n.arange(0,5,.25)
array([ 0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. , 2.25, 2.5 , 2.75, 3. , 3.25, 3.5 , 3.75, 4. , 4.25, 4.5 , 4.75])
n.zeros(3, dtype=float)
array([ 0., 0., 0.])
x = n.array([1,2,3,4,5,6,7,8], dtype=float)
x.shape
(8,)
y = x.reshape(4,2)
y
array([[ 1., 2.], [ 3., 4.], [ 5., 6.], [ 7., 8.]])
y[0,:] # everything in first row
array([ 1., 2.])
y[0:2,1] # first two elements in second column
array([ 2., 4.])
y[::2,1] # every second element in second column
array([ 2., 6.])
y[::2,1] = 0 # set every second element in second column to zero
y
array([[ 1., 0.], [ 3., 4.], [ 5., 0.], [ 7., 8.]])
x = n.array([1,2,3,4,5,6,7,8], dtype=float)
y = n.array([2,0,2,0,2,0,2,0], dtype=float)
x*y
array([ 2., 0., 6., 0., 10., 0., 14., 0.])
x + 2
array([ 3., 4., 5., 6., 7., 8., 9., 10.])
y = n.cos( n.linspace(0, n.pi,11) )
y
array([ 1.00000000e+00, 9.51056516e-01, 8.09016994e-01, 5.87785252e-01, 3.09016994e-01, 6.12323400e-17, -3.09016994e-01, -5.87785252e-01, -8.09016994e-01, -9.51056516e-01, -1.00000000e+00])
x = n.array([1,2,3,4,3,2,1], dtype=float)
x > 2
array([False, False, True, True, True, False, False], dtype=bool)
(x > 2) & (x < 4)
array([False, False, True, False, True, False, False], dtype=bool)
x[(x > 2) & (x < 4)]
array([ 3., 3.])
x[(x > 2)]
array([ 3., 4., 3.])
x = n.array([2,1,3])
n.argsort(x)
array([1, 0, 2])
x[n.argsort(x)]
array([1, 2, 3])
x = n.array([1,2,3,4,3,2,1], dtype=float)
y = n.array([1,2,3,4,5,6,7], dtype=float)
m = x > 2
m &= x < 4
y[m]
array([ 3., 5.])
More examples: