Saturday, 13 April 2019

#Example 1: Basic Program for basic data types(int,float,string,complex)
a=10
b=3.14
c="jahab"
d=3+2j
e=None
print(a)
print("a =",a)
print("b =",b)
print("Floating value is : %0.1f"%b)
print("c =",c)
print("d :",d)
print("a : ",a," b :",b)
print("a={},b={}".format(a,b))
---------------------------------------------------------
#Example 2:
a1 = 5
print(a1, "is of type", type(a1))
print(a1, "is Integer number?", isinstance(a1,int))
print(a1, "is Float number?", isinstance(a1,float))

a2 = 2.0
print(a2, "is of type", type(a2))
print(a2, "is floating number?", isinstance(a2,float))

a3 = 1+2j
print(a3, "is of type", type(a3))
print(a3, "is complex number?", isinstance(a3,complex))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Note:
------
* type() - type() function to know which class a variable or a value
* isinstance()- isinstance() function to check if an object belongs to a particular class.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Program 1: Add Two Numbers
a=10
b=20
c=a+b
print ("Addition result is : ",c)
print ('Addition result is : ',c)
print ('Addition result is : '+str(c))

#Program 2: Add Two Numbers using input function
a=input("Enter First value :")
b=input("Enter Second value :")
c=int(a)+int(b)
print ("Addition result is : ",c)

#Program 3: Add Two Numbers using input function
a=int(input("Enter First value :"))
b=int(input("Enter Second value :"))
c=a+b
print ("Addition result is : ",c)

#Program 4: Arithmetic Calculations
a=int(input("Enter First Value :"))
b=int(input("Enter Second Value :"))
add=a+b
sub=a-b
mul=a*b
div=a/b
rem=a%b
print ("Addition result is : ",add)
print ("subtraction result is : ",sub)
print ("Multiplication result is : ",mul)
print ("Divide result is : ",div)
print ("Divide result is : %0.2f"%div)
print ("Remainder result is : ",rem)

#Program 5: Simple Interest Calculation
p=float(input("Enter P:"))
n=float(input("Enter N:"))
r=float(input("Enter R:"))
si=p*n*r/100
print ("Simple ins :",si)


#Program 6:  Volume of Cone
r=float(input("Enter r :"))
h=float(input("Enter h :"))
v=(1.0/3.0)*3.14*r*r*h
print ("Cone :%0.2f" %v)

#Program 7:  Volume of Sphere
r=float(input("Enter r :"))
v=(4.0/3.0)*3.14*r*r*r
print ("Sphere :",v)

#Program 8:  Using math function
import math
n=int(input("Enter input :"))
dis=math.sqrt(n)
dis1=math.pow(n,2)
print (dis)
print ('Power ',dis1)
print ('Pi ',math.pi)

#Program 9:  Using math function(cone)
import math
r=float(input("Enter r :"))    #convert string to floating value
h=float(input("Enter h :"))
v=(1.0/3.0)*math.pi*math.pow(r,2)*h
print ("Cone :",v)

#Program 10: swapping two numbers using third variable
a=10
b=20
print("Before swapping")
print("a :",a,"\t b :",b)
t=a
a=b
b=t
print("After swapping")
print("a :",a,"\t b :",b)

#Program 11: swapping two numbers without third variable
a=10
b=20
print("Before swapping")
print("a :",a,"\t b :",b)
a=a+b
b=a-b
a=a-b
print("After swapping")
print("a :",a,"\t b :",b)
===============================================

No comments:

Post a Comment