Multiplication of two Matrices

X = [[4,1,7],[2,1,8],[3 ,7,1]];
Y = [[6,8,1],[9,7,5],[2,3,1]];
result = [[0,0,0],[0,0,0],[0,0,0]];
        
for i in range(len(X)):
     for j in range(len(Y[0])):
         for k in range(len(Y)):
             result[i][j] += X[i][k] * Y[k][j]

for r in result:
 print(r)

Mathematical operators on Numpy Array and List

import numpy as np

a = np.array([1, 2, 3])
print(type(a))           
print('Numpy Array:\n',a)
print('Addition of Numpy Arrays with constant:\n',a+13)
print('Addition of Numpy Arrays:\n',a+a)
print('Multiplication of Numpy Arrays with constant:\n',a*3)
print('Multiplication of Numpy Arrays with another:\n',a*a)
print('Divison of Numpy Arrays with constant:\n', a/3)
print('Divison of Numpy Arrays with another:\n', a/a)
print('Power of Numpy Arrays with constant:\n', a**4)
print('Power of Numpy Arrays with another:\n', a**a)
print('Remainder of Numpy Arrays with constant:\n',a%2)
print('Remainder of Numpy Arrays with another:\n',a%a)
print('Subtraction of Numpy Arrays with constant:\n', a-1)
print('Subtraction of Numpy Arrays with another:\n', a-a)

try:
  a1=[1, 2, 3]
  print('\n',type(a1))
  print('Common List:\n', a1)
  print('Addition of Lists:\n',a1+a1)
  print('Multiplication of List with constant:\n',a1*3)
  print(a1+13) #error
  print(a1*a1) #error
  print(a1/3) #error
  print(a1/a1) #error
  print(a1**4) #error
  print(a1*a1) #error
  print(a1%2) #error
  print(a1%a1) #error
  print(a1-1) #error
  print(a1-a1) #error
except TypeError:
  print('TypeError')
 

To check the number is composite number or not

n=int(input('Enter the number '))
factor=0
for i in range(1,n):
  if n%i==0:
    factor=i
if factor>1:
  print ('The number is a composite number!')
elif n==1:
  print ('The number 1 is neither prime nor composite!')
else:
  print ('This is not a composite number!')

Statistical and Extrema operations on Numpy Array

import numpy as np
x = np.array([11, 13, 121, 181, 99, 100])
print('Numpy Array Elements',x)
print ('Minimum Value in array',x.min())
print ('Maximum Value in array',x.max())
print ('Index of Minimum Value',x.argmin())
print ('Index of Maximum Value',x.argmax())
print ('Mean of Array Values',x.mean())
print ('Median of Array Values',np.median(x))
print ('Standard deviation of Array Values',x.std())

Elementwise sum of two array elemets

import numpy as np
x = [[11,22],[33,44]]
y = [[55,66],[77,88]]
x1 = np.array([[11,22],[33,44]], dtype=np.int32)
y1 = np.array([[55,66],[77,88]], dtype=np.int32)
print("ADD USING LIST\n",x + y)
print("ADD NUMPY ARRAY\n",np.add(x1, y1))

Datatypes in Numpy

import numpy as np

x = np.array([101, 202])  
print(x.dtype)        

x = np.array([11.75, 21.75]) 
print(x.dtype)            

x = np.array([1, 2], dtype=np.int64) 
print(x.dtype)                    

x = np.array([1, 2], dtype=np.complex128) 
print(x.dtype) 

2D-array representation using with & without numpy implementation

import numpy as np

a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
print ("Using Numpy\n",a)

b=[[1,2,3,4], [5,6,7,8], [9,10,11,12]]
print ("Without Numpy\n", b)

Matrix representation and version detailsof Numpy

import numpy as np
print("\nVersion of Numpy is ",np.__version__)
x =  np.arange(25, 50).reshape(5,5)
print("\n Matrix representation in Numpy\n",x)


Different Set Operations

A = {1,2,3,4,5,6,7,8};
B = {5,10,15,20,25,30,35,40};
   
print("Union of A and B is",A | B)

print("\nIntersection of A and B is",A & B)

print("\nDifference of A and B is",A - B)

print("\nSymmetric difference of A and B is",A ^ B)

Use pass for an empty block

#Credits to NPTEL MOOC, Programming, Data Structures & Algorithms in 
#Python by Madhavan Mukund, Chennai Mathematical Institute 

while(True):  
  try:
    userdata = input("Enter a number: ")
    usernum = int(userdata)
  except ValueError:
    print("Not a number. Try again")
  except NameError:
    pass
  else:
    break