CSPC212

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 31

COMPUTER SCIENCE

[Date]

HARSHITA CHOPRA
CLASS- XII-B ROLL NO. 24
Q2-Write a python code to define a function to find the factorial of a given
number.
n=int(input("enter a number: "))
def fact(n):
s=1
i=1
while i<=n:
s=s*i
i=i+1
print("the factorial of a given number",n,"is: ",s)
fact(n)
OUTPUT-

Q3- Write a python code to define a function checkprime to check whether the
number is prime or not prime.
n=int(input("enter a number: "))
def checkprime(n):
for i in range(2,n):
if n%i == 0:
print("prime")
break
else:
print("not prime")
checkprime(n)
OUTPUT-

Q1- Write a python code to define a function list change which excepts a list A of
numbers, the function will replace even numbers by 10 and odd number by 5.
def listchange(numbers):
result = []
for num in numbers:
if num % 2 == 0:
result.append('#')
else:
result.append('*')
return result
numbers = input("Enter a list of numbers separated by spaces: ")
list = [int(num) for num in numbers.split()]
changed = listchange(list)
print("Original List:", list)
print("Changed List:", changed)
OUTPUT

Q4-Write a python module to display square root of a number using math


module.
import math
def cal_sqrt():
number=float(input("enter a number"))
sq_root= math.sqrt(number)
print("the square root of",number,"is",sq_root)
cal_sqrt()
OUTPUT
Q5- Write a program to find roots of quadratic equation.
def equation(a,b,c):
d=(b*b-4*a*c)**0.5
return (-b+d)/(2*a),(-b-d)/(2*a)
s,t=equation(2,-2,-2)
print(s,'\t',t)
OUTPUT

Q6- Write a program that fills a list with numbers using randint.
from random import randint
def fill(L,limit,low,high):
for i in range(limit):
L.append(randint(low,high))
minimum=int(input("min"))
maximum=int(input("max"))
n=int(input("enter limit of numbers"))
a=[]
fill(a,n,minimum,maximum)
print(a)
OUTPUT
Q7- Write a program for basic calculations like addition subtraction etc.
def calc(n1,n2):
print("sum is",n1+n2)
print("difference is",n1-n2)
print("product is",n1*n2)
print("quotient is",n1/n2)
a=int(input("enter the no. 1"))
b=int(input("enter the no. 2"))
calc(a,b)
OUTPUT
Q8- Write a code to define a function to generate a table of a given number.
def table(n):
for i in range(1,11):
print(n,"X",i,"=",n*i)
a=int(input("enter the number"))
table(a)
OUTPUT
Q9- Write a code to generate an OTP using random module.
import random
r=' '
for i in range(6):
r+=str(random.randint(0,9))
print("your otp is",r)
OUTPUT

Q10- Write a program to pick 3 lucky winners. The registration numbers of the
ticket are 10 digits long.
import random
for i in range(3):
print(random.randrange(0,1000000000,3))
OUTPUT
Q11- Write a python program to implement all basic operations of stack.
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==1):
print("stack empty")
else:
print("deleted element is: ",s.pop())
elif (choice==3):
l=len(s)
for i in range(l-1,-1,-1):
print(s[i])
else:
print("wrong input")
c=input("Do you want to continue?")
if c=='n':
break
OUTPUT
Q12- Write a program to display unique vowels present in the given word using
stack.
vowels=['a','e','i','o','u']
word=input("enter the word to seaarch for vowels: ")
stack=[]
for letter in word:
if letter in vowels:
if letter not in stack:
stack.append(letter)
print(stack)
print("the number of differnt vowels present in",word,"is",len(stack))
OUTPUT
Q13- Write a program to create a stack called EMPLOYEE, to perform the basic
operations on stack using list.
employee=[]
c="y"
while(c=="y"):
print("1. Push")
print("2. Pop")
print("3. Display")
choice=int(input("enter your choice: "))
if (choice==1):
eid=input("enter employee number: ")
ename=input("enter employee name: ")
emp=(eid,ename)
employee.append(emp)
elif (choice==2):
if (employee==1):
print("stack empty")
else:
eid,ename=employee.pop()
print("deleted element is: ",eid,ename)
elif (choice==3):
i=len(employee)
while i>0:
print(employee[i-1])
i=i-1
else:
print("wrong input")
c=input("Do you want to continue?")
if c=='n':
break
OUTPUT
Q14- Write a program in python to add, delete and display elements from a
queue using list.
a=[]
c="y"
while(c=="y"):
print("1. Insert")
print("2. Delete")
print("3. Display")
choice=int(input("enter your choice: "))
if (choice==1):
b=input("enter new number: ")
b.append(b)
elif (choice==2):
if (employee==1):
print("queue empty")
else:
print("deleted element is: ",a[0])
a.pop(0)
elif (choice==3):
l=len(a)
for i in range(0,1):
print(a[i]
else:
print("wrong input")
c=input("Do you want to continue?")
if c=='n':
break
OUTPUT
Q15- Write a program to perform basic operation of queue on details of books
like adding book name and book id in a queue.
library=[]
c="y"
while(c=="y"):
print("1. Insert")
print("2. Delete")
print("3. Display")
choice=int(input("enter your choice: "))
if (choice==1):
bid=input("enter book number: ")
bname=input("enter name of book: ")
lib=(bid,bname)
library.append(lib)
elif (choice==2):
if (library==1):
print("queue empty")
else:
print("deleted element is: ",library.pop(0))
elif (choice==3):
l=len(library)
for i in range(0,l):
print(library[i])
else:
print("wrong input")
c=input("Do you want to continue?")
if c=='n':
break
OUTPUT
Q16-Write a program to write the content of a binary file.
import pickle
def write():
f= open("myfile.dat",'wb')
l=['ram','shyam','sita','gita']
pickle.dump(l,f)
f.close()
write()
OUTPUT

Q17- Write a program to read the content of binary file.


import pickle
def read():
f=open("myfile.dat",'rb')
l=pickle.load(f)
print(l)
f.close()
read()
OUTPUT

Q18-Write a program to write content in binary file from dictionary


import pickle
def write():
f=open("file.dat",'wb')
dict1={'roll':1,'name':'arnav'}
dict1=pickle.dump(dict1,f)
f.close()
write()
OUTPUT
Q19- Write a program to search the content of binary file.
import pickle
def search():
f=open("myfile.dat",'rb')
n=input("enter name to be searched")
x=pickle.load(f)
k=0
for i in x:
if i == n:
print("record found: ",i)
k=1
break
if k==0:
print("not found")
search()
Q20- Write a program to write details of students in a text file.
n=int(input("enter no of students:"))
f=open("student.txt",'w')
for i in range(n):
roll=int(input("enter roll no"))
name=input("enter name of stusdent")
marks=int(input("enter marks"))
record=str(roll)+","+name+","+str(marks)+"\n"
f.write(record)
f.close()
OUTPUT

Q21- Write a program to add 3 more student details in previous program.


n=int(input("enter no of students:"))
f=open("student.txt",'a')
for i in range(n):
roll=int(input("enter roll no"))
name=input("enter name of stusdent")
marks=int(input("enter marks"))
record=str(roll)+","+name+","+str(marks)+"\n"
f.write(record)
f.close()
OUTPUT

Q22-Write a program to read the content of previous program.


f=open("student.txt",'r')
str="-"
while str:
str=f.readline()
print(str)
f.close()
OUTPUT

Q23-Write a program to display size of a file removing spaces.


f=open("student.txt",'r')
str=" "
size=0
r=0
while str:
str=f.readline()
size=size+len(str)
r=r+len(str.strip())
print(size)
print(r)
f.close()
OUTPUT

Q24-Write a program to perform addition of two numbers in text file.


f=open("test.txt",'w')
value1=int(input("enter no 1"))
value2=int(input("enter no 2"))
s=value1=value2
f.write("\n number1:"+str(value1))
f.write("\n number2:"+str(value2))
f.write("\n sum of numbers:"+str(s))
f.close()
OUTPUT
Q25-Write a program to read the contents line by line of a text file.
f=open("hello user.txt",'r')
lines=f.readlines()
for i in lines
print(i,end='')
f.close
OUTPUT

Q26-Write a program to create a csv file and enter 3 records of students.


import csv
def create():
with open("details.csv",'w',newline='') as f:
x=csv.writer(f)
x.writerow(['roll','name','marks'])
while True:
roll=int(input("enter roll no"))
name=input("enter name")
marks=int(input("enter marks"))
record=[roll,name,marks]
x.writerow(record)
ch=input("do you want to enter more records")
if ch=='n':
break
create()
OUTPUT
Q27-Write a program to create employee.csv and add 5 records.
import csv
def create():
with open("employee.csv",'w',newline='') as f:
x=csv.writer(f)
x.writerow(['e.no','e.name','salary'])
while True:
n=int(input("enter employee no"))
name=input("enter name")
slary=int(input("enter salary"))
record=[n,name,slary]
x.writerow(record)
ch=input("do you want to enter more records")
if ch=='n':
break
create()
OUTPUT
Q28-Write a program to search a record in a csv file of previous question.
import csv
def search():
with open("employee.csv",'r',newline='') as f:
x=csv.reader(f)
next(f)
for i in f:
if i[3]>='10000':
print(i)
search()
OUTPUT
Q29-Write a program to count the number of records in employee.csv file.
import csv
def count():
with open("employee.csv",'r',newline='') as f:
x=csv.reader(f)
next(f)
count=0
for i in x:
count=count+1
print("no of employees :",count)
count()
OUTPUT

Q30-Write a program to update a record in a csv file


import csv
def update():
with open("employee.csv",'a',newline='') as f:
x=csv.reader(f)
v=int(input("enter employee no:"))
m=int(input("enter new salary"))
for i in x:
if i =='v':
salary=='m'
csv.writerow()
print("record is updated")

update() .
OUTPUT

You might also like