EX.
NO: 1 BASIC PYTHON PROGRAMS
DATE:
Aim: To perform basic calculations using python programming
Algorithm:
Step 1: Start.
Step 2: Get The values from user.
Step 3: Compute or calculate using respective formulas
to find The answer.
Step 4: Print the output value.
Step 5: Stop.
1.a.CONVERT CELSIUS TO FAHRENHEIT
Program :
Celsius=int(input(“enter The Celsius value :”))
Fahrenheit=(1.8*Celsius)+32
Print(“Fahrenheit=”,Fahrenheit)
Sample input and output:
Input:
Enter The Celsius value=31
Output:
Fahrenheit=87.
1.b.TO CALCULATE THE SIMPLE INTREST
Program:
Principle_amt=float(input(“principle amount=”))
Rate_of_intrest=float(input(“rate of intrest=”))
Time=float(input(“time=”))
SI=(Principle_amt*Rate_of_intrest*Time)/100
Print(“simple intrest=”,SI)
Sample input and output:
Input:
Principle amount=10.0
Rate of intrest=5.0
Time=5.0
Output:
SI=2.5
1.c.FINDING ROOTS OF A QUADRATIC EQUATION
Program:
a = float(input('a='))
b = float(input('b='))
c = float(input('c='))
discriminant = (b**2 - 4*a*c)**0.5
root1 = (-b + discriminant) / (2*a)
root2 = (-b - discriminant) / (2*a)
print("roots =", root1, root2)
Sample input and output:
Input:
a=2
b=7
c=3
Output:
roots = -0.5 -3.0
1.d.FIND THE DISTANCE BETWEEN TWO POINTS
Program :
x2 = float(input("x2="))
x1 = float(input("x1="))
y2 = float(input("y2="))
y1 = float(input("y1="))
dist = ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5
print(dist)
sample input and output:
Input:
x2=2
x1=1
y2=2
y1=1
output:
1.4142135623730951
1.e AREA OF A CIRCLE INSCRIBED IN A SQUARE
Program:side=int(input(“side of a square=”))
radius=side/2
area_of_circle=3.14*radius*radius
print(area_of_circle)
Sample input and output:
Input:side of a square=20
Output:
Area_of_circle=314
Result :
The above programs of basic python programming have been
run and executed successfully
EX.NO:2 DEMONSTRATING DIFFERENT DATATYPES
DATE:
Aim: To write programs to demonstrate different number data-types
in python
Algorithm:
Step 1: Start.
Step 2: Assign input values to different variables.
Step 3: Compute the datatype using inbuilt function
”type(variable_name)”.
Step 4: Print the output.
Step 5: Stop.
Program:
X=20
print(X)
print(type(X))
X=20.5
print(X)
print(type(X))
X='python'
print(X)
print(type(X))
X=['py','th','on']
print(X)
print(type(X))
X=('py','th','on')
print(X)
print(type(X))
X=True
print(X)
print(type(X))
X=b'hello'
print(X)
print(type(X))
Output:
20
<class 'int'>
20.5
<class 'float'>
python
<class 'str'>
['py', 'th', 'on']
<class 'list'>
('py', 'th', 'on')
<class 'tuple'>
True
<class 'bool'>
b'hello'
<class 'bytes'>
Result :
The above program to print different Data-types using
python programming was successfully executed.
EX.NO:3 CONDITIONAL STATEMENTS
DATE:
Aim: To demonstrate various Conditional Statements using
python programming.
Algorithm:
Step 1: Start the program.
Step 2: Get the input from the user.
Step 3: Use different types of statements to solve the problem.
Step 4: Display the result.
Step 5: Stop the program.
3.a.SELECTIVE STATEMENTS(DECISION MAKING STATEMENTS)
SIMPLE IF STATEMENT
Algorithm: Step 1: The condition specified in ‘if’ is checked first.
Step 2: if The condition is True, The subsequent statements are executed.
Step 3: if The condition is False, The instructions outside will be executed.
Program:
num=3
if num>0:
print(num,”is a positive intiger”)
num=-1
if num>0:
print(num,”is a positive intiger”)
print(“The statements have been executed”)
Output:
3 is a positive intiger
The statements have been executed
IF-ELSE STATEMENT
Algorithm:
Step 1:The condition/expression specified in ‘if’ is checked first.
Step 2:If The condition is True, The subsequent statements inside are executed.
Step 3:If The condition is False ,it executes the subsequent statements in the else part
of The program.
Program:
num=3
if num>=0:
print(num,“is positive or zero”)
else:
print(num,”is negative”)
Output:3 is positive or zero
IF-ELIF-ELSE
Algorithm:
Step 1: The condition/expression specified in ‘If’ is checked first.
Step 2: If The condition is True, The subsequent statements inside are executed
Step 3: If The condition is False ,it executes the subsequent statements in the elif part
of the program(if the ’elif ’ condition is satisfied).
Step 4: If The condition is False,it executes the subsequent statements in the else part
of The program.
Program:
num=input(“enter a number:”)
ifnum>0:
print(num,”is positive”)
elifnum==0:
print(num,”is zero”)
else:
print(num,”is negative”)
Sample input and output:
Input:num=-4
Output: -4 is negative
NESTED IF
Program:
var=100
if var<200:
print(“expression value is less than 200”)
if var==150:
print(“The variable is 150”)
elif var==100:
print(“variable is 100”)
elif var==50:
print(“variable is 50”)
else:
print(“could not find The variable”)
print(“GOOD BYE!”)
Output:Expression value is less than 200
Value is 100
GOOD BYE!
3.b.ITERATIVE STATEMENTS
WHILE LOOP
Algorithm:
Step 1: The while condition is checked first.
Step 2: IF the condition is True, then The statements inside the loop .
will be executed, Then the variable is incremented\decremented at the end of looping.
Step 3:if The condition is False, the loop body will be skipped and the statements after
the while loop will be executed.
Program:n=10
i=1
while i<=n:
sum=sum+i
i=i+1
print(“sum =”,sum)
Output:
Sum=55
FOR-LOOP
Algorithm:
Step 1: The FOR condition is checked first.
Step 2: IF the condition is True, Then the statements inside the loop
will be executed..
Step 3:if The condition is False, The loop body will be skipped and the statements
after the for loop will be executed.
Program:
numbers=[1,2,3]
sum=0
for value in numbers:
sum=sum+value
print(“The sum is ”,sum)
Output:
The sum is 6
NESTED-FOR-LOOP:
Algorithm:
Step 1:The first for condition is checked first.
Step 2: If The condition is True, then the statements inside the loop
will be executed.
Step 3: If The condition is False, the loop body will be skipped and the statements
after The for loop will be executed(i.e, the nested for loop will be executed).
Step 4:If The condition is False, the loop body will be skipped and the statements outside
for loop will be executed.
Program:for i in range(0,3):
for j in range(0,3):
print(i,end=' ')
print("\n")
Output:0 0 0
111
222
Result: The program to run and execute different control statements in python is
successfully completed .
EX.NO:4 USER DEFINED FUNCTIONS
DATE:
Aim: To demonstrate different types of user defined functions in python.
Algorithm: Step 1:Start.
Step 2:Obtain The input from the user.
Step 3:Perform calculation using different types of function.
Step 4:Display the output.
Step 5:Stop.
Program:
def a1(x,y):
sum=int(x)+int(y)
print("sum=",sum)
a=input("a=")
b=input("b=")
a1(a,b)
Output:
>>>a=1
>>>b=2
Sum=3
Program:
def a1(x, y):
sum = x + y
return sum
a = int(input("a = "))
b = int(input("b = "))
result = a1(a, b)
print("Sum =", result)
Output:
>>>a=1
>>>b=2
Sum=3
Program:
def a1():
a = int(input("a = "))
b = int(input("b = "))
sum = a + b
return sum
result = a1()
print("Sum =", result)
Output:
>>>a=1
>>>b=2
Sum=3
Program:
def a1():
a = int(input("a = "))
b = int(input("b = "))
sum = a + b
print("Sum =", sum)
a1()
Output:
>>>a=1
>>>b=2
Sum=3
Result: The program to print a simple calculation using different functions in python
programming was successfully completed.