Professional Documents
Culture Documents
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
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
#2 Program to input a list of numbers and swap elements at the even location with the
elements at the odd location
CODE:-
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:-
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:-
p=n
s=0
while p!=0:
i=p%10
s+=i*i*i
p=p//10
if s==n:
else:
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
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: -
a="y"
while a=="y":
f.write(l1+"\n")
d={}
line=" "
while line:
line=f.read()
if word not in d:
d[word]=line.split().count(word)
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: -
d={} except:
fh=open("nameollno.dat","wb") if found=="true":
while True:
{'Roll no.': 1, 'Name': 'shub'}
if 'a' in line:
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 ("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: -