Exercise 1:

Write a NumPy program to create an array of ones of size n.

Example:
         n = 10
         A = [ 1 1 1 1 1 1 1 1 1 1]

Solution 1:

In [3]:
import numpy as np
n = int(input("put the 1st number = "))
x = int(input("put the 2nd number = "))
a = np.array([])

for i in range(n):
    a = np.append(a,x)
print(a)
put the 1st number = 10
put the 2nd number = 1
[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

Solution 2:

In [5]:
import numpy as np
n = int(input("put the 1st number = "))
x = int(input("put the 2nd number = "))
a = np.array([])

a = np.ones(n) * x
print(a)
put the 1st number = 5
put the 2nd number = 2
[2. 2. 2. 2. 2.]

Exercise 2:

Write a NumPy program to create a zero vector of size n and update sixth value to 11.

     Example:
         n = 10
         A = [ 0 0 0 0 0 0 0 0 0 0]

         Update sixth value to 11 :
         A = [ 0 0 0 0 0 0 11 0 0 0]
In [7]:
import numpy as np

n = int(input("put a number = "))
a = np.ones(n)*0        # a = np.zeros(n)
print(a)

for i in range(n):
    a[6]= 11
print(a)
put a number = 10
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0.  0.  0.  0.  0.  0. 11.  0.  0.  0.]

Exercise 3:

Write a NumPy program to create a array with values ranging from m to n.

     Example:
         m = 12
         n = 30
     Output:
         [12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29]
In [10]:
import numpy as np

m = int(input("put the 1st number = "))
n = int(input("put the 2nd number = "))

a = np.array([])

for i in range(n-m):
    a = np.append(a,m+i)         # a = np.arange(m,n)
print(a)
put the 1st number = 2
put the 2nd number = 5
[2. 3. 4.]

Exercise 4:

Write a NumPy program to reverse an array (first element becomes last).

     Example:

          Original array:
                          [12 13 14 15]
          Reverse array:
                          [15 14 13 12]
In [11]:
import numpy as np
m = int(input("put the 1st number = "))
n = int(input("put the 2nd number = "))
a = np.arange(m,n)
a = a[::-1]
print(a)
put the 1st number = 1
put the 2nd number = 5
[4 3 2 1]

Exercise 5:

Write a NumPy program to convert the values of Centigrade degrees into Fahrenheit degrees.
Centigrade values are stored into a NumPy array. Use the following equation:
C = (F - 32) x 5/9 (Convert Fahenheit into Centigrade)

     Example:

         A = [0, 12, 45.21 ,34, 99.91]

         Expected Output:
         Values in Fahrenheit degrees:
                                       [ 0. 12. 45.21 34. 99.91]
         Values in Centigrade degrees:
                                       [-17.78 -11.11 7.3389 1.11 37.7278]
In [12]:
import numpy as np
A = np.array([0, 12, 45.21, 34, 99.91])

# A = np.multiply(A - 32,5/9)
A = (A - 32) * 5/9
print(A)
[-17.77777778 -11.11111111   7.33888889   1.11111111  37.72777778]

Exercise 6:

Write a NumPy program to test whether each element of a 1-D array is also present in a second array.

     Example:

         Array1: [ 0 10 20 40 60]
         Array2: [0, 40]

         Compare each element of array1 and array2
         [ True False False True False]

Solution 1:

In [13]:
import numpy as np
A = np.array([0, 10, 20, 40, 60])
B = np.array([0, 40])
c = np.array([]).astype(bool)
n = len(A)

for i in range(n):
    if A[i] in B:
        t = True
    else:
        t = False
    c = np.append(c,t)
print(c)
[ True False False  True False]

Solution 2:

In [14]:
import numpy as np
A = np.array([0, 10, 20, 40, 60])
B = np.array([0, 40])
x = np.in1d(A, B)
print(x)
[ True False False  True False]

Exercise 7:

Write a NumPy program to get the unique elements of an array.

     Example:

         Original array:
                         [10 10 20 20 30 30]
         Unique elements of the above array:
                         [10 20 30]

Solution 1:

In [19]:
import numpy as np
A = np.array([10, 10, 20, 20, 30, 30])
A = set(A)
B = np.array([])
for a in A:
    B = np.append(B,a)
print(B)
[10. 20. 30.]

Solution 2:

In [22]:
import numpy as np
A = np.array([10, 10, 20, 20, 30, 30])
x = np.unique(A)
print(x)
[10 20 30]

