You are on page 1of 18

NIKHIL KUMAR

XIIA
23
COMPUTER SCIENCE
S.NO CODE NUMBER PAGE NO.
.
1 CODE-1 5-8

2 CODE-2 9
3 CODE-3 10
4 CODE-4 11-12
5 CODE-5 13
6 CODE-6 14
7 CODE-7 15
8 CODE-8 16
9 CODE-9 17-18
10 CODE-10 19-20
11 CODE-11 21-22
12 CODE-12 23-24
13 CODE-13 25
14 CODE-14 26
15 CODE-15 27
16 CODE-16 28-29
ACKNOWLEDGEMENT

I would like to thank my teacher to


handing this project to me by
considering my capability to do this
project and also to my parents who
provide me the adequate equipment to
me to complete this project.
#1 Menu driven program to do the following operations in a List entered by the User :
(i) Search an element using Linear search (iii) Minimum element using loop
(ii) Maximum element using loop (iv) Reverse the list using loop
CODE:-

def search_(a,b): a="true"

for i in range(0,len(b)): while a=="true":

if b[i]==a: print("1. search an element")

print(a,"is in the lsit") print("2. maximum element in list")

else: print("3. minimum element in list")

print ("there is not such element") print("4. reverse the list")

def max_(b): print("5. exit the menu")

m=0 t=int(input("enter the choice"))

for i in range(0,len(b)): if t==1:

