NAME= SARTHAK SHARMA
ROLL. NO.= 41
CLASS = XI B
COMPUTER SCIENCE
PROJECT
Q1. Wap to accept a number and find out whether it is
perfect number or not.
ANS. i=1
S=0
Num=int(input(‘enter number:’))
while i<num:
if num%i==0:
s +=i
i = i+1
if s == num:
print(‘it is a perfect no.’)
else:
print(‘it is not a perfect no.’)
Q2.wap that reads a string and then prints a string
that capitalizes every other letter in the string .
ANS. string=input('enter a string:')
length = len (string)
print('original string:',string)
string2=''
for a in range(0,length):
if a%2==0:
string2 +=string[a]
else:
string2 += string[a].upper()
print(string2)
Q3. Write the output of the following python program
code.
Ans.
l='neat'
x=''
l1=[]
count=1
for i in l:
if i in ['a','e','i','o','u']:
x= x + i.swapcase()
else:
if((count%2)!=0):
x = x + str((len(l[:count])))
else:
x = x+i
count = count+1
print(x)
output= lEAt
Q4. Wap to find a)area of circle b)area of rectangle
c)circumference of circle d)area of square
Ans.
while true:
print('1. for area of circle')
print('2. for area of rectangle')
print('3. for circumference of circle')
print('4. for area of square')
ch=int(input('enter your choice (enter 0 to exit):'))
if ch==1:
r=int(input('enter the radius of circle:'))
a=3.14*r*r
print('area of circle: ',a)
elif ch==2:
l=int(input('enter the length: '))
b=int(input('enter the breadth: '))
a=l*b
print('area of rectangle: ',a)
elif ch==3:
r=int(input('enter the radius of the circle: '))
c=2*(int(3.14*r))
print('circumference of circle: ',c)
elif ch==4:
l=int(input('enter the side of square: '))
a=l*l
print('area of square: ',a)
elif ch==0:
break
else:
print("invalid option")
Q5.wap to accept a character from the user and display
whether it is a vowel or consonant.
Ans.
ch = input('enter a character:')
if (ch =='a' or ch =='A'):
print(ch,'is a vowel')
elif (ch =='e' or ch=='E'):
print(ch,'is a vowel')
elif(ch =='i' or ch=='I'):
print(ch,'is a vowel')
elif(ch =='o' or ch=='O'):
print(ch,'is a vowel')
elif(ch =='u' or ch=='U'):
print(ch,'is a vowel')
else:
print(ch,'is a consonant')
Q6.wap to print Fibonacci series up to certain limit.
Ans.
i = int(input('enter the limit:'))
x=0
y=1
z=1
print('fibonacci series\n')
print(x,y,end='')
while(z<=i):
print(z,end='')
x=y
y=z
z =x+y
Q7.wap to accept a number and find out whether it is a
perfect no. or not.
Ans.
i=1
s=0
num=int(input('enter number:'))
while i<num:
if num%i==0:
s+=i
i=i+1
if s==num:
print('it is a perfect no.')
else:
print('it is not a perfect no.')
Q8.wap to accept a decimal no. and display its binary
no.
Ans.
n=int(input('enter no:'))
a=0
m=0
c=1
while n>0:
a=n%2
m=m+(a*c)
c=c*10
n=int(n/2)
print(m)
Q9.what will be output of the following program?
Ans.
mes1=('sky')
mes2=('the')mes3=('limit')
11=len(mes1)
12=len(mes2)
13=len(mes3)
n=11+12+13
for c in range(1,n):
if (c%4==0):
print(mes2)12=12-1
else:
if (c%3==0):
print(mes1)11=11-1
else:
print(mes3)13=13-1
output
>>> [‘limit’]
>>>[‘limit’]
Q10.write a python program to input ‘n’ names and
phones no.s to store it in a dictonary and print ther no.
no. of a particular names.
Ans.
phone=dict()
i=1
n=int(input('enter no of entries:'))
while i<=n:
a=input('enter name:')
b=input('enter phone no.:')
phone[a]=b
i=i+1
1=phone.keys()
x=input('enter the name to be searched:')
for i in 1:
if i==x:
print(x,': phone no is:',phone[i])
break
else:
print(x,'does not exist')
Q11.wap to display unique and duplicate iteams of a
given list into two different lists.
Ans.
l1=[2,7,1,4,9,5,1,4,3]
l2=[]
l3=[]
for i in l1:
if i not in l2:
l2.append(i)
else:
l3.append(i)
print(l2)
print(l3)
output:
[2,7,1,4,9,5,3]
[1,4]
Q12.wap which accept the no. from the user and prints
the frequency of the no. in the list
Lst=[3,21,5,6,3,8,21,6]
Ans.
lst=[3,21,5,6,,3,8,,21,6]
length=len(lst)
element=int(input('enter element:'))
c=0
for i in range(0,length):
if element==lst[i]:
c=c+1
if c==0:
print(n'no. not found')
else:
print(c)
output:
enter element:21
2
Q13.wap in python to find max. and min. in
python in the list entered by user.
Ans.
lst=[]
num=int(input('how many no.s:'))
for n in range(num):
no.s=int(input('enter no.s:'))
lst.append(no.s)
print('max. element in the list:',max(1st))
print('min. element in the list:',min(1st))
output:
how many no.:5
enter no:1
enter no.:88
enter no.:45
enter no.:27
enter no.:10
min value:1
max value:88
Q14. Predict the output:
List1 = [13, 18, 11, 16, 13, 18, 13]
print(List1.index(18))
print(List1.count(18))
List1.append(List1.count(13))
print(List1)
Answer
Output
1
2
[13, 18, 11, 16, 13, 18, 13, 3]
Q15. Predict the output:
a, b, c = [1,2], [1, 2], [1, 2]
print(a == b)
print (a is b)
Answer
Output
True
False
Q16. Write a program to increment the elements of a
list with a number.
Ans.
lst = eval(input("Enter a list: "))
print("Existing list is:", lst)
n = int(input("Enter a number: "))
for i in range(len(lst)):
lst[i] += n
print("List after increment:", lst)
Output
Enter a list: [1, 2, 3, 4, 5]
Existing list is: [1, 2, 3, 4, 5]
Enter a number: 10
List after increment: [11, 12, 13, 14, 15]
Q17. Write a program to check if a number is present
in the list or not. If the number is present, print the
position of the number. Print an appropriate message
if the number is not present in the list.
Ans.
l = eval(input("Enter list: "))
n = int(input("Enter number to search: "))
if n in l:
print(n, "found at index", l.index(n))
else :
print(n, "not found in list")
Q18. Write a program to input ‘n’ numbers and store it
in tuple.
Answer:
t = tuple ( )
n = input (“Enter any number”)
print (“Enter all numbers one after other”)
for i in range (n) :
a = input (“Enter number”)
t = t+(a, )
print (“Output is”)
print (t)
Q19. Write a program to input any two tuples and
interchange the tupel values.
Answer:
t1 = tuple( )
n = input(“Total number of values m first tuple”) for i in
range (n):
a = input(“Enter elements”)
t2 = t2 + (a, )
print (“First tuple”)
print (t1)
print (“Second tuple”)
print( t2)
t1, t2 = t2, t1
print (“After swapping”)
print (“First tuple”)
print (t1)
print (“Second tuple”)
print (t2)
Q20. What will be the output of following code?
x = ['3', '2', '5']
y = ''
while x:
y = y + x[-1]
x = x[:len(x) - 1]
print(y)
print(x)
print(type(x), type(y))
Answer
Output
523
[]
<class 'list'> <class 'str'>
Q21. Create the following lists using a for loop:
A list consisting of the integers 0 through 49.
Ans.
l = []
for i in range(50):
l.append(i)
print("List with integers from 0 to 49:")
print(l)
Output
List with integers from 0 to 49:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19,
20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37
,38,39,40,41,42,43,44,45,46,47,48,49]
Q22. Write a program that should do the following :
prompt the user for a string
extract all the digits from the string
If there are digits:
o sum the collected digits together
o print out the original string, the digits, the
sum of the digits
If there are no digits:
o print the original string and a message "has no
digits"
Sample
given the input : abc123
prints abc123 has the digits 123 which sum to 6
given the input : abcd
prints abcd has no digits
ans.
str = input("Enter the string: ")
sum = 0
digitStr = ''
for ch in str :
if ch.isdigit() :
digitStr += ch
sum += int(ch)
if not digitStr :
print(str, "has no digits")
else :
print(str, "has the digits", digitStr, "which sum to",
sum)
Output
Enter the string: abc123
abc123 has the digits 123 which sum to 6
Q23. Write a program that should prompt the user to
type some sentence(s) followed by "enter". It should
then print the original sentence(s) and the following
statistics relating to the sentence(s) :
Number of words
Number of characters (including white-space and
punctuation)
Percentage of characters that are alphanumeric
Ans.
str = input("Enter a few sentences: ")
length = len(str)
spaceCount = 0
alnumCount = 0
for ch in str :
if ch.isspace() :
spaceCount += 1
elif ch.isalnum() :
alnumCount += 1
alnumPercent = alnumCount / length * 100
print("Original Sentences:")
print(str)
print("Number of words =", (spaceCount + 1))
print("Number of characters =", (length + 1))
print("Alphanumeric Percentage =", alnumPercent)
Q24. Write a Python program as per specifications given
below:
Repeatedly prompt for a sentence (string) or for 'q' to
quit.
Upon input of a sentence s, print the string produced
from s by converting each lower case letter to upper
case and each upper case letter to lower case.
All other characters are left unchanged
Ans.
while True :
str = input("Please enter a sentence, or 'q' to quit : ")
newStr = ""
if str.lower() == "q" :
break
for ch in str :
if ch.islower() :
newStr += ch.upper()
elif ch.isupper() :
newStr += ch.lower()
else :
newStr += ch
print(newStr)
Q25. Write a program that does the following :
takes two inputs : the first, an integer and the
second, a string
from the input string extract all the digits, in the
order they occurred, from the string.
o if no digits occur, set the extracted digits to 0
add the integer input and the digits extracted from
the string together as integers
print a string of the form :
"integer_input + string_digits = sum"
ans.
num = int(input("Enter an integer: "))
str = input("Enter the string: ")
digitsStr = ''
digitsNum = 0;
for ch in str :
if ch.isdigit() :
digitsStr += ch
if digitsStr :
digitsNum = int(digitsStr)
print(num, "+", digitsNum, "=", (num + digitsNum))