Exercise 8:

Write a NumPy program to find the set difference of two arrays.
The set difference will return the sorted, unique values in array1 that are not in array2.

     Example:

         Array1: [ 0 10 20 40 60 80]
         Array2: [10, 30, 40, 50, 70, 90]

         Set difference between two arrays:
         [ 0 20 60 80]
In [24]:
import numpy as np
A = np.array([ 0, 10, 20, 40, 60, 80])
B = np.array([10, 30, 40, 50, 70, 90])
x = np.setdiff1d(A, B)
print(x)
[ 0 20 60 80]

Exercise 9: Write a NumPy program to find the set exclusive-or (xor) of two arrays.
Set exclusive-or will return the sorted, unique values that are in only one (not both) of the input arrays.

    Example:

     Array1: [ 0 10 20 40 60 80]
     Array2: [10, 30, 40, 50, 70]
     Unique values that are in only one (not both) of the input arrays:
     [ 0 20 30 50 60 70 80]

Solution 1:

In [27]:
import numpy as np

A = np.array([ 0, 10, 20, 40, 60, 80])
B = [10, 30, 40, 50, 70]

A = set(A)
B = set(B)
C = A^B
C = list(C)
C = sorted(C)
C = np.array(C)
print(C)
[ 0 20 30 50 60 70 80]

Solution 2:

In [28]:
import numpy as np

A = np.array([ 0, 10, 20, 40, 60, 80])
B = [10, 30, 40, 50, 70]
x = np.setxor1d(A, B)
print(x)
[ 0 20 30 50 60 70 80]

Exercise 10:

Write a NumPy program to find the union of two arrays.
Union will return the unique, sorted array of values that are in either of the two input arrays.

    Example:

     Array1: [ 0 10 20 40 60 80]
     Array2: [10, 30, 40, 50, 70]
     Unique sorted array of values that are in either of the two input arrays:
     [ 0 10 20 30 40 50 60 70 80]

Solution 1:

In [29]:
import numpy as np
a = np.array([ 0, 10, 20, 40, 60, 80])
b = [10, 30, 40, 50, 70]
c = np.concatenate((a,b),axis = 0)
c = list(set(c))
c = sorted(c)
c = np.array(c)
print(c)
[ 0 10 20 30 40 50 60 70 80]

Solution 2:

In [30]:
import numpy as np
a = np.array([ 0, 10, 20, 40, 60, 80])
b = [10, 30, 40, 50, 70]
x = np.union1d(a, b)
print(x)
[ 0 10 20 30 40 50 60 70 80]

Solution 3:

In [31]:
import numpy as np
a = np.array([ 0, 10, 20, 40, 60, 80])
b = [10, 30, 40, 50, 70]
a = set(a)
b = set(b)
c = a|b
c = list(c)
c = sorted(c)
c = np.array(c)
print(c)
[ 0 10 20 30 40 50 60 70 80]

Exercise 11:

Write a NumPy program to construct an array by repeating n times.

    Example:

     array: [1, 2, 3, 4]
     n = 3
     Repeating 3 times
     [1 2 3 4 1 2 3 4 1 2 3 4]

Solution 1:

In [32]:
import numpy as np
a = [1, 2, 3, 4]
m = len(a)
n = 3
b = []

for i in range(n):
    for j in range(m):
        b.append(a[j])

b = np.array(b)
print(b)
[1 2 3 4 1 2 3 4 1 2 3 4]

Solution 2:

In [33]:
import numpy as np
a = [1, 2, 3, 4]
a = np.tile(a, 3)
print(a)
[1 2 3 4 1 2 3 4 1 2 3 4]

Exercise 12:

Write a NumPy program to get the values and indices of the elements that are bigger than n in a given array.

    Example:
    n = 10
    Original array: [0 10 20 20 30 40]
    Values bigger than 10 :  [20 20 30 40]

Solution 1:

In [34]:
import numpy as np
a = np.array([0, 10, 20, 20, 30, 40])
n = int(input("put a number = "))
m = len(a)
b = np.array([])

for i in range(m):
    if a[i]>n:
        b = np.append(b,a[i])
print(b)
put a number = 20
[30. 40.]

Solution 2:

In [36]:
import numpy as np
a = np.array([0, 10, 20, 20, 30, 40])
n = int(input("put a number = "))
x = a[a>n]
print(x)
put a number = 20
[30 40]