if b[i]>m: ele=int(input('enter the element you


want to search in the list'))
m=b[i]
search_(ele,lst)
print(m,"is the max value of list")
elif t==2:
def min_(b):
max_(lst)
m=b[0]
elif t==3:
for i in range(1,len(b)):
c= min_(lst)
if m>b[i]:
elif t==4:
m=b[i]
r_l=rev(lst)
print(m,"is the min value of list")
lst=r_l
def rev(b):
print(lst, " now list is bing reversed")
s=list()
elif t==5:
for i in range(len(b)-1,-1,-1):
break
s.append(b[i])
print("tnk you totrying out program")
return s
output 1:-
lst=eval(input("enter the list:"))
enter the list:[12,34,56,78,98,765]
1. search an element 4. reverse the list

2. maximum element in list 5. exit the menu

3. minimum element in list enter the choice3

4. reverse the list 12 is the min value of list

5. exit the menu 1. search an element

enter the choice1 2. maximum element in list

enter the element you want to search in the 3. minimum element in list
list78
4. reverse the list
78 is in the lsit
5. exit the menu
there is not such element
enter the choice4
1. search an element
[765, 98, 78, 56, 34, 12] now list is bing
2. maximum element in list reversed

3. minimum element in list 1. search an element

4. reverse the list 2. maximum element in list

5. exit the menu 3. minimum element in list

enter the choice2 4. reverse the list

765 is the max value of list 5. exit the menu

1. search an element enter the choice5

2. maximum element in list tnk you totrying out program

3. minimum element in list

#2 Program to input a list of numbers and swap elements at the even location with the
elements at the odd location
CODE:-

l= eval(input("enter the list:-"))

s=l.copy()

for i in range(0,len(l),2):

s.insert(i+1,s.pop(i))

print(s)

output 2:-
enter the list:-[1,2,3,4,5,6,7,8]
[2, 1, 4, 3, 6, 5, 8, 7]
#3 Program to create a dictionary with the roll number, name and marks of n students in a
class and display the names of students who have marks above 75.
CODE:-

n=int(input("enter the no. till which you want floyd's triangle:-"))

for i in range(n,0,-1):

for j in range(n,i-1,-1):

print(j,end=" ")

print( )

output 3:-
enter the no. till which you want floyd's triangle:-6
6
65
654
6543
65432
654321
#4 Write a Program to enter the number and print the Floyd’s Triangle up to the row
entered by the
user in decreasing order { A 5 row Floyd’s triangle in decreasing order is as follows:
5
54
543
5432
54321
}CODE:-
n=int(input("enter the no. of student:-"))
D=dict()
s=[]
for i in range(1,n+1):
name=input("name")
mark=float(input("enter mark"))
roll_no=int(input("roll no."))
D[name] = tuple((roll_no,mark))
if mark>75:
s.append(name)
print("The student with there marks are:-")
print(D)
print("name of student scored maor then 75 are:-",s)
OUTPUT 4:
enter the no. of student:-5 enter mark68.5
nameranvir roll no.4
enter mark65 namesurya
roll no.1 enter mark55
nameshruti97 roll no.5
enter mark97 The student with there marks
are:-
roll no.2
{'ranvir': (1, 65.0),
namedeepak 'shruti97': (2, 97.0),
enter mark76 'deepak': (3, 76.0), 'naina':
(4, 68.5), 'surya': (5, 55.0)}
roll no.3
name of student scored maor
namenaina then 75 are:- ['shruti97',
'deepak']
#5 Program to enter a number and find whether it is a perfect
number or not. {Perfect number, a positive integer that is
equal to the sum of its proper divisors. The smallest perfect
number is 6, which is the sum of 1, 2, and 3. Other perfect
numbers are 28, 496, and 8,128.}
CODE:-
n=int(input("enter the no., which you wamn to know is a perfect no. or
not:-"))
s=[]
for i in range(1,n//2+1):
if n%i==0:
s.append(i)
print("factors of ",n," are:-",s)
sum=0
for i in range(len(s)):
sum+=s[i]
if sum==n:
print("with factor:-",s,n,"is the perfecr no.")
else:
print(n,"is not the perfect no.")

OUTPUT 5:
#Firstly

enter the no., which you wamn to know is a perfect no. or not:-6
factors of 6 are:- [1, 2, 3]
with factor:- [1, 2, 3] 6 is the perfecr no.
#Secondly

enter the no., which you wamn to know is a perfect no. or not:-67
factors of 67 are:- [1]
67 is not the perfect no.
#6 Program to check if the entered number is Armstrong or not. {In case of an Armstrong
number of
3 digits, the sum of cubes of each digit is equal to the number itself. For example: 153 =
1*1*1 + 5*5*5 + 3*3*3 // 153 is an Armstrong number}
CODE:-

n=int(input("enter the no.:-"))

print("your enteres no. is :-",n)

p=n

s=0

while p!=0:

i=p%10

s+=i*i*i

p=p//10

if s==n:

print(n,",is the armstrong number")

else:

print(n,",is not a armstrong number")

OUTPUT 6:-
#Firstly
enter the no.:-153
your enteres no. is :- 153
153 ,is the armstrong number

#Secondly:
enter the no.:-345
your enteres no. is :- 345
345 ,is not a armstrong number
#7
Write a program to print the sum of the following series:
Sum=x + x 2 / 3! – x 3 / 5! + x 4 / 7! – x 5 / 9! + ----------
Code-
x = int(input("Enter the value of x: "))
n = int(input("Enter the value of n: "))

sum = 0

for i in range(1, n + 1) :
term = x ** i / i
sum += term

print("Sum =", sum)

output-
Enter the value of x: 2
Enter the value of n: 5
Sum = 17.066666666666666
#8 Create and Read a text file line by line and display each
word separated by a #.
CODE: -
l=int(input("enter no. of line you wanted to printed "))
fo=open(r"C:\Users\piyush\OneDrive\Desktop\python
files\#speratedp.txt","w")
for i in range l:
l1=input("enter line :")
fo.write(l1+\n)
fo.close()
myfile=open(r"C:\Users\piyush\OneDrive\Desktop\python
files\#speratedp.txt","r")
line=''
while line:
line= myfile.readline()
for word in line.split():
print(word,end="#")
print()
myfile.close()

OUTPUT 8:
enter no. of line you wanted to printed 2
enter line :hello my name is piyush
enter line :i am a PCM student
hello#my#name#is#piyush#
i#am#a#PCM#student.#
#9 Create and Read a text file and display the number of vowels/ consonants/ uppercase/
lowercase characters in the file.
CODE: -

f=open(r"C:\Users\piyush\OneDrive\Desktop\ elif i in
python files\case.txt","w") ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','
v','w','x','y','z','B','C','D','F','G','H','J','K','L','M','
a="y" N','P','Q','R','S','T','V','W','X','Y','Z']:
while a=="y": consonent+=1
line=input("enter the line :") else:
f.write(line+"\n") continue
a=input("want to write more line (y\n):") print("uppercase=",uc,"\nlowercase=",lc,"\nv
f.close() owel=",vowel,"\nconsonent",consonent)

f=open(r"C:\Users\piyush\OneDrive\Desktop\ f.close()
python files\case.txt","r")
OUTPUT 9:
consonent=vowel=uc=lc=0
line=' ' INITIALY
while line: enter the line :Hellow, my naem is PIYUSH
TIWARI
line=f.readline()
want to write more line (y\n):y
for i in line:
enter the line :i am a twleth student.
if i>=a and i<=z:
want to write more line (y\n):y
lc+=1
enter the line :I want to become a scoring
elif i>=A and i<=Z:
student
uc+=1
want to write more line (y\n):n
else:
continue
FINALY
for i in line: uppercase= 14

if i in ["a",'e','i','o','u','A','I','O','U','E']: lowercase= 57

vowel+=1 vowel= 27

consonent 44
#10 Create a text file and Read it. Create a dictionary having words present in the file as
keys and the frequency of words as values.
CODE: -

with open(r"C:\Users\piyush\OneDrive\Desktop\python files\#word frequancy.txt","w") as f:

a="y"

while a=="y":

l1=input("enter the line:")

f.write(l1+"\n")

a=input("want to write more lines(n\y):")

f=open(r"C:\Users\piyush\OneDrive\Desktop\python files\#word frequancy.txt","r")

d={}

line=" "

while line:

line=f.read()

for word in line.split():

if word not in d:

d[word]=line.split().count(word)

print("The words with there frequancy are:-\n",d)

OUTPUT 10:
enter the line:there are many things one can do in his or her life.
want to write more lines(n\y):y
enter the line:rather than only adopting education , one can chase sports in their life.
want to write more lines(n\y):y
enter the line:but being educated is a primaty thing in life to have an good life , to keep your
feet along with this changiing and compatable world.
want to write more lines(n\y):n
The words with there frequancy are:-
{'there': 1, 'are': 1, 'many': 1, 'things': 1, 'one': 2, 'can': 2, 'do': 1, 'in': 3, 'his': 1, 'or': 1, 'her':
1, 'life.': 2, 'rather': 1, 'than': 1, 'only': 1, 'adopting': 1, 'education': 1, ',': 2, 'chase': 1, 'sports':
1, 'their': 1, 'but': 1, 'being': 1, 'educated': 1, 'is': 1, 'a': 1, 'primaty': 1, 'thing': 1, 'life': 2, 'to':
2, 'have': 1, 'an': 1, 'good': 1, 'keep': 1, 'your': 1, 'feet': 1, 'along': 1, 'with': 1, 'this': 1,
'changiing': 1, 'and': 1, 'compatable': 1, 'world.': 1}
#11 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.
CODE: -

import pickle d1=d

n=int(input("Enter the no. of records:")) found="true"

d={} except:

fh=open("nameollno.dat","wb") if found=="true":

for i in range (n): print(d1, "search successful")

name=input("enter the name:") else:

roll=int(input("enter the roll no.")) print("Name with roll no. ",roll,"not


found")
d["Roll no."]=roll
f.close()
d["Name"]=name

pickle.dump(d,fh) output 11:


fh.flush()
Enter the no. of records:2
fh.close()
enter the name:shub
f=open("nameollno.dat","rb")
enter the roll no.1
roll=int(input("Enter the roll no. to be
searched")) enter the name:shruti
found="false"
enter the roll no.2
d={}
Enter the roll no. to be searched2
try:

while True:
{'Roll no.': 1, 'Name': 'shub'}

d=pickle.load(f) {'Roll no.': 2, 'Name': 'shruti'}


print(d) {'Roll no.': 2, 'Name': 'shruti'} search
if d["Roll no."]==roll: successful
#12 Create a binary file with roll number, name and marks. Input a roll number and
update
the marks.
CODE: -

n=int(input("Enter how many student you fh.seek(pos)


have to write:-"))
print(d)
import pickle
pickle.dump(d,fh)
d={}
found=2
with
except:
open(r"C:\Users\piyush\OneDrive\Desktop\p
ython files\#rollno update.dat","wb+") as f1: if found==1:
for i in range(n): print("not updated")
d["name"]=input("enter the name_") fh.close()
d["roll no."]=int(input("enter the roll else:
no.-"))
print(" done")
d["marks"]=input("enter the marks")
fh.close()
pickle.dump(d,f1)
output 12:
fh=open(r"C:\Users\piyush\OneDrive\Desktop
\python files\#rollno update.dat","rb+") Enter how many student you have to write:-2
roll=int(input("enter the roll no. of which you enter the name_pi
want to change marks"))
enter the roll no.-1
d={}
enter the marks21
found=1
enter the name_su
try:
enter the roll no.-25
while True:
enter the marks98
pos=fh.tell()
enter the roll no. of which you want to change
d=pickle.load(fh) marks25
if d["roll no."]==roll: {'name': 'su', 'roll no.': 25, 'marks': '98'}
print(d) Enter new marks-34
d["mark"]=input("Enter new marks-") {'name': 'su', 'roll no.': 25, 'marks': '98', 'mark':
'34'}
#13 Remove all the lines that contain the character a; in a file and write it to another file .
nfile = file('./oldfile.txt')

newopen = open('./newfile.txt', 'w')

for line in infile :

if 'a' in line:

line = line.replace('.' , '')

else:

newopen.write(line)

file.close()

OUTPUT-
#14 Write a random number generator that generates random numbers between 1 and 6
(simulates a dice).

CODE: -

import random

a="y"

while a=="y":

print("randam no. between 1 and 6 is :\n",random.randrange(1,7))

a=input("want to print more no.,(n\y)")

print ("THANKS")

Output 14:-
randam no. between 1 and 6 is :
2
want to print more no.,(n\y)y
randam no. between 1 and 6 is :
6
want to print more no.,(n\y)n
THANKS
#15 Create a CSV file by entering user-id and password, read and search the password for
given userid.
CODE: -
import csv
fh=open(r"C:\Users\piyush\OneDrive\Desktop\python
files\user_id.csv","w",newline='')
fo=csv.writer(fh,delimiter=',')
fo.writerow(["user-ID","password"])
a="y"
while a=="y":
ui=input("Enter the USER-ID:-")
pass_=input("Enter the password:-")
data=[ui,pass_]
fo.writerow(data)
a=input("Due you wanted to enter more?(n\y):-")
fh.flush()
fh.close()
with open(r"C:\Users\piyush\OneDrive\Desktop\python
files\user_id.csv","r")as f:
fo=csv.reader(f)
for i in fo:
print(i)
output 15:-
Enter the USER-ID:-harsh@132
Enter the password:-har321
Due you wanted to enter more?(n\y):-y
Enter the USER-ID:-rahul@128
Enter the password:-rah772
Due you wanted to enter more?(n\y):-n
['user-ID', 'password']
['harsh@132', 'har321']
['rahul@128', 'rah772']
#16 Write a binary file of employees with Employee Number, Employee Name and Salary.
Read
the file and display only those employees data whose salary is greater the 75000.
CODE: -

import pickle else:

fh=open(r"C:\Users\piyush\OneDrive\Desktop print("not done")


\python files\#empoly.dat","wb")
f.flush()
d={}
f.close()
a="y"
output 16:-
while a=="y":
Enter the name:-shruti
d["NAME"]=input("Enter the name:-")
Enter the empolyee number:-101
d["E.NUMBER"]=int(input("Enter the
Enter the salary:-85000
empolyee number:-"))
want to write about more empolyee, (y\n):-y
d["SALARY"]=int(input("Enter the salary:-"))
Enter the name:-piyush
pickle.dump(d,fh)
Enter the empolyee number:-102
a=input("want to write about more
empolyee, (y\n):-") Enter the salary:-150000
fh.close() want to write about more empolyee, (y\n):-y
f=open(r"C:\Users\piyush\OneDrive\Desktop\ Enter the name:-ritash
python files\#empoly.dat","rb+")
Enter the empolyee number:-103
d={}
Enter the salary:-65000
found=1
want to write about more empolyee, (y\n):-y
try:
Enter the name:-sunakshi
while True:
Enter the empolyee number:-104
d=pickle.load(f)
Enter the salary:-72000
if d["SALARY"]>75000:
want to write about more empolyee, (y\n):-n
print(d)
{'NAME': 'shruti', 'E.NUMBER': 101, 'SALARY':
found=2 85000}
except: {'NAME': 'piyush', 'E.NUMBER': 102, 'SALARY':
150000}
if found==2:
done
print("done")

You might also like