Numpy Array Object Programs

Previous
Next

Read, Construct and Display Array:

  • Read number of rows
  • Read column size
  • Construct matrix with row and column size
  • Don’t specify data type.
  • Display all elements using nested loop.
import numpy
x = int(input("Enter rows : "))
y = int(input("Enter columns : "))
matrix = numpy.ndarray(shape=(x,y),dtype=int)
print("Elements are :")
for row in matrix:
    for ele in row:
        print(ele)

Reading elements into array:

import numpy as np

n = int(input("Enter size :"))
arr = np.ndarray(shape=(n))

print("Enter %d elements :" %n)
for i in range(n):
    arr[i] = int(input())

print("Array is :",arr)

Find the sum of array elements:

import numpy as np

n = int(input("Enter size :"))
arr = np.ndarray(shape=(n), dtype=int)

print("Enter %d elements :" %n)
for i in range(n):
    arr[i] = int(input())

sum=0
for ele in arr:
    sum=sum+ele
print("Sum is : ",sum)

Checking properties of 2 dimensional array:

import numpy as np
nest_list = [[10,20,30],[40,50,60],[70,80,90]]
matrix = np.array(nest_list)
print("Size :",matrix.size)
print("Datatype :",matrix.dtype)
print("Shape :",matrix.shape)
print("Dimension :",matrix.ndim)
Previous
Next

Add Comment

Courses Enquiry Form