Python Projects
Python Projects
Python Projects
Class: Xll-‘B’
Roll no.: 11
import math
def cal_sqrt():
number=float(input("Enter a number: "))
sq_root=math.sqrt(number)
#Display the square root
print("The square root of",number,"is",sq_root)
cal_sqrt()
Output:
Enter a number: 36
The square root of 36.0 is 6.0
2. Write a function to find factorial of a given number.
The function should return the calculated factorial
using return statement.
def factorial(n):
if n == 1:
return n
else:
return n*factorial(n-1)
if num < 0:
print("Sorry, factorial for a negative number does not
exist")
elif num == 0:
print("The factorial of 0 is 1")
else:
print("The factorial of",num,"is",factorial(num))
Output:
Enter a number :8
The factorial of 8 is 40320
3. Write a function to accept string as an input and to count
and display the total number of vowels present in it.
def countVowels(str1):
count=0
for ch in str1:
if ch in "aeiouAEIOU":
count+=1
return count
# Main code
# function calls
str_input=input("Enter any String :")
count =countVowels(str_input)
print("Total no. of vowels present in the string are :",count)
Output:
Enter any String :Hello i am using function
Total no. of vowels present in the string are : 9
4. Input elements in a tuple and to count and display number
of even and odd number present in it using function.
def countEvenOdd(tup):
counteven=0
countodd=0
for i in tup:
if i % 2 ==0:
counteven += 1
else:
countodd += 1
return counteven,countodd
tup1=()
n=int(input("Enter the total elements in a tuple: "))
for i in range(n):
num=int(input("Enter the number :"))
tup1=tup1 + (num,)
count_stats=countEvenOdd(tup1)
print("Even numbers are:",count_stats[0])
print("Odd numbers are:",count_stats[1])
Output:
Enter the total elements in a tuple: 4
Enter the number :1
Enter the number :2
Enter the number :3
Enter the number :4
Even numbers are: 2
Odd numbers are: 2
5. Compute the area of rectangle on the basis of length and
breadth inputed by the user as the arguments to this
function.
Output:
Enter the following values for rectangle:
Length : integer value: 10
Breadth : integer value: 20
Area of rectangle is 200
6.Write a program to write dictionary to the
binary file.
import pickle
dict1 = {'Python':90, 'Java': 95, 'C++': 85}
f = open('bin_file.dat','wb')
pickle.dump(dict1,f)
f.close()
Output:
7.Write a program to read dictionary items
from the binary file.
import pickle
f = open('bin_file.dat','rb')
dict1 = pickle.load(f)
f.close()
print(dict1)
Output:
{'Python': 90, 'Java': 95, 'C++': 85}
8.Store and display multiple integers in and
from a binary file.
def binfile():
import pickle # line1
file = open('data.dat','wb') # line 2
while True:
x = int(input("Enter the integer: ")) # line 3
pickle.dump(x,file) # line 4
ans = input('Do yo want to enter more data Y / N :')
if ans.upper()== 'N' : break
file.close() # line 5
file = open('data.dat','rb') # line 6
try : # line 7
while True : # line 8
y = pickle.load(file) # line 9
print(y) # line 10
except EOFError : # line 11
pass
file.close() # line 12
binfile()
Output:
Enter the integer: 7
Do yo want to enter more data Y / N :Y
Enter the integer: 5
Do yo want to enter more data Y / N :N
7
5
9. Program for inserting/appending a record in a binary
file “student.dat”.
import pickle
record=[ ]
while True:
roll_no = int(input("Enter student roll no:"))
name = input("Enter the student name:")
marks=int(input("Enter the marks obatined:"))
data=[roll_no,name,marks]
record.append(data)
choice=input("wish to enter more records (Y/N)?:")
if choice.upper()=='N':
break
f=open("student","wb")
pickle.dump(record,f)
print("Record Added")
f.close()
Output:
Enter student roll no :6
Enter the student name: Rohit
Enter the marks obtained :499
wish to enter more records (Y/N)?: Y
Enter student roll no :5
Enter the student name :Karan
Enter the marks obtained :490
wish to enter more records (Y/N)?: N
Record Added
10. Program to read a record from the
binary file “student.dat”.
import pickle
f = open("student","rb")
stud_rec = pickle.load(f) # To read the object from the opened file
print("Contents of students file are:")
# reading the fields from the file
for R in stud_rec:
roll_no = R[0]
name = R[1]
marks = R[2]
print(roll_no,name, marks)
f.close()
Output:
Contents of students file are:
6 Rohit 499
5 Karan 490
11. Write a menu-driven program to perfrom all the basics operations using
dictionary on student binary file such as inserting, reading, updating,
searching and deleting a record.
import os
import pickle
while True:
print('Type 1 to insert rec. ')
print('Type 2 to display rec. ')
print('Type 3 to search rec. ')
print('Type 4 to update rec. ')
print('Type 5 to delete rec. ')
print('Enter your choice 0 to exit')
choice = int(input("Enter your choice:"))
if choice == 0:
break
elif choice == 1:
insertRec ()
elif choice == 2:
readRec ()
elif choice == 3:
r = int(input("Enter a rollno to search:"))
searchRollNo(r)
elif choice == 4:
r = int(input("Enter a rollno:"))
m = int(input("Enter new Marks:"))
updateMarks(r, m)
elif choice == 5:
r = int(input("Enter a rollno:"))
deleteRec(r)
Output:
Type 1 to insert rec.
Type 2 to display rec.
Type 3 to search rec.
Type 4 to update rec.
Type 5 to delete rec.
Enter your choice 0 to exit
Enter your choice:1
Enter roll number:5
Enter Name:Kiran
Enter Marks:456
Type 1 to insert rec.
Type 2 to display rec.
Type 3 to search rec.
Type 4 to update rec.
Type 5 to delete rec.
Enter your choice 0 to exit
Enter your choice: 2
Roll Num: 5
Name: Kiran
Marks: 456
Type 1 to insert rec.
Type 2 to display rec.
Type 3 to search rec.
Type 4 to update rec.
Type 5 to delete rec.
Enter your choice 0 to exit
Enter your choice:0
12. Write a Python program to implement all basic operations of a Stack, such
as adding element (PUSH operation), removing element (POP operation)
and displaying the Stack elements (Traversal operation) sing lists.
s=[ ]
c="y"
while (c=="y"):
print ("1. PUSH")
print ("2. POP ")
print ("3. Display")
choice=int(input("Enter your choice: "))
if (choice==1):
a=input("Enter any number :")
s.append(a)
elif (choice==2):
if (s==[ ]):
print ("Stack Empty")
else:
print ("Deleted element is : ",s.pop())
elif (choice==3):
l=len(s)
for i in range(1-1,-1,-1): #To display elements from last element to first
print (s[i])
else:
print("Wrong Input")
c=input("Do you want to continue or not? ")
Output:
1. PUSH
2. POP
3. Display
Enter your choice: 2
Stack Empty
Do you want to continue or not? n