Computer Science Vikas Prajapati 12 A (Word)
Computer Science Vikas Prajapati 12 A (Word)
PROGRAMMING
NOIDA PUBLIC
SCHOOL
________
n=num
res=0
while num>0:
rem=num%10
res=rem+res*10
num=num//10
if res==n:
print(n,'is a palindrome')
else:
str=fin.read()
L=str.split()
count_words=0
print('Content of file:-',str)
for i in L:
count_words=count_words+1
return a*a
def rectangle(l,b):
return l*b
def triangle(B,H):
return 1/2*B*H
while 1:
print('*'*43)
print('~~~~~~~~~~~~~~Main menu~~~~~~~~~~~~')
print('*'*30)
if n==1:
area=square(s)
elif n==2:
area=rectangle(l,b)
elif n==3:
area=triangle(B,H)
else:
print('Wrong choice')
chr=input('Y/N:')
if chr=='n' or chr=='N':
break
Q4. Write random number generator that generates
random no. between 1 to 6 (simulates a dice).
Ans.
import random
a=0
b=7
print('A random no. from range is:',end='')
print(random.randrange(a,b))
Q5. Write a program to enter two numbers and
perform arithmetic operations like +,-,/,//,*and%.
Ans.
val1=float(input('enter the first value:'))
if op=='+':
result=val1+val2
elif op=='-':
result=val1-val2
elif op=='*':
result=val1*val2
elif op=='/':
if val2==0:
else:
result=val1/val2
elif op=='//':
result=val1//val2
else:
result=val1%val2
my_str=my_str.casefold()
rev_str=reversed(my_str)
if list(my_str)==list(rev_str):
else:
Pi=3.14
area=Pi*radius*radius
circumference=2*Pi*radius
print('Area of circle=%.2f'%area)
print('Circumference of circle=%.2f'%circumference)
Q8. Fibonacci Series program Using While Loop.
Ans.
Number=int(input('\nPlease enter the range number:'))
i=0
first_value=0
second_value=1
while(i<Number):
if(i<=1):
Next=i
else:
Next=first_value+second_value
first_value=second_value
second_value=Next
print(Next)
i=i+1
Q9. Write a program to perform some functions using
maths module.
Ans.
import math
a=math.ceil(1.03)
b=math.sqrt(81.0)
c=math.fabs(1.0)
d=math.floor(1.03)
e=math.pow(4.0,2.0)
print(a,b,c,d,e)
Q10. Write a program to display vowels present in a
word using stack in python.
Ans.
vowels=['a','e','i','o','u']
stack=[]
if letter in vowels:
stack.append(letter)
print(stack)
f=open(r"C:\Users\subha\Desktop\test2.txt","r")
cont=f.read()
print(cnt)
v=0
cons=0
l_c_l=0
u_c_l=0
for ch in cont:
if (ch.islower()):
l_c_l+=1
elif(ch.isupper()):
u_c_l+=1
ch=ch.lower()
if( ch in ['a','e','i','o','u']):
v+=1
cons+=1
f.close()
#main program
cnt()
Q12. Read a text file line by line and display each
word separated by #.
Ans.
file=open(r"C:\Users\subha\Desktop\test.txt","r")
doc=file.readlines()
for i in doc:
words=i.split()
for a in words:
print(a+"#")
file.close()
Q13. Read a text file by line and display the no. of
vowels/consonants/uppercase/lowercase.
Ans.
def cnt():
f=open(r"C:\Users\subha\Desktop\test2.txt","r")
cont=f.read()
print(cnt)
v=0
cons=0
l_c_l=0
u_c_l=0
for ch in cont:
if (ch.islower()):
l_c_l+=1
elif(ch.isupper()):
u_c_l+=1
ch=ch.lower()
if( ch in ['a','e','i','o','u']):
v+=1
cons+=1
f.close()
print("Vowels are : ",v)
#main program
cnt()
Q14.Creat a binary file with name and roll number.
Search for a given roll number and display the name,
if not found display appropriate message.
Ans.
f=open('s1','w+b') # Create a binary file with name,
for i in range(n):
rollno=input('Enter rollno:')
marks=input('Enter marks:')
bname=bytes(name,encoding='utf-8')
brollno=bytes(rollno,encoding='utf-8')
bmarks=bytes(marks,encoding='utf-8')
f.write(brollno)
f.write(bname)
f.write(bmarks)
f.write(b'\n')
f.seek(0)
data=f.read()
f.seek(0)
bsk=bytes(sk,encoding='utf-8')
l=len(bsk)
loc=data.find(bsk)
if loc<0:
else:
f.seek(loc+l,0)
i=0
while f.read(1).isalpha():
i=i+1
f.seek(-1,1)
bmarksu=bytes(marksu,encoding='utf-8')
f.write(bmarksu)
f.seek(0)
udata=f.read()
print(udata.decode())
Q15. Write a program that adds individual elements
of list2 and list3 into 1. Resultant list should be in
order of list3, list1, list2.
Ans.
l1=[11,34,45,56,67]
l2=[22,68,90,112,134]
l3=[21,34,55,76,88]
print('List1:-',l1)
print('List2:-',l2)
print('List3:-',l3)
l3.extend(l1)
l3.extend(l2)
if char in tuple1:
count=0
for a in tuple1:
if a!=char:
count+=1
else:
break
print(char,'is an index',count,'in',tuple1)
Q17. Write a program that checks for the presence of
a value inside a dictionary and print its keys with
ignore case.
Ans.
info={'Subhash':'Engineer','Mohit':'BTech','Dhoni':'Cricketer','Ambani':'Businessman'}
for a in info:
if(info[a].upper()==inp.upper()):
break
else:
while var:
if choice==1:
ch=input('Enter a character:')
print(ord(ch))
elif choice==2:
print(chr(val))
else:
option=input()
if option=='y' or option=='Y':
var=True
var=False
Q19. Write a program to create binary file to store
Roll No. and Name , Search any Roll No. and display
name if roll no. found otherwise ‘Roll no. not found’.
Ans.
import pickle
student=[]
f=open('student.dat','wb')
ans='y'
while ans.lower()=='y':
name=input('Enter Name:')
student.append([roll,name])
ans=input('Add More?(y)')
pickle.dump(student,f)
f.close()
f=open('student.dat','rb')
student=[]
while True:
try:
student=pickle.load(f)
except EOFError:
break
ans='y'
while ans.lower()=='y':
found=False
for s in student:
if s[0]==r:
print('##Name is:',s[1],'##')
found=True
break
if not found:
ans=input('Search more?(y):')
f.close()
Q20. Write a program to perform read and write
operation with csv file.
Ans.
import csv
mywriter=csv.writer(csvfile,delimiter=',')
ans='y'
while ans.lower()=='y':
mywriter.writerow([eno,name,salary])
ans=input('Add More?')
ans='y'
myreader=csv.reader(csvfile,delimiter=',')
print(myreader)
while ans=='y':
found=False
print(row)
if len(row)!=0:
if int(row[0])==e:
print('============================')
print('Name:',row[1])
print('Salary:',row[2])
found=True
break
if not found:
print('===========================')
print('============================')
ans=input('Search More?(y)')