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  

Check wheather a number is composite or not

#Credits to NPTEL MOOC, Programming, Data Structures & Algorithms in 
#Python by Madhavan Mukund, Chennai Mathematical Institute 
 
def composite(n):
      for i in range(2,n):
        if n%i == 0:
          return(True)
      return(False)

Divison with multiple conditions

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


def divides(m,n):
  if n%m == 0:
    return(True)
  else:
    return(False)


def even(n):
  return(divides(2,n))


def odd(n):
  return(not divides(2,n))

GCD using euclids algorithms & looping

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

def gcd(m,n):
  if m < n:  # Assume m >= n
    (m,n) = (n,m)
  while (m%n) != 0:
    diff = m-n
    # diff > n? Possible!
    (m,n) = (max(n,diff),min(n,diff))
  return(n)