0% found this document useful (0 votes)
37 views45 pages

Final Practical

This document is a practical file for the Computer Science course at Delhi Public School, Roorkee for the session 2023-24. It contains a series of programming exercises, including factorial calculation, prime number checking, list operations, file handling, and database connectivity. Each practical task includes code snippets and descriptions of the intended functionality.

Uploaded by

gargojas31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views45 pages

Final Practical

This document is a practical file for the Computer Science course at Delhi Public School, Roorkee for the session 2023-24. It contains a series of programming exercises, including factorial calculation, prime number checking, list operations, file handling, and database connectivity. Each practical task includes code snippets and descriptions of the intended functionality.

Uploaded by

gargojas31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Roll No: _________

DELHI PUBLIC SCHOOL,


ROORKEE
Session: 2023-24

PRACTICAL FILE
FOR
THE FULLFILLMENT OF SSCE 2023-24 EXAMINATION

(AS A PART OF THE COMPUTER SCIENCE COURSE (083))

Under the guidance of : Submitted By :


Mr. Amit Kumar Gupta Siddhant Mittal
PGT(Computer Science) XII-B
Practical -1

Write a program to input any number and calculate it's


factorial.
num = int (input ("enter a number"))
fact= 1
for i in range(num):
fact = fact*(i+1)
print ("Factorial of ",num,"=",fact)
Practical -2
Write a program to input number and check if it is prime
or not
num =int(input("enter a number"))
a=False
if num == 1:

print(num,"is not a prime number")


elif num> 1:

for i in range (2, num):


if (num % i) == 0:
a=true
break
if a:
print (num, " is not a prime number")
else:
print (num," is a prime number")
Practical 3

Program to find the sum of elements of list recursively.

list1 = [11, 5, 17, 18, 23]


# creating sum_list function
def sumOfList(list, size):
if (size == 0):

return 0
else:
return list[size - 1] + sumOfList(list, size - 1)
total = sumOfList(list1, len(list1))
print("Sum of all elements in given list: ", total)
Practical -4
Write a program to calculate the nth term of Fabonacci
series
i=int(input("enter number "))
x=0
y=1
z=1
print("Fabonacci series")
print(x,y,end="")
while(z <= i):
print(z)

x=y
y=z
z=X+y
Practical 5

Program to Sort the list in descending order of marks


using selection sort algorithm.
def Selection(arr,n):
for i in range(n):
min_index = i
min_str = arr[i]
for j in range(i+1,n):
# If min_str is greater than arr[j]
if min_str>arr[j]
min_str = arr[j]
min_index = j
if min_index != i:
temp = arr[i]
arr[i] = arr[min_index]
arr[min_index] = temp
return arr
arr = ["GeeksforGeeks", "Practice.GeeksforGeeks", "GeeksQuiz"]
print("Given array is")
for i in range(len(arr)):
print(i,":",arr[i])
print("Sorted array is")
for i in range(len(Selection(arr,len(arr)))):
print(i,":",Selection(arr,len(arr))[i])
Practical 6
Program to Search for the number in sorted list using
binary search.

def binarySearch(arr, l, r, x):


while l <= r:
mid = l + (r - l) // 2
# Check if x is present at mid
f arr[mid] == x:
return mid
# If x is greater, ignore left half
elif arr[mid] < x:
l = mid + 1
else:
r = mid - 1
return -1
if __name__ == '__main__':
arr = [2, 3, 4, 10, 40]
x = 10
result = binarySearch(arr, 0, len(arr)-1, x)
if result != -1:
print("Element is present at index", result)
else:
print("Element is not present in array")
Practical -7
Write a program to search any word in a given string
a=input("enter a string")
b=input("string to be searched ")
result =a.find(b)
print(result)
Practical-8
Write a program to input a number between 1-6 and use
random module and check if both number match
a=int(input("enter a number between 1-6"))
import random
b=print(random.randrange(0,6))
if a == b:
print(a,"number matched")
else :
print("number not matched")
Practical -9
Write a program using function to input a character and to
print whether a given character Is an alphabet, digit or
any other character
def check():
char=input("enter your character")
if(char.isalpha()):
print("The given character" ,char,"is an alphabet")
elif(char.isdigit()):
print("The given character ",char,"is a digit")
else:
print("the given character ",char,"is a specialcharacter")
check()
Practical -10
Write a program which adds any random even numbers in a
list that falls between the highest and lowest number
import random
l=[]
b=int(input("enter highest value "))
a=int(input("enter lowest value "))
d=random.randint(a,b)
for i in range(6):
d=random.randint(a,b)
print(d)
if d%2 == 0:
print("even")
l.append(d)
print(l)
Practical 11
Program to read a file named "article.txt". Count & print
the following function listed below
article_file = open("article.txt", "r+")
text = article_file.read()
count = 0
space = 0
upper = 0
lower = 0
digits = 0
special_char = 0
alphabets = 0
for i in text:
if i != "":
count += 1
elif i.isalpha():
alphabets += 1
if i.isupper():
upper += 1
if i.islower():
lower += 1
elif i.isdigit():
digits += 1
elif i.isspace():
space += 1
else: special_char += 1
print("Total Characters: ",count)
print("Spaces: ",count)
print("Alphabets: ",alphabets)
print(" Uppercase Characters: ",upper)
print("Lowercase Characters: ",lower)
print("Digits: ",digits)
print("Special Characters: ",special_char)
article_file.close()
Practical 12
Program to read and display a file content line by line with

each word separated by a "#".

file = open("articles.txt", "r")

data = file.readlines()

for i in data:

print(i.replace(' ',"#"))
Practical 13
Program to read the content of a file and display the total
no.Of consonants,vowels,lowercase & uppercase characters.
def count():
f=open("article.txt","r")
cont=f.read()
print(cnt)
a=0
const=0
l=0
u=0
for ch in cont:
if (ch.islower()):
l+=1
elif(ch.isupper()):
u+=1
ch=ch.lower()
if( ch in ['a','e','i','o','u']):
v+=1
elif (ch in['b','c','d','f','g',
'h','j','k','l','m',
'n','p','q','r','s',
't','v','w','x','y','z']):
Cons+=1
f.close()
print("Vowels are : ",v)
print("consonants are : ",const)
print("Lower case letters are : ",l)
print("Upper case letters are : ",u)
count()
Practical 14
Program to create a binary file to store Roll No. & Name,
search any roll No. And display the name
import pickle
while True:
rollno = int(input("Enter your rollno: "))
name = input("Enter your name: ")
d = [rollno, name]
f = open("Student.dat", "wb")
pickle.dump(d, f)
choice = input("enter more records: y/n")
if choice== "N":
break
f.close()
f = open("Student.dat", "rb")
rno = int(input("Enter the roll no to search: "))
pt = 0
try:
while True:
r = pickle.load(f)
if rno == r[0]:
print (rollno, name)
pt = 1
except:
if pt == 0:
print("Record does not exist")
f.close( )
Practical 15
Program to create a binary file to store Roll no, Name,
Marks and update marks of entered Roll No.
import pickle
record=[]
while True:
roll=int(input("Enter roll no: "))
name=input("Enter name of student: ")
marks=int(input("Enter marks of student: "))
data=[roll,name,marks]
record.append(data)
choice=input("To enter more press y/n:")
if choice =='N':
break
f=open("student",'wb')
pickle.dump(record,f)
f.close()
f=open("student",'rb')
import pickle
f=open("student",'rb+')
stud=pickle.load(f)
found=0
rollno=int(input("Enter roll no to be searched:"))
for i in stud:
rno=i[0]
if rno==rollno:
print("Current name is:", i[1])
i[2]= input("New marks:")
found = 1
break
if found == 1:
f.seek(0)
pickle.dump(stud, f)
print("Marks Updated!!!")
f.close()
Practicle 16
Program to read the content of a file line by line and write
it to another file except for the lines containing "a" or "A"
def simpleA():
f=open("article.txt","r")
g=open("article.txt","w")
while True:
line=f.readline()
if line==" ":
break
if "a" not in line:
f1.write(line)
f.close()
g.close()
simpleA()
Practical 17
Program to create CSV file and store Empno, Name,
Salary and search any Empno and display name, salary.
import csv
a=[]
b = int(input("enter no. of element"))
for i in range(b):
emp=int(input("enter empnumber "))
name=input("enter name")
salary=int(input("enter the salary"))
c=[emp,name,salary]
a.append(c)
l=['empnumber','name','salary']
with open("try.csv","w",newline='') as f:
w=csv.writer(f,delimiter=',')
w.writerow(l)
w.writerows(a)
with open("try.csv","r",newline='') as f:
w=csv.reader(f)
k=input("enter empnumber")
for i in w:
global m
m=0
if k == i[0]:
m=1
print("name",i[1])
print("salary",i[2])
break
if m==0:
print("does not exist")
Practical 18
Program to generate a random no. 1-6 simulating a dice

import random
m=input("press enter to roll the dice")
s= random.randint(1,6)
print("dice rolled ",s)
Practical 19
Program to implement a stack in python using list
a=[]
def push(w):
a.append(w)
def pop():
z= a.pop()
print("item removed",z)
while True:
x= int(input('''1 to push an element 2 to pop an element 3 to
exit'''))
if x==1:
b=input("what do you want to enter")
push(b)
elif x==2:
pop()
elif x == 3:
break
print(a)
Practical 20
Program to take 10 sample phishing emails and find the
most common occurring.
a=[]
a=[]
for i in range(10):
c=input("enter the email")
a.append(c)
def d(List):
counter = 0
num = List[0]
for i in List:
frequency = List.count(i)
if frequency> counter:
counter = frequency
num = i
return num
print("most common phishing email is, ",d(a))
Practical 21
Program to connect with database and store records of
employee and display records.
import mysql.connector
a= mysql.connector.connect(host="localhost" ,user="root"
,passwd="1234",database="project")
b=a.cursor()
try:
b.execute("create table emp(id int(30),name varchar(30), salary
int(30))")
except:
pass
c=int(input("enter the no. elements"))
for i in range(c):
id=int(input("enter id "))
name=input("enter name")
salary=int(input("enter salary"))
b.execute("insert into emp
values(%s,%s,%s)",(id,name,salary))
a.commit()
b.execute("select * from emp")
d=b.fetchall()
for i in d:
print(i)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy