Sunday, 29 November 2020

User defined Exceptions in Python

#USER DEFINED EXCEPTION

Here MyException is an userdefined class and it must be inherited from the base class Exception.

Create a constructor and send the error message to it.

class MyException(Exception):
    def __init__(self,value):
        self.value=value
    def __str__(self):
        return(repr(self.value))
try:
    raise(MyException("This is my user defined Exception"))
except MyException as e:
    print("Printable Message:",e.value)

#USER DEFINED EXCEPTION WITH INHERITANCE 

class MyException(Exception):
     pass
class Dividebyzero(MyException):
      pass
try:
    a = int(input("Enter a value: "))
    b=int(input("Enter b value: "))
    if b==0:
        raise Dividebyzero
    else:
        print("result=",(a/b))
except Dividebyzero:
    print("Dont enter Input value for b as zero, try again!")
#FINALLY KEYWORD

try:
    a=int(input("enter value for a"))
    b=int(input("enter value for b"))
    c=a/b
    print(c)
except ZeroDivisionError:    
    print("Dont enter b value as zero")    
finally:    
    print('This is always executed')  

Tuesday, 24 November 2020

Inheritance in Python

 

 

 #Multiple Inheritance

class Father:
    def reading1():
        Father.height1=int(input("Enter Fathers Height"))
    def printing1():
        print("Fathers height: ",Father.height1)
class Mother:
    def reading2():
        Mother.height2=int(input("Enter mothers Height"))
    def printing2():
        print("Mothers height: ",Mother.height2)
class child(Father,Mother):
    def calculate():
        child.height3=(Father.height1+Mother.height2)/2
    def printing():
        print(child.height3)
Father.reading1()
Father.printing1()
Mother.reading2()
Mother.printing2()
child.calculate()
child.printing()       

OUTPUT:


Monday, 2 March 2020

Python Programs

#Program to print natural numbers
n=int(input("enter number"))
for i in range(1,n+1):
  print(i,end=' ')

#Program to print 1 to n natural numbers and find the sum also
n=int(input("enter n value"))
print("natural numbers between 1 to {0}".format(n))
sum=0
for i in range(1,n+1):
   print(i,end=' ')
   sum=sum+i
print("\nsum of natural numbers are:{0}".format(sum))

#Reading and Printing of array elements
a=[]
n=int(input("enter n value"))
for i in range(n):
  a.append(int(input("enter elements into an array")))
for i in range(n):
   print(a[i]) 

# Calculate Factorial of a given number using functions
def fact(n):
   f=1;
   for i in range(1,n+1):
     f=f*i
   return f
n=int(input("enter n value"))
print("factorial of {0} is {1}".format(n,fact(n)))

Friday, 31 January 2020

Context Free Grammar

A Context Free Grammar(CFG) is defined as G=(V,T,P,S) where
V=Finite set of Variables
T=Finite set of Terminals
P=Production rule which can be defined as 𝝰→𝛃 where 𝝰𝟄V and 𝞫→(V U T)*
S is a starting production

Example:

S→aSb|𝟄 is a grammar which accepts the language contains equal number of a's and equal number of b's

similarly

S→aSa|bSb|𝟄 is grammar which generates even length plaindrome

S→aSa|bSb is grammar which generates odd length palindrome

Saturday, 25 January 2020

/*Program to check whether two strings are Anagram or Not*/

Anagram Definition:

An anagram of a string is another string that contains same characters, only the order of characters can be different

Example: Weak, Wake
                 Mad, Dam

/* Program using Java*/

class StringAnagram
{
public static void main(String[] a) throws Exception
{
boolean flag=true;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 
System.out.println("enter two strings");
str1=br.readLine();
str2=br.readLine();  
 int n1 = str1.length;
 int n2 = str2.length;
        if (n1 != n2)
            return false;
        char[] ch1 = new char[str1.length()];
        for (int i = 0; i < str1.length(); i++) {
            ch1[i] = str1.charAt(i);
        char[] ch2 = new char[str2.length()];
        for (int i = 0; i < str1.length(); i++) {
            ch2[i] = str1.charAt(i);
      
        Arrays.sort(str1);
        Arrays.sort(str2);
    
        for (int i = 0; i < n1; i++)
            if (str1[i] != str2[i])
               flag=false;
        if(flag==true)
                System.out.println("Strings are Anagram");
       else
                System.out.println("Strings are not Anagram");
 }
}