Posts

Showing posts with the label Python Programming

Python Programming #Example of __init__(), __del__(), class variable, object variable and their uses

#Example of __init__(), __del__(), class variable, object variable and their uses class Bbh: n= 10 # this is class variable def __init__ ( self , x): Bbh.n+= 20 # 1st use of class variable it will work for every object self .n+= 10 #2nd use of class variable it will work only once self .x=x # x is the object variable print ( "the value of object variable=" , x) print ( "the value of class variable=" , self .n) def __del__ ( self ): Bbh.n= 0 print ( "Object variable = " , self .x , " Class variable= " , self .n) # the above statement print the each of value of object variable # and value of last modified class variable # the __init__() like constructor in C++ will be executed automatically obj1=Bbh( 5 ) obj2=Bbh( 15 ) obj3=Bbh( 25 ) # del is use to free the memory and to execute the method __del__(). It is same as destructor in C++ # but in C++ it executed au...

Python Programming: Roots of Quardratic Equation

#Finding Roots of a quardratic Equation import math a=int(input("Enter the value of a : ")) b=int(input("Enter the value of b : ")) c=int(input("enter the value of c : ")) dis=b**2-4*a*c # finding discriminant if(dis>0):     print("Roots are Real and unequal")     sq=math.sqrt(dis)     r1=(-b+sq)/(2*a)     r2=(-b-sq)/(2*a) elif(dis<0):     print("Roots are Imaginary ")     r1=complex(-b,math.sqrt(abs(dis)))/(2*a)     r2 = complex(-b, -math.sqrt(abs(dis))) / (2 * a) else:     print("Roots are Real and Equal")     r1 = -b / (2 * a)     r2 = r1 print("Roots are : r1=", r1, ", r2=", r2)

Python Programming: Area of Triangle

#Find the area of triangle import math a= int ( input ( "Enter the length of first side : " )) b= int ( input ( "Enter the length of second side : " )) c= int ( input ( "Enter the lenth of third side : " )) if ((a+b)>c and (c+a)>b and (b+c)>a): s=(a+b+c)/ 2 Area=math.sqrt(s*(s-a)*(s-b)*(s-c)) print ( "Area of the triangle : " , Area) else : print ( "Invalid Triangle" ) Output: Run 1:  Enter the length of first side : 2 Enter the length of second side : 2 Enter the lenth of third side : 6 Invalid Triangle Run 2: Enter the length of first side : 6 Enter the length of second side : 4 Enter the lenth of third side : 7 Area of the triangle : 11.976539567003485

Python Programming: Checking of Armstrong Number

#Program to check whether a number is Armstrong or Not n=int(input("Enter a number")) s=0 n1=n while(n>0):     r=n%10     s=s+(r**3)     n=n/10 if(s==n1):     print(n1," is Armstrong Number") else:     print(n1," is not Armstrong Number")    

Python Programming: Ternary operator and lambda function

Ternary operator and the lamda function  in Python: a=int(input("Enter first no ")) b=int(input("Enter second no ")) #Ternary operator used with Lambda function max=lambda a,b: a if a>b else b print("Biggest no. ",max(a,b)) #Simply use of ternary operator in Python print("Smallest no. ", a if a<b else b) # you can use this type also min=a if a<b else b print("Smallest no. ", min)

Python Programming: Swapping two numbers using Function

def swap(a,b):     a,b=b,a     print("After swapping","a=",a," b=",b) a=10 b=20 print("Before swapping a=",a," and b=",b) swap(a,b)