Practical File
Class XII - Computer Science with Python(083)
Program 1: Write a Python Program to enter two numbers and print the
arithmetic operations like +,-,*, /, // and %.
Aim : To enter two number and perform the arithmetic operations
Source Code :
#Program for Arithmetic Calculator
result = 0
val1 = float(input("Enter the first value :"))
val2 = float(input("Enter the second value :"))
op = input("Enter any one of the operator (+,-,*,/,//,%)")
if op == "+":
result = val1 + val2
elif op == "-":
result = val1 - val2
elif op == "*":
result = val1 * val2
elif op == "/":
if val2 == 0:
print("Please enter a value other than 0")
else:
result = val1 / val2
elif op == "//":
result = val1 // val2
else:
result = val1 % val2
print("The result is :",result)
Output :
Enter the first value :50
Enter the second value :24
Enter any one of the operator (+,-,*,/,//,%) +
The result is : 74.0
Enter the first value :50
Enter the second value :24
Enter any one of the operator (+,-,*,/,//,%) -
The result is : 26.0
Enter the first value :50
Enter the second value :24
Enter any one of the operator (+,-,*,/,//,%) *
The result is : 1200.0
Enter the first value :50
Enter the second value :24
Enter any one of the operator (+,-,*,/,//,%) /
The result is : 2.08333333333333335
Enter the first value :50
Enter the second value :24
Enter any one of the operator (+,-,*,/,//,%) //
The result is : 2.0
Enter the first value :50
Enter the second value :24
Enter any one of the operator (+,-,*,/,//,%) %
The result is : 2.0
Result : Thus the Program is executed successfully
2. Write a Python Program to enter the number of terms and to print the
Fibonacci Series.
Aim: To Print the Fibonacci Series of N terms
Source Code :
i =int(input("Enter the limit:"))
x=0
y=1
z=1
print("Fibonacci series :\n")
print(x, y,end= " ")
while(z<= i):
print(z, end=" ")
x=y
y=z
z=x+y
Output :
Enter the limit: 50
Fibonacci Series :
0 1 1 2 3 5 8 13 21 34
Result :
Thus the Program is executed successfully.
3. Write a Program to check if the entered number is Armstrong or not.
Aim: To print the given number is Armstrong number or not
#An Armstrong number has sum of the cubes of its digits is equal to the
number itself
Source Code :
no=int(input("Enter any number to check : "))
no1 = no
sum = 0
while(no>0):
ans = no % 10;
sum = sum + (ans * ans * ans)
no = int (no / 10)
if sum == no1:
print("Armstrong Number")
else:
print("Not an Armstrong Number”)
Output:
Enter any number to check : 523
Not an Armstrong Number
Enter any number to check : 371
Armstrong Number
Result :
Thus the Program is executed successfully.
4. Write a menu driven Python program to calculate
Area of triangle
Area of a circle
• Area of Rectangle
Aim: To find the area of a circle, triangle, rectangle in Python
Source Code:
C=’y’
while(C==’y’):
print(“1.Area of triangle”)
print(“2.Area of Circle”)
print(“3.Area of Rectangle”)
choice=int(input(“Enter the choice”))
if(choice==1):
b=int(input(“enter the base”))
h = int (input(“Enter the height”))
Area = 0.5 * b*h
Print(“Area of triangle is”, Area”)
elif(choice==2):
r=int(input(“Enter the radius”))
Area= 3.14*r*r*
print(“the area of the circle is”, Area)
elif(choice==3):
l=int(input(“enter the length”))
b=int(input(“enter the breadth”))
Area= l*b
print(“The Area of the Rectangle is “, Area)
else:
print(“Wrong input”)
c=input(“Do you want to continue?”)
Output:
1.Area of triangle
2.Area of Circle
3.Area of Rectangle
Enter the choice : 1
Enter the base: 5
Enter the height : 6
Area of Triangle is 15.0
Do you want to continue ? y
1.Area of triangle
2.Area of Circle
3.Area of Rectangle
Enter the choice : 3
Enter the length:2
Enter the breadth : 8
Area of Rectangle is 16.0
Do you want to continue ? n
Result : thus the program is executed successfully.
5. Write the Python Program to find the given number is perfect or not.
Aim : To find the given number is perfect number or not
Source code:
def pernum(num):
divsum=0
for i in range(1,num):
if num%i == 0:
divsum+=i
if divsum==num:
print('Perfect Number')
else:
print('Not a perfect number')
pernum(6)
pernum(15)
Output
Perfect Number
Not a perfect number
Result : thus the program executed successfully.
Ex.No: Date
Write a Python Program to Print Largest Even and Largest Odd Number in a List.
Aim:
To print Largest Even Number and Largest Odd Number in a List.
Source Code:
n=int(input("Enter the number of elements in the list"))
b=[]
for i in range(0,n):
a=int(input("Enter element"))
b.append(a)
print("The entered list",b)
c=[]
d=[]
for i in b:
if i%2==0:
c.append(i)
c.sort()
else:
d.append(i)
d.sort()
print("Even numbers",c)
e=len(c)-1
print("Largest even number is",c[e])
print("Odd numbers",d)
f=len(d)-1
print("Largest odd number is ", d[f])
Output:
Result:
Thus the given program executed successfully.
Ex.No: Date
Write a menu driven Program to perform the following
a)addition of two tuples
b) Printing the duplicates values in a tuple
Aim:
To implement a menu driven program to perform addition of two tuples and
printing the duplicates values in a tuple
Source code:
print("Enter 1 for Addition of two tuples")
print("Enter 2 for Printing the duplicate values")
ch=int(input("Enter 1 or 2"))
if ch==1:
tp1=tuple(input("Enter 1st tuple set"))
tp2=tuple(input("Enter 2nd tuple set"))
print("Addition of two tuples")
tup=tp1+tp2
print(tup)
if ch==2:
tp3=tuple(input("Enter tuple values"))
print("Entered tuple are",tp3)
for i in tp3:
if tp3.count(i)>=2:
print("Duplicate values are",i)
Output:
Result:
Thus the given program executed successfully.
Ex:No:8
Write a menu driven program to perform the following
i) To check the given string is Palindrome
ii) To count the upper case and lowercase character
Aim:
(i) To find the given string is Palindrome or not.
(ii) To count the upper case characters and lowercase characters in the given string of
characters.
Source Code:
print("Enter 1 for palindrome program")
print("Enter 2 for counting a uppercase letters and lowercase letters")
ch=int(input("Enter a number 1 or 2"))
if ch==1:
msg=input("Enter any string")
newlist=[]
newlist[:0]=msg
l=len(newlist)
ed=l-1
for i in range(0,l):
if newlist[i]!=newlist[ed]:
print("Given string is not palindrome")
break
if i>=ed:
print("Given string is palindrome")
break
l=l-1
ed=ed-1
if ch==2:
line=input("Enter a line")
lowercount=uppercount=0
for a in line:
if a.islower():
lowercount+=1
if a.isupper():
uppercount+=1
print("Number of Uppercase letters",uppercount)
print("Number of Lowercase letters",lowercount)
Output:
Result:
Thus the given program is executed successfully.
Ex:No:9 Random number generation
Aim:
To generate random numbers between 1 to 6 (stimulate a
dice)
Source Code:
import random
Guess = True
while Guess:
n = random.randint(1,6)
userinput = int(input("Enter a number between 1 to 6: "))
if userinput == n:
print("Congratulations!!!,You won the lottery")
else: print("Sorry, Try again, The lucky number was: ",n)
val = input("Do you want to continue y/n: ")
if val in ['y','Y']:
Guess = True
else:
Guess = False
Output:
Enter a number between 1 to 6: 2
Sorry, Try again, The lucky number was: 4
Do you want to continue y/n: y
Enter a number between 1 to 6: 3
Sorry, Try again, The lucky number was: 2
Do you want to continue y/n: y
Enter a number between 1 to 6: 5
Sorry, Try again, The lucky number was: 4
Do you want to continue y/n: y
Enter a number between 1 to 6: 2
Congratulations!!!,You won the lottery
Do you want to continue y/n: n
Result:
The above program has been executed successfully
Ex.No: Date
Write a python program that reads a text file and creates another file that is
identical except for every sequence of consecutive white spaces is replaced by a
single space.
Ex: Python Programming Language
Python Programming Language
Aim:
To read a text file and creates another file that is identical except for every
sequence of consecutive white spaces is replaced by a single space.
Source Code:
f1=open("d:\\read.txt","r")
f2=open("d:\\read1.txt","w")
s=" "
while s:
s=f1.readline()
s1=s.split()
print(s1)
for i in s1:
f2.write(i)
f2.write(" ")
f1.close()
f2.close()
Output:
read.txt
read1.txt
Output:
Result:
Thus the given program executed successfully.
Ex.No: Date
Write a Python Program to read data from data file in read mode and count the
particular word occurrences in given string, number of times.
Aim:
To count the number of “to” in the file.
Source Code:
f1=open("d:\\file\\file1.txt","r")
s=" "
count=0
while s:
s=f1.readline()
s1=s.split(' ')
for i in s1:
i=i.lower()
if i=='to' or i=='to\n' or i=='To\n' or i=='To':
count+=1
print("total no of 'to'", count)
f1.close()
Output:
d:\\file\\file1.txt
Result:
Thus the given program executed successfully.
Ex: No: 12 Date:
Write a function word5( ) in Python that displays 5 letter words present in a text file ‘myfile.txt’.
If the ‘myfile.txt’ contents are as:
We are here to learn python. Python has so many features which make it more comfortable
interface.
Output:
learn
which
Aim:
A function word5( ) in python that displays 5 letter words present in a text file ‘myfile.txt’.
Source Code:
def word5():
f1=open(r“E:\myfile.txt”,”r”)
m=f1.read()
n=m.split()
for word in n:
if len(word) == 5:
print(word)
f1.close()
word5()
Output:
Result:
Thus, the given program executed successfully.
Ex.No:13 Date
Write a Python program for inserting a record in a binary file and read a record
from the binary file.
Aim:
To insert a record in a binary file and read a record from the binary file.
Source code:
import pickle
f=open("e:\\stud.dat",'wb')
while True:
rno=int(input("Enter Rollno:"))
name=input("Enter Name")
marks=int(input("Enter Marks:"))
L=[rno,name,marks]
pickle.dump(L,f)
ch=input("Enter y to continue n to exit")
if ch.upper()=='N':
break
f.close()
f=open("e:\\stud.dat",'rb')
try:
while True:
rec=pickle.load(f)
print(rec)
except:
f.close()
Output:
Result:
Thus the given program executed successfully.
Ex:No: 14 Date:
Write a program in Python using Pickle library and Create a binary file with
following structure
· Travelid
· Departure Location
· Destination
· Append data to the file
· Search a particular travel-id given by the user and display the details
Aim:
To insert a record in a binary file, read a record from the binary file and search a
record from the binary file.
Source Code:
import pickle
f=open("e:\\Travel.dat",'ab')
while True:
Travelid=int(input("Enter Travelid:"))
DL=input("Enter Departure Location:")
D=input("Enter Destination:")
L=[Travelid,DL,D]
pickle.dump(L,f)
ch=input("Enter y to continue n to exit")
if ch.upper()=='N':
break
f.close()
f=open("e:\\Travel.dat",'rb')
try:
tid=int(input("Enter travelid to search"))
while True:
rec=pickle.load(f)
if rec[0]==tid:
print(rec)
except:
f.close()
Output:
Result:
Thus the given program executed successfully.
Ex:No:15 Date:
Write a Python program for inserting a record in a csv file and read a record from the csv file.
Aim:
To insert a record in a csv file and read a record from the csv file.
Source code:
import csv
f=open("e:\\marks.csv",'w')
fields=['Name','Class', 'Year', 'Percent']
csv_w=csv.writer(f)
csv_w.writerow(fields)
while True:
Name=input("Enter name")
Class=input("Enter Class")
Year=int(input("Enter the Year"))
Percent=int(input("Enter the percentage"))
rows=[Name,Class,Year,Percent]
csv_w.writerow(rows)
ch=input("Enter Y to continue N to break")
if ch.upper()=='N':
break
f.close()
with open("e:\\marks.csv","r") as file:
read=csv.reader(file)
for i in read:
print(i)
file.close()
Output:
Result:
Thus the given program executed successfully.