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)

Greatest common divisor program using euclids algorithms

#Credits to NPTEL MOOC, Programming, Data Structures & Algorithms in 
#Python by Madhavan Mukund, Chennai Mathematical Institute 
 
def gcd(m,n):
    if m < n:
        (m,n) = (n,m)
    if (m%n) == 0:
        return(n)
    else:
        diff = m-n
        return(gcd(max(n,diff),min(n,diff)))

print(gcd(12,3))

Greatest common divisor program with looping

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


def gcd(m,n):
  i = min(m,n)
  while i > 0:
    if (m%i) == 0 and (n%i) == 0:
      return(i)
    else:
      i = i-1

Greatest common divisor program without list Implementation

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

def gcd(m,n):
  for i in range(1,min(m,n)+1):
    if (m%i) == 0 and (n%i) == 0:
      mrcf = i
  return(mrcf)

Greatest common divisor program - A shorter Implementation

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

def gcd(m,n):
  cf = []
  for i in range(1,min(m,n)+1):
    if (m%i) == 0 and (n%i) == 0:
      cf.append(i)
  return(cf[-1])

Greatest common divisor program

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

def gcd(m,n):
  fm = []
  for i in range(1,m+1):
    if (m%i) == 0:
      fm.append(i)
  fn = []
  for j in range(1,n+1):
    if (n%j) == 0:
      fn.append(j)
  cf = []
  for f in fm:
    if f in fn:
      cf.append(f)
  return(cf[-1])

Calculation of Gross and Net Calorific Value

#  Credits   Name: ANUPRIYA R

#  HCV - Higher and Gross Calorific Value
#  LCV -  Lower or Net Calorific Value
# Compatible with python 3.x.x

c=float(input("enter value of carbon in %: "))
h=float(input("enter value of hydrogen in %: "))
s=float(input("enter value of sulphur in %: "))
o=float(input("enter value of oxygen in %: "))
HCV=(8080*c+34500*(h-o/8)+2240*s)*1/100
LCV=(HCV-0.09*h*587)
print ("Higher calorific value of sample is ",HCV)
print("Lower calorific value of sample is ",LCV)
     

Chemistry Problem Solver

# Credits  Name: Abhijith P   Email: abhijithp64@gmail.com
# Compatible with python 3.x.x 

# Program for problems related to chemistry
# 1. Spectroscopy Problems
# 2. Hardness of solution or water

# 3. Calorific value
# 4. Viscosity index 

#   Abhijith P
#   Roll number 2
#
import os
import sys
import math

def clear():#Function for clearing screen
    os.system('cls')

def error(txt):#Function to quit the program by showing an error
    print(txt + "\n\t  Program will terminate now.")
    input("\t  Press ENTER to continue...")
    exit()

def getint(txt):#Function for geeting integer as input. Shows error if input is not int type
    str = input(txt)
    if str.isdigit():
        return int(str)
    else:
        error("\n\tError : Invalid input \"" + str + "\".")
       
def getfloat(txt): #Function for getting float number as input. Shows error if input is not float type
    str = input(txt)
    partition = str.partition('.')
    if str.isdigit():
        return float(str)
    elif (partition[0].isdigit() and partition[1]=='.' and partition[2].isdigit()) or (partition[0]=='' and partition[1]=='.' and partition[2].isdigit()):
        return float(str)
    else:
        error("\n\tError : Invalid input \"" + str + "\".")

#Main function starts from here!
choice = None
while choice !=5:
    choice1 = None
    choice2 = None
    choice3 = None
    clear()
    print("\tProgram for problems related to chemistry")
    print("\t-----------------------------------------")
    print("\n\t1. Spectroscopy Problems\n\t2. Hardness of solution or water\n\t3. Calorific value\n\t4. Viscosity index\n\t5. EXIT")
    choice = input("\n\tEnter your choice : ")
    if choice == "1":
        choice1 = None
        while choice1 != 5:
            clear()
            print("\tSpectroscopy Problems")
            print("\t---------------------")
            print("\n\tAbsorbance")
            print("\n\t1. Calculate Absorbance using intensities of\n\t   incident and transmitted light")
            print("\t2. Calculate Absorbance using concentration,\n\t   cell lenght and molar absorbance coefficient")
            print("\t3. Calculate Absorbance using transmittance")
            print("\n\tTransmittance")
            print("\n\t4. Calculate Transmittance using intensities of\n\t   incident and transmitted light\n\t5. Go to main menu")
            choice1 = input("\n\tEnter your choice : ")
            if choice1 == "1":
                t1 = getfloat("\n\t  Enter intensity of incident light : ")
                t2 = getfloat("\t  Enter intensity of transmitted light : ")
                if t1 <= 0.0 or t2 <= 0.0:
                    error("\n\t  Intensity of incident light or transmitted light cannot be\n\t  zero or negative.")
                print("\t  Absorbance is "+ str(math.log10(t1/t2)) +" .")
                input("\t  Press ENTER to conitnue...")
            elif choice1 == "2":
                conc = getfloat("\n\t  Enter concentration of solution : ")
                x = getfloat("\t  Enter cell length : ")
                coeff = getfloat("\t  Enter molar absorbance coefficient : ")
                if conc < 0.0 or x < 0.0 or coeff < 0.0:
                    error("\n\t  Concentration, cell lenght or molar absorbance coefficient\n\t cannot be negative!")
                print("\t  Absorbance is " + str(coeff*x*conc) + " .")
                input("\t  press ENTER to continue...")
            elif choice1 == "3":
                trans = getfloat("\n\t  Enter transmittance : ")
                if trans < 0:
                    error("\t  Transmitance cannot be negative!")
                print("\t  Absorbance is " + str(-1 * math.log10(trans)) + " .")
                input("\t  Press ENTER to continue...")
            elif choice1 == "4":
                t1 = getfloat("\n\t  Enter intensity of incident light : ")
                t2 = getfloat("\t  Enter intensity of transmitted light : ")
                if t1 <= 0.0 or t2 <= 0.0:
                    error("\n\t  Intensity of incident light or transmitted light cannot be\n\t  zero or negative.")
                print("\t  Transmittance is " + str(t2/t1) + " .")
                input("\t  Press ENTER to continue...")
            elif choice1 == "5":
                choice1 = 5
            else:
                print("\n\tWrong choice")
                input("\tPress ENTER to continue...")
    elif choice == "2":
        choice2 = None
        while choice2 != 4:
            clear()
            print("\tHardness ofsolution or water")
            print("\t----------------------------")
            print("\n\t1. Calculate using normality ")
            print("\t2. Calculate using molarity")
            print("\t3. Calculate using mass")
            print("\t4. Go to main menu")
            choice = str(input("\tEnter your choice : "))
            if choice == "1":
                norm = getfloat("\n\t  Enter normality : ")
                if norm < 0:
                    error("\n\t  Normality cannot be negative!")
                print("\tHardness of " + str(norm)+"N solution is "+ str(norm*50*100)+" mg/l!")
                input("\tPress ENTER to continue...")
            elif choice == "2":
                mol = getfloat("\n\t  Enter molarity : ")
                if mol < 0:
                    error("\n\t  Molarity cannot be negative!")
                print("\tHardness of " + str(mol)+"M solution is "+ str(mol*100*100)+" mg/l!")
                input("\tPress ENTER to continue...")
            elif choice == "3":
                massp = []
                print("\n\t  Note:- Enter zero if their is no ion")
                ph = getint("\t  Enter total number of chloride and sulphide of Ca and Mg : ")
                if ph < 0:
                    error("\n\t  Number of ions cannot be negative!")
                for i in range(ph):
                      massp.append([])
                      m = getfloat("\t  Enter mass of ion " + str(i+1) + " : ")
                      if m <= 0:
                          error("\n\t  Mass cannot be zero or negative!")
                      w = getfloat("\t  Enter molecular weight of ion " + str(i+1) + " : ")
                      if w<=0:
                          error("\n\t  Molecular weight cannot be zero or negative!")
                      massp[i].append(m)
                      massp[i].append(w)
                masst = []
                print("\n\t  Note:- Enter zero if their is no ion")
                th = getint("\t  Enter total number of carbonates and bicarbonates of Ca and Ma : ")
                if th < 0:
                    error("\n\t  Number of carbonates or bicarbonates cannot be negative!")
                for i in range(th):
                      masst.append([])
                      m = getfloat("\tEnter mass of carbonate/bicarbonate " + str(i+1) + " : ")
                      if m <= 0:
                          error("\n\t  Mass cannot be zero or negative!")
                      w = getfloat("\tEnter molecular weight of carbonate/bicarbonate " + str(i+1) + " : ")
                      if w<=0:
                          error("\n\t  Molecular weight cannot be zero or negative!")
                      masst[i].append(m)
                      masst[i].append(w)
                perh = 0.0
                for i in range(len(massp)):
                      perh = perh + ((massp[i][0]*100)/massp[i][1])
                temh = 0.0
                for i in range(len(masst)):
                      temh = temh + ((masst[i][0]*100)/masst[i][1])
                print("\n\tPermamnent hardness is " + str(perh) + " ppm.")
                print("\tTemporary hardness is " + str(temh) + " ppm.")
                print("\tTotal hardness is " + str(perh + temh) + " ppm!")
                input("\tPress ENTER to continue...")
            elif choice == "4":
                choice2 = 4
            else:
                print("\n\tWrong choice!")
                input("\tPress ENTER to continue...")
    elif choice == "3":
        choice3 = None
        HCV = None
        LCV = None
        while choice3 != 3:
            clear()
            print("\tCalorific Value")
            print("\t---------------")
            print("\n\t1. Calculate HCV ")
            print("\t2. Calculate LCV")
            print("\t3. Go to main menu")
            choice3 = input("\n\tEnter your choice : ")
            if choice3 == "1":
                W = getfloat("\n\t  Enter mass of water taken in calorimeter : ")
                if W<=0:
                    error("\n\t  Mass of water cannot be zero or negative!")
                w = getfloat("\t  Enter water equvalent of calorimeter : ")
                if w<=0:
                    error("\n\t  Water equvalent of calorimeter cannot be zero or negative!")
                x = getfloat("\t  Enter mass of fuel taken : ")
                if x<=0:
                    error("\n\t  Mass of fuel cannot be zero or negative!")
                t1 = getfloat("\t  Enter initial temperature of water : ")
                t2 = getfloat("\t  Enter final temperature of water : ")
                cc = getfloat("\t  Enter cooling correction (zero if their is no correction) : ")
                ac = getfloat("\t  Enter acid correction (zero if their is no correction) : ")
                fc = getfloat("\t  Enter fuse correction (zero if their is no correction) : ")
                HCV = ((W+w)*(t2-t1+cc)-(ac+fc))/x
                print("\t  HCV is " + str(HCV))
                input("\t  Press ENTER to continue...")
            elif choice3 == "2":
                if HCV != None:
                    print("\n\t  Last calculated value of HCV is " + str(HCV),"\n")
                    tHCV = float(input("\t  Enter HCV (input -1 to use last calculated value) : "))
                    if tHCV == -1.0:
                        tHCV = HCV
                else:
                    tHCV = float(input("\n\t  Enter HCV : "))
                ph = float(input("\t  Enter percentage of hydrogen : "))
                lhs = float(input("\t  Enter latent heat of steam (-1 to use default value) : "))
                if lhs == -1.0:
                    lhs = 587
                LCV = tHCV-0.09*ph*lhs
                print("\t  LCV is " + str(LCV))
                input("\t  Press ENTER to continue...")
            elif choice3 == "3":
                choice3 = 3
            else:
                print("\n\tWrong choice!")
                input("\tPress ENTER to continue...")
    elif choice == "4":
        choice4 = None
        while choice4 != 2:
            clear()
            print("\tViscosity Index")
            print("\t---------------")
            print("\t1. Calculate viscosity index")
            print("\t2. Go to main menu")
            choice4 = input("\n\tEnter your choice : ")
            if choice4 == "1":
                u = getfloat("\n\t  Enter viscosity of sample oil : ")
                h = getfloat("\t  Enter viscosity of High Viscosity Standard oil : ")
                l = getfloat("\t  Enter viscosity of Low Viscosity Standard oil : ")
                if l == h:
                    error("\t  High viscosity standard oil and low viscosity standard oil cannot have same viscosity!")
                if u < 0 or h < 0 or l < 0:
                    error("\t  Viscosity cannot be zero!")
                print("\t  Viscosity index of sample is " + str(((l-u)/(l-h))*100) + " .")
                input("\t  Press ENTER to continue...")
            elif choice4 == "2":
                choice4 = 2
            else:
                print("\n\tWrong choice!")
                input("\tPress ENTER to continue...")
    elif choice == "5":
        choice = 5
    else:
        print("\n\tWrong choice!")
        input("\tPress ENTER to continue...")


Character Classification based on ASCII value

SingleCharacter = input("Enter a character:",)
Character = ord(SingleCharacter)
# ord --> Convert a string to ASCII value
if Character > 0 and Character <= 31:
    print("ASCII value", Character," is a control character")
elif Character >= 48 and Character <= 57:
    print("ASCII value", Character,"  is a character is a digit")
elif Character >= 65 and Character <= 90:
    print("ASCII value", Character,"  is a uppercase letter")
elif Character >= 97 and Character <= 122:
    print("ASCII value", Character,"  is a lowercase letter")
else:
    print("ASCII value", Character," is a Symbol")

To find the distance between two points, given by the coordinates (x1, y1) and (x2, y2) by Pythagorean theorem using functions

def pt(x1,y1,x2,y2):
      x=x2-x1
      y=y2-y1
      x=x**2
      y=y**2
      d=(x+y)**(0.5)
      return d
     
x1=int(raw_input('Enter the coordinate x1: '))
x2=int(raw_input('Enter the coordinate x2: '))
y1=int(raw_input('Enter the coordinate y1: '))
y2=int(raw_input('Enter the coordinate y2: '))
k=pt(x1,y1,x2,y2)
print 'Distance between two points is : ', k
     

Database version retrieving using Python

#username of MySQL: root
#Password: root
#Hostname: localhost
# Database Name in MySQL: test

import mysql.connector
x = mysql.connector.connect(user='root', password='root',host='localhost',database='test')
# prepare a cursor object using cursor() method
cursor = x.cursor()
# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")
# Fetch a single row using fetchone() method.
data = cursor.fetchone()
print ("Database version : %s " % data)
# disconnect from server
x.close()

Database connection test to MySQL

#username of MySQL: root

#Password: root

#Hostname: localhost

# Database Name in MySQL: test

import mysql.connector

x = mysql.connector.connect(user='root', password='root',host='localhost',database='test')

x.close()

Reversal of a string

startmsg = "hello"
endmsg = ""
for i in range(0,len(startmsg)):
  endmsg = startmsg[i] + endmsg
print (endmsg)

String replacing with another string with occurrence count

s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('red','brown')
print (s1)

s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',1)
print (s1)

s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',2)
print (s1)

s='brown box red pencil brown pen red pen red box'
print (s)
s1=s.replace('brown','red',3)
print (s1)

Searching the pattern in a String using find and index

try:
    s="brown box red pencil brown pen red pen red box"
    print s.find("red")
    print s.find("red",13,len(s))
    print s.find("red",34,len(s))
    print s.find("crow")
    print s.index("red")
    print s.index("red",13,len(s))
    print s.index("red",34,len(s))
    print s.index("crow")
except ValueError:
   print "crow not a pattern in string"
   

String operations to remove white spaces

s=" t  ioi ii "
t1=s.rstrip() # removes trailing white space
t2=s.lstrip() # removes leading white space
t3=s.strip()  # removes leading and trailing white space

print s

print t1

print t2

print t3

File operation for copying content of one file to another without for loop

#  Implementation without for loop
#  Copying content of 1.txt file to 2.txt after creation of file
#  Please provide the 1.txt file in the current folder as input and
#  check after runing the program in the same folder for 2.txt

I=open('1.txt',"r")
O=open('2.txt',"w")
C=I.readlines()
O.writelines(C)
I.close()
O.close()

File operation for copying content of one file to another

#  Copying content of 1.txt file to 2.txt after creation of file
#  Please provide the 1.txt file in the current folder as input and
#  check after runing the program in the same folder for 2.txt

I=open('1.txt',"r")
O=open('2.txt',"w")
for l in I.readlines():
    O.write(l)
I.close()
O.close()

Exceptional handling for Zero Division Error

# if value of x is given as zero the zero divison error generates and program terminates
x=int(raw_input('Enter the number of students failed:'))
y=100/x
print y


# If x is not defined itself the program not terminated instead it goes to except loop
try:
    x=int(raw_input('Enter the number of students failed:'))
    y=100/x
    print y
except ZeroDivisionError:
    x=1
    y=100/x
    print y

Exceptional handling for Name Error

# If x is not defined itself the program not terminated
try:
    y=50*x
except NameError:
    x=1
    y=50*x
    print y
   

Exceptional handling for Key Error

#Without Exceptional handling program terminates if key b not found
s={'d':2,'k':4}
if b in s.keys():
    s['b'].append[9]
else:
    s['b']= 8


#Exceptional handling for key error and program run without termination
s={'d':2,'k':4}
try:
    s['b'].append[9]
except KeyError:
    s['b']= 8

Reverse of a number using functions

def intreverse(Number):
  Reverse = 0          # Initialize the reverse value to overcome garbage value storage
  while(Number > 0):
    Reminder = Number %10
    Reverse = (Reverse *10) + Reminder
    Number = Number//10
  return (Reverse)

What is the type of each of the following expressions (within the type function)?

print type(5)         <type 'int'>

print type("abc")  <type 'str'>

print type(True)   <type 'bool'>

print type(5.5)     <type 'float'>

print type(12/27)   <type 'int'>

print type(2.0/1)   <type 'float'>

print type(12 ** 3)  <type 'int'>

print type(5 == "5")  <type 'bool'>

a = str((-4 + abs(-5) / 2 ** 3) + 321 -((64 / 16) % 4) ** 2)

print a

Ans: 317


New Dictionary Creation and updation

student ={'john':50,'Tom':60,'Nina':82}
print 'First Time:',student
newstudent=student
print 'First Time:',newstudent
newstudent['Tom']=45
print 'Second Time:',newstudent
print 'Second Time:',student

online compiler used  https://repl.it/languages/python










Transpose of a matrix (nested list) in python

row1 = [13,25,73]
row2 = [54,95,36]
row3 = [27,98,19]

matrix = [row1, row2, row3]
trmatrix = [[row[0] for row in matrix],[row[1] for row in matrix],  [row[2] for row in matrix]]
print'Transpose of a matrix',trmatrix

source: http://stackoverflow.com