EX.
NO:1
TEMPERATURE CONVERSION
DATE:
AIM:
To write a python program to convert the given temperature from Fahrenheit to Celsius
and vice versa.
ALGORITHM:
Step 1: Start
Step 2: Declare the variable temp and read the input from user
Step 3 if a==’f’:
Calculate con_temp=(temp-32) * 5/9
else:
Calculate con_temp=(9/5*temp)+32
Step 4: Stop
PROGRAM:
print(“Enter (F) to convert Fahrenheit to Celsius”)
print(“Enter (C) to convert Celsius to Fahrenheit”)
a=input(“Enter your choice:”)
if a==’f’:
temp=int(input(“Enter temperature to convert:”))
con_temp=(temp-32)*5/9
print(temp,’degrees fahrenheit equals’,con_temp,’degree celsius’)
else:
temp=int(input(“Enter temperature to convert:”))
con_temp=(9/5*temp)+32
print(temp,’degrees celsius equals’,con_temp,’degree fahrenheit’)
OUTPUT:
Result:
Thus the program has been executed successfully.
EX.NO:2
DIAMOND PATTERN PROGRAM
DATE:
AIM:
To Write a python program to construct the diamond pattern using nested for
loop
ALGORITHM:
Step 1: Start
Step 2: Read the input values
Step 3: To print the pattern using the nested loop of for
Step 4: Stop
PROGRAM:
rows=4
k=2*rows-2
for i in range(0,rows):
for j in range(0,k):
print(end=” “)
k=k-1
for j in range(0,i+1):
print(“*”,end=” “)
print(“ “)
k=rows-2
for i in range(rows,-1,-1):
for j in range(k,0,-1):
print(end=” “)
k=k+1
for j in range(0,i+1):
print(“*”,end=” “)
print(“ “)
OUTPUT:
Result:
Thus the program has been executed successfully.
EX.NO:3
STUDENT MARK LIST CALCULATION
DATE:
AIM:
To write a python program to calculate total Marks, average and grades of a student.
ALGORITHM:
Step 1: Start
Step 2: Read the input from user (mark1, mark2, mark3, mark4, makr5)
Step 3: Calculate sum and average
Step 4: Display Sum, average and grade
Step 5: Stop
PROGRAM:
mark1=int(input("Enter your English mark:"))
mark2=int(input("Enter your Accounts mark:"))
mark3=int(input("Enter your Business studies mark:"))
mark4=int(input("Enter your Economics mark:"))
mark5=int(input("Enter your Computer science mark:"))
sum=mark1+mark2+mark3+mark4+mark5
print("Total marks=",sum)
avg=sum/5
if (avg>=80):
print("Your grade is A")
elif(avg>=70 and avg<80):
print("Your grade is B")
elif(avg>=60 and avg<70):
print("Your grade is C")
elif(avg>=40 and avg<60):
print("Your grade is D")
elif(avg<40 and avg>=35):
print("Your grade is E")
else:
print("No grade")
OUTPUT:
Result:
Thus the program has been executed successfully.
EX.NO:4
AREA OF DIFFERENT SHAPES
DATE:
AIM:
To write a python program to find the area of rectangle, square, circle, triangle.
ALGORITHM:
Step 1: Start
Step 2: Read the input from user for side, length, breadth, radius, height
Step 3: To calculate and display the area of rectangle square, circle and
triangle and call the corresponding function
Step 4: Stop
PROGRAM:
def square(s):
return(s*s)
def rect(l,b):
return(l*b)
def circle(r):
return(3.14*r*r)
def tri(b,h):
return((b*h)/2)
s=int(input(“Enter side:”))
b=int(input(“Enter breadth:”))
l=int(input(“Enter length:”))
r=int(input(“Enter radius:”))
print(“Area of square=”,square(s))
print(“Area of rectangle=”,rect(l,b))
print(“Area of circle=”,circle(r))
print(“Area of triangle=”,tri(b,h))
OUTPUT:
Result:
Thus the program has been executed successfully.
EX.NO:5
DATE:
PRIME NUMBER
AIM:
To write a python program to find the prime number using nested loop.
.
ALGORITHM:
Step 1: Start
Step 2: Read n
Step 3: Initialize a nested for loop starting from 2 ending at the integer value n+1
Step 4: Declare flag==0
Step 5: Then Initialize inner nested for loop
Step 6:Use the prime Check function to check if the number is a prime or not
Step 7:If not prime, break the loop to the next outer loop
Step 8:If prime, print it.
Step 9:Run the for loop till the n value is reached.
Step 10: Stop
PROGRAM:
n=20
for i in range (2,n+1):
flag=0
for j in range(2,int(i/2)+1):
if((i%j)==0):
flag=1
break
if (flag==0):
print(i,end="\n")
OUTPUT:
Result:
Thus the program has been executed successfully.
EX.NO:6
FACTORIAL USING RECURSION
DATE:
AIM:
To write a python program to find factorial of the give number using
recursive function.
ALGORITHM:
Step 1: Start
Step 2: Read numbers num
Step 3: if num<0 then no value
else call fact(n)
Step 4: Print factorial
Step 5: Stop
PROGRAM:
def fact(n):
if n==1:
return n
else:
return n*fact(n-1)
num=int(input(“Enter a number:”))if
num<0:
print(“NO VALUE”)elif
num==0:
print(“The factorial of 0 is 1”)
else:
print(“The factorial of”, num,” is”, fact(num))
OUTPUT:
Result:
Thus the program has been executed successfully.
EX.NO:7
ODD AND EVEN NUMBERS COUNT
DATE:
CALCULATION
AIM:
To write a python program to count the number of even and odd numbers from array of
n numbers.
ALGORITHM:
Step1: Start
Step2: Read the numbers to check if the input is odd or even.
Step 3: If the number is divided by two and gives a remainder of zero then a number
is even. If the reminder is 1, it is an odd number.
Step4: Print the result.
Step5: Stop.
PROGRAM:
import array as arr
numbers=arr.array('i',[1,2,3,4,5,6,7,8,9])
count_odd=0
count_even=0
for i in numbers:
if i%2==0:
count_even+=1
else:
count_odd+=1
print("Number of even numbers:",count_even)
print("Number of odd numbers:",count_odd)
OUTPUT:
Result:
Thus the program has been executed successfully.
EX.NO:8
REVERSE A STRING WORD
DATE: BY WORD
AIM:
To write a python program using class to reverse a string word by word.
ALGORITHM:
Step1: Start
Step2: Create class & user defined function
Step 3: Split the string
Step 4: Reverse & join the string then return result
Step 5: Getting the Input from User
Step 6: Call the user defined function to print the result
Step7: Stop
PROGRAM:
class rev:
def revr(self,strs):
s=strs.split()
s.reverse()
result=" ".join(s)
return result
str1=input("Enter a String with two or more words:")
print("Reverse the String word by word:\n",rev().revr(str1))
OUTPUT:
Result:
Thus the program has been executed successfully.
EX.NO:9
COUNT THE OCCURRENCES OF ITEMS
DATE: USING TUPLE & LIST
AIM:
To write a Python program to count the occurrences of all items of the list in the tuple.
ALGORITHM:
Step1: Start
Step 2: From collections import counter
Step 3: Create user defined function countoccur
Step 4: Create variable & assign the counted value into counts using counter
Step 5: Returns Result
Step 6: Create tuple & list
Step 7: Call the user defined function to print the result
Step 8: Stop
PROGRAM:
from collections import Counter
def CountOccur(tup,lst):
Counts=Counter(tup)
return sum(Counts[i] for i in lst)
tup=['a','a','c','b','d']
lst=('a','b')
print(CountOccur(tup,lst))
OUTPUT:
Result:
Thus the program has been executed successfully.
EX.NO:10
DATE: SAVINGS ACCOUNT
AIM:
To write a Python program to create a class savings account has interest rate and method
increases the balance by appropriate amount of interest using inheritance.
ALGORITHM:
Step1: Start
Step2: Create class savings account
Step 3: Create user-defined functions for getting name,
account number, branch and amount
Step4: Create child class
Step 5: Create user-defined function withdraw
Step 6: Create user-defined function display, calculate amount depends on
rate of interest
Step 7: Create object & call all the user-defined functions.
Step 8: Stop
PROGRAM:
class savings_account:
def __init__(self):
self.balance=0
print("\n VIVID UAVA BANK \n")
print("hello!! welcome to the deposit and withdrawal machine")
def int(self,name,account_num,branch,address,phone_num):
self.name=name
self.account_num=account_num
self.branch=branch
self.phone_num=phone_num
def getname(self):
return self.name
def getaccount_num(self):
return self.account_num
def getbranch(self):
return self.branch
def getphone_num(self):
return self.phone_num
def deposit(self):
amount=float(input("Enter amount to deposit:"))
self.balance+=amount
print("\n Deposited Amount is:",amount)
class available_balance(savings_account):
def withdraw(self):
amount=float(input("Enter amount to be withdrawn:"))
if self.balance>=amount:
self.balance-=amount
print("\n Your Withdrawn Amount is:",amount)
else:
print("\n Insufficient Balance")
def display(self):
print("\n Available Balance=",self.balance)
print("Time Period",t)
print("Rate Of Interest",r)
si=(self.balance*t*r)/100
net_balance=self.balance+si
print("\n Net Balance is",net_balance)
s=available_balance()
name=input("Enter name:")
phone_num=input("Enter phone_num:")
branch=input("Enter branch:")
account_num=input("Enter account_num:")
s.deposit()
s.withdraw()
t=float(input("Enter the Time Period:"))
r=float(input("Enter the Rate of Interest:"))
s.display()
OUTPUT:
Result:
Thus the program has been executed successfully.
EX.NO:11
COPY THE FILE CONTENTS
DATE:
AIM:
To write a python program to read a file content and copy only the contents of odd
lines into a new file.
ALGORITHM:
Step1: Start
Step2: Create to file objects namely F1 for reading and F2 for writing
Step 3: Read the file contents of F1 and find it as odd or even by using condition
Step4: If it is odd line then write the content to F2 and close the file F2
Step 5: Open the file F2 in read mode
Step 6: Read the content of F2 and display them
Step 7: Close both the files
Step 8: Stop
PROGRAM:
f1=open('file1.txt','r')
f2=open('file2.txt','w')
cont=f1.readlines()
for i in range(0,len(cont)):
if i%2==0:
f2.write(cont[i])
else:
pass
f2.close()
f2=open('file2.txt','r')
cont1=f2.read()
print(cont1)
f1.close()
f2.close()
OUTPUT:
file1.txt
file2.txt
Result:
Thus the program has been executed successfully.
EX.NO:12
TURTLE GRAPHICS WINDOW
DATE:
CREATION
AIM:
To write a python program to create a turtle graphics window with specific size.
ALGORITHM:
Step1: Start
Step2: Import the turtle module
Step3: Create a turtle object
Step4: Define the size of the screen with height and width attribute
Step5: Exit the turtle window
Step 6: Stop
PROGRAM:
import turtle
draw_area=turtle.Screen()
draw_area.setup(width=500,height=450)
turtle.exitonclick()
OUTPUT:
Result:
Thus the program has been executed successfully.
EX.NO:13
TOWER OF HANOI
DATE:
AIM:
To write a python program for Tower of Hanoi using recursion.
ALGORITHM:
Step1: Start
Step2: Read number of disks from user.
Step 3: Define the recursive function, namely Tower of Hanoi.
Step4: Call the function TowerOf Hanoi with number of disk and A, B, C rods.
Step 5: Check if number of disc equal to one then disk1 one move from Source rod
to the destination rod.
Step 6: Otherwise, call the function recursively until all the disks are arranged in
order.
Step 7: Stop
PROGRAM:
def TowerOfHanoi(n ,from_rod, to_rod, aux_rod):
if n==1:
print ("Move disk 1 from source",from_rod,"to destination",to_rod)
return
TowerOfHanoi(n-1, from_rod, aux_rod, to_rod)
print ("Move disk",n,"from source",from_rod,"to destination",to_rod)
TowerOfHanoi(n-1, aux_rod, to_rod, from_rod)
disks=int(input('Enter number of disks:'))
TowerOfHanoi(disks,'A','B','C')
OUTPUT:
Result:
Thus the program has been executed successfully.
EX.NO:14
MENU DRIVEN USING DICTIONARY
DATE:
AIM:
To create a menu driven python program with a dictionary for words and their
meanings.
ALGORITHM:
Step1: Start
Step 2: Create the dictionary
Step 3: Import the module json
Step 4: Import the get close matches function from difflib module.
Step 5: Load the dictionary to the variable data
Step 6: Define the function translate.
Step 7: Check if the words in dictionary return the respective key value.
Step 8: Otherwise, display the word is not in the dictionary.
Step 9: Read the word from the users.
Step 10: Call the translate function and store the result in the output variable and
display it.
Step 11: Stop
PROGRAM:
import json
from difflib import get_close_matches
data=json.load(open("dict.json"))
def translate(w):
w=w.lower()
if w in data:
return data[w]
elif len(get_close_matches(w,data.keys()))>0:
yn=input("Did you mean %s instead? Enter Y if yes,or N if
no:"%get_close_matches(w,data.keys())[0])
yn=yn.lower()
if yn=="y":
return data[get_close_matches(w,data.keys())[0]]
elif yn=="n":
return"The word dosen't exist.Please double check it."
else:
return "We didn't understand your entry."
else:
return "The word dosen't exist.please double check it."
word=input('enter word:')
output=translate(word)
if type (output)==list:
for item in output:
print(item)
else:
print(output)
input('Press ENTER to exit')
dict.json
{“Software”: “The program and other operating information used by a computer”,
“Module”: “collection of source files”,
“Exception”: “An Exception is an event , which occurs during the execution of a program
that disrupts the normal flow of the program instruction”,
“String”: “OOPS is a programming paradigm based on the concepts of objects ,which can
contain data and code”}
OUTPUT:
Result:
Thus the program has been executed successfully.
EX.NO:15
HANGMAN GAME
DATE:
AIM:
To write a python program to implement the hangman game
ALGORITHM:
Step1: Start
Step2: Import the time module
Step 3: Read the time module
Step4: Assign the secret word to be found
Step5: Assign the maximum number of turns
Step 6: Repeat the steps until turns is less than zero
Step 7: Initialize the counter variable failed to zero
Step 8: Loop the statement for each characters in the word
Step 9: Check if char is in guesses then display
Step 10: Otherwise display a dash and increment the counter value
Step 11: Check if the counter value equal to zero
Step 12: Check if the turns values equals to zero then the player lose
Step 13: Stop
PROGRAM:
import time
name=input("What's your name:")
print('Hello',name)
print('')
time.sleep(1)
print('Start guessing...')
time.sleep(0.5)
word='cash'
guesses=''
turns=10
while turns>0:
failed=0
for char in word:
if char in guesses:
print(char,)
else:
print('_')
failed+=1
if failed==0:
print('You Won')
break
print('')
guess=input('Guess a character:')
guesses+=guess
if guess not in word:
turns-=1
print('WRONG')
print('You have',turns,'more guesses')
if turns==0:
print('You lose')
OUTPUT:
Result:
Thus the program has been executed successfully.