Professional Documents
Culture Documents
ClassXII-Practical File
ClassXII-Practical File
ClassXII-Practical File
9. Read a text file line by line and display each word separated
by a #.
11. Write a Program to remove all the lines that contain the
character 'a' in a file and write it to another file.
12. Write a program to create a binary file with name and roll
number. Search for a given roll number and display the name,
if not found display appropriate message.
13. Create a binary file with roll number, name and marks. Input a
roll number and update the marks.
15. Create a CSV file by entering user-id and password, read and
search the password for given userid.
1
PRACTICAL FILE
Experiment 1: Date :
Program : Input any number from user and calculate factorial of a number.
Output :
Factorial of 5 is : 120
Factorial of 10 is : 3628800
2
Experiment 2: Date :
Program : Input any number from user and check it is Prime no. or not.
---------------------------------------------------------------------------------------------------------------------------------------
import math
isPrime=True
for i in range(2,int(math.sqrt(num))+1):
if num % i == 0:
isPrime=False
if isPrime:
else:
Output :
## Number is Prime ##
## Number is Prime ##
3
Experiment 3: Date :
Output :
Values inside the function: [10, 20, 30, [1, 2, 3, 4]]
Values outside the function: [10, 20, 30, [1, 2, 3, 4]]
Output :
4
Experiment 4: Date :
------------------------------------------------------------------------------------------------------------------------------------
if num == 0:
return 0
else:
return lst[num-1] + findSum(lst, num-1)
for i in range(num):
n = int(input("Enter Element " +str(i+1) + ":"))
Output :
5
Experiment 5: Date :
def nthfiboterm(n):
if n<=1:
return n
else:
return (nthfiboterm(n-1)+nthfiboterm(n-2))
term =nthfiboterm(num)
Output :
6
Experiment 6: Date :
------------------------------------------------------------------------------------------------------------------------------------
# Function to do insertion sort
def insertionSort(arr):
key = arr[i]
j = i-1
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
insertionSort(arr)
for i in range(len(arr)):
Output:
11
12
13
7
Experiment 7: Date :
-------------------------------------------------------------------------------------------------------------------------------------
def binary_search(arr, low, high, x):
if arr[mid] == x:
return mid
else:
# Element is not present in the array
return -1
# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
if result != -1:
print("Element is present at index", str(result))
else:
print("Element is not present in array")
Output :
8
Experiment 8: Date :
Write a Python program to find out number of occurrence of any word in given string.
------------------------------------------------------------------------------------------------------------------------------------
s = str1.split()
count = 0
for w in s:
if w == word:
count+=1
return count
if count == 0:
else:
Output :
Enter any Sentence:Welcome to Python Programming.
Enter word to search in a sentence:Python
## Python occurs 1 times##
9
Experiment 9:
Read a text file line by line and display each word separated by a #.
--------------------------------------------------------------------------------------------------------------------------------------
#program to read content of file line by line and display each word seperated by '#'
f = open("file1.txt")
for line in f:
words = line.split()
for w in words:
print()
f.close()
10
Experiment no. 10 Date :
-----------------------------------------------------------------------------------------------------------------------------------
f = open("file2.txt")
v=0
c=0
u=0
l=0
o=0
data = f.read()
vowels = ['a','e','i','o','u']
for ch in data:
if ch.isalpha():
if ch.lower() in vowels:
v+=1
else:
c+=1
if ch.isupper():
u+=1
elif ch.islower():
l+=1
o += 1
f.close()
12
Experiment no. 11 Date:
Write a Program to remove all the lines that contain the character 'a' in a file and write it to
another file.
f1 = open("file2.txt")
f2 = open("file2copy.txt","w")
OUTPUT
13
Experiment No. 12 Date:
Write a program to create a binary file with name and roll number. Search for a given roll number
and display the name, if not found display appropriate message.
while ans.lower()=='y':
found=False
r = int(input("Enter Roll number to search :")) for s in
student:
if s[0]==r:
print("## Name is :",s[1], " ##") found=True
break
if not found:
print("####Sorry! Roll number not found ####")
ans=input("Search more ?(Y) :")
f.close()
14
OUTPUT
15
Experiment No. 13 Date:
Create a binary file with roll number, name and marks. Input a roll number and update the marks.
16
OUTPUT
17
Experiment No. 14 Date:
Write a random number generator that generates random numbers between 1 and 6 (simulates a
dice).
---------------------------------------------------------------------------------------------------------------------------------------
import random
x = "y"
while x == "y":
# Generates a random number between 1 and 6 (including both 1 and 6)
no = random.randint(1,6)
if no == 1:
print("[-----]")
print("[ ]")
print("[ 0 ]")
print("[ ]")
print("[-----]")
if no == 2:
print("[-----]")
print("[ 0 ]")
print("[ ]")
print("[ 0 ]")
print("[-----]")
if no == 3:
print("[-----]")
print("[ ]")
print("[0 0 0]")
print("[ ]")
print("[-----]")
18
if no == 4:
print("[-----]")
print("[0 0]")
print("[ ]")
print("[0 0]")
print("[-----]")
if no == 5:
print("[-----]")
print("[0 0]")
print("[ 0 ]")
print("[0 0]")
print("[-----]")
if no == 6:
print("[-----]")
print("[0 0 0]")
print("[ ]")
print("[0 0 0]")
print("[-----]")
print("\n")
19
Output :
[-----]
[ ]
[0 0 0]
[ ]
[-----]
[-----]
[0]
[ ]
[0]
[-----]
[-----]
[0 0]
[ ]
[0 0]
[-----]
[-----]
[0 0 0]
[ ]
[0 0 0]
[-----]
20
Experiment No. 15 Date:
Create a CSV file by entering user-id and password, read and search the password for given userid.
import csv
with open("user_info.csv", "w") as obj:
fileobj = csv.writer(obj)
if x in "Nn":
break
elif x in "Yy":
continue
for i in fileobj2:
next(fileobj2)
# print(i,given)
if i[0] == given:
print(i[1])
break
21
Output:
n
enter the user id to be searched
cbse
123
22
Experiment No. 16 Date:
---------------------------------------------------------------------------------------------------------------------------------------
def isEmpty(stk):
if stk == []:
return True
else:
return False
def add(stk,item):
stk.append(item)
top = len(stk)-1
def remove(stk):
if(stk==[]):
print("Stack empty;UNderflow")
else:
def display(stk):
if isEmpty(stk):
else :
top = len(stk)-1
print(stk[top],"<-top")
for a in range(top-1,-1,-1):
print(stk[a])
stack=[]
23
top = None
while True:
print("STACK OPERATION:")
print("1.ADD student")
print("2.Display stack")
print("3.Remove student")
print("4.Exit")
if ch==1:
add(stack,item)
input()
elif ch==2:
display(stack)
input()
elif ch==3:
remove(stack)
input()
elif ch==4:
break
else:
input()
24
Output :
STACK OPERATION:
1.ADD student
2.Display stack
3.Remove student
4.Exit
Enter your choice(1-4):1
Enter Roll no to be inserted :11
Enter Student name to be inserted :ATHANG
STACK OPERATION:
1.ADD student
2.Display stack
3.Remove student
4.Exit
Enter your choice(1-4):1
Enter Roll no to be inserted :12
Enter Student name to be inserted :SUJATA
STACK OPERATION:
1.ADD student
2.Display stack
3.Remove student
4.Exit
Enter your choice(1-4):1
Enter Roll no to be inserted :13
Enter Student name to be inserted :MEENA
STACK OPERATION:
1.ADD student
2.Display stack
3.Remove student
4.Exit
Enter your choice(1-4):1
Enter Roll no to be inserted :14
Enter Student name to be inserted :SUSHIL
STACK OPERATION:
1.ADD student
2.Display stack
3.Remove student
4.Exit
Enter your choice(1-4):4
25