You are on page 1of 27

Computer Science

Practical File
I use arch btw

Name : Aryaman Singh


Class : XI-C
Roll Number : 13
Q1 : WAP to enter a string and check
whether the string is palindrome or not.
Code :
inp = input("Enter string: ")
c = 0
listInp = list(inp)
lenListInp = len(listInp)
ListInp_ = []
for i in range(1, lenListInp+1):
if listInp[-i]==listInp[i-1]:
c += 1
if c == lenListInp:
print("String is a palindrome")
else:
print("String is not a palindrome")
Output:
Enter string: racecar
String is a palindrome
Enter string: integrand
String is not a palindrome
Q2 : WAP to enter a sentence (string) and
display the smallest word
Code :
inp = input("Enter string: ")
listInp = in.split()
maxList = []
c = 0
for j in range(len(listInp)):
if j == 0:
c = len(listInp[j])
maxList.append(listInp[j])
elif j !=0:
if len(listInp[j])<c:
c = len(listInp[j])
maxList = []
maxList.append(listInp[j])

print(maxList)
Output:
Enter string: The cake is a lie
['a']
Enter string: this isn't indicative of the reality
['of']
Q3. WAP to enter a sentence (string) and
display the longest word.
Code :
inp = input("Enter string: ")
listIng = inp.split()
maxList = []
c = 0
for j in range(len(listInp)):
if j == 0:
c = len(listInp[j])
maxList.append(listInp[j])
elif j !=0:
if len(listInp[j])>c:
c = len(listInp[j])
maxList = []
maxList.append(listInp[j])

print(maxList)
Output:
Enter string: Thee cake is a lie
['Thee']
Enter string: this isn't indicative of the reality
['indicative']
Q4. WAP to enter a sentence (string) and
convert it into title case.
Code :
inp = input("Enter String: ")
inp_ = ''
c=0
listUpper = []
listLower = []
for j in 'abcdefghijklmnopqrstuvwxyz':
listLower.append(j)
for j in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
listUpper.append(j)
for i in range(len(inp)):
if i == 0:
for j in range(26):
if inp[i] == listLower[j]:
inp_ += listUpper[j]
elif inp[i] == ' ':
c=1
elif c == 1:
for j in range(26):
if inp[i] == listLower[j]:
inp_ += ' ' + listUpper[j]
c = 0
else:
inp_+= inp[i]
print(inp_)
Output:
Enter String: life could be a dream
Life Could Be A Dream
Enter String: python's are snakes
Python's Are Snakes
Q5. WAP to enter n integer elements in a
Tuple and check whether all the elements
are in ascending order or not.
Code :
num = int(input("Enter number of elements : "))
c = 0
T = ()
for i in range(num):
inp = int(input("Enter element"))
T = T + (inp,)
ele = T[0]
for i in T:
if i >= ele:
c +=1
ele = i
if c == len(T):
print("Elements are in ascending order")
else:
print("Elements are not in ascending order")
Output:
Enter number of elements : 5
Enter element1
Enter element2
Enter element3
Enter element4
Enter element5
Elements are in ascending order
Enter number of elements : 5
Enter element1
Enter element2
Enter element2
Enter element1
Enter element4
Elements are not in ascending order
Q6. WAP to enter n integer elements in a
Tuple and display the second highest
element.
Code :
num = int(input("Enter number of elements : "))
t = ()
for i in range(num):
inp = int(input("Enter Element : "))
t = t + (inp,)
l = list(t)
ele = l[0]
for i in l:
if i > ele:
ele = i
l.remove(ele)
ele = l[0]
for i in l:
if i > ele:
ele = i
print(ele)
Output :
Enter number of elements : 5
Enter Element : 1
Enter Element : 2
Enter Element : 3
Enter Element : 4
Enter Element : 7
4
Enter number of elements : 5
Enter Element : 3248
Enter Element : 2348234
Enter Element : 238974837
Enter Element : 409348
Enter Element : 9439409
9439409
Q7. WAP to create a nested tuple of n
elements where each element of the nested
tuple contains RN, NAME AND MARKS.
Code :
num = int(input("Enter Number of Elements: "))
t = ()
for i in range(num):
rn = input("Enter registration number : ")
name = input("Enter name : ")
marks = input("Enter marks : ")
t = t + ((rn, name, marks),)
print(t)
Output :
Enter Number of Elements: 2
Enter registration number : 54
Enter name : name
Enter marks : 67
Enter registration number : 23
Enter name : name2
Enter marks : 59
(('54', 'name', '67'), ('23', 'name2', '59'))
Q8. WAP to enter a key and display the
corresponding value stored in a Dictionary,
if key does not exist then write appropriate
message "key does not exist”.
Code:
d = {}
c = 0
num = int(input("Enter number of elements in the dictionary: "))
for i in range(num):
key = input("Enter key: ")
val = input("Enter value: ")
d[key]=val
check = input("Enter the key you want the value for: ")
for j in d:
if j == check:
print("The key and the corresponding value are: ",j, "
",d[j])
c = 1
break
if c == 0:
print("key does not exist")
Output:
Enter number of elements in the dictionary: 2
Enter key: aryaman
Enter value: s
Enter key: s
Enter value: e
Enter the key you want the value for: i
key does not exist
Enter number of elements in the dictionary: 1
Enter key: aryaman
Enter value: singh
Enter the key you want the value for: aryaman
The key and the corresponding value are: aryaman singh
Q9. WAP to enter elements (name: marks)
in a Dictionary and update the value of
particular key entered by user.
Code:
d = {}
num = int(input("Enter number of elements: "))
for i in range(num):
key = input("Enter name: ")
value = input("Enter marks: ")
d[key]=value
key_ = input("Enter key of value you want to change: ")
value_ = input("Enter new value: ")
d[key_]= value_
print(d)
Output:
Enter number of elements: 2
Enter name: aryaman
Enter marks: 5
Enter name: arjun
Enter marks: 25
Enter key value you want to change: aryaman
Enter new value: 6708
{'aryaman': '6708', 'arjun': '25'}
Q10. WAP to enter elements (name: marks)
in a Dictionary and count the students
whose marks is >90.
Code:
d = {}
c = 0
num = int(input("Enter number of elements: "))
for i in range(num):
key = input("Enter name: ")
value = int(input("Enter marks: "))
d[key]=value
for ele in d:
if d[ele]>90:
c +=1
print("The number of students with marks above 90 are: ", c)
Output:
Enter number of elements: 2
Enter name: aryaman
Enter marks: 95
Enter name: arjun
Enter marks: 67
The number of students with marks above 90 are: 1
Q11. WAP to enter elements (name: marks
out of 500) in a dictionary and display the
students whose name starts with A.
Code:
d = {}
c = 0
num = int(input("Enter number of elements: "))
for i in range(num):
key = input("Enter name: ")
value = int(input("Enter marks: "))
d[key]=value
for ele in d:
if ele[0] == 'A':
print(ele, d[ele])

Output:
Enter number of elements: 3
Enter name: Aryaman
Enter marks: 450
Enter name: Jaspreet
Enter marks: 430
Enter name: Arjun
Enter marks: 380
Aryaman 450
Arjun 380
Q12. WAP to enter n elements (name:
marks) in a dictionary and delete the
element whose key is entered by user.
Code:
d = {}
c = 0
num = int(input("Enter number of elements: "))
for i in range(num):
key = input("Enter name: ")
value = int(input("Enter marks: "))
d[key]=value
key_ = input("Enter key value you want to remove: ")
d.pop(key_)
print(d)
Output:
Enter number of elements: 2
Enter name: aryaman
Enter marks: 97
Enter name: justin
Enter marks: 57
Enter key value you want to remove: aryaman
{'justin': 57}
Q13. WAP to enter n elements (name:
marks out of 500) in a Dictionary and delete
all the elements whose percentage is <90.
Code:
d = {}
c = 0
num = int(input("Enter number of elements: "))
for i in range(num):
key = input("Enter name: ")
value = int(input("Enter marks: "))
d[key]=value
d_ = d.copy()
for key in d:
if d[key]<450:
d_.pop(key)
print(d_)
Output:
Enter number of elements: 2
Enter name: aryaman
Enter marks: 459
Enter name: jaspreet
Enter marks: 430
{'aryaman': 459}
Q14. WAP to enter n elements (name:
marks out of 500) in a dictionary and delete
all the elements whose percentage is <90,
also store all the deleted elements in
another dictionary and display them.
Code:
d = {}
d__ = {}
c = 0
num = int(input("Enter number of elements: "))
for i in range(num):
key = input("Enter name: ")
value = int(input("Enter marks: "))
d[key]=value
d_ = d.copy()
for key in d:
if d[key]<450:
d_.pop(key)
d__[key]=d[key]
print(d_)
print(d__)
Output:
Enter number of elements: 3
Enter name: aryaman
Enter marks: 437
Enter name: jaspreet
Enter marks: 490
Enter name: arjun
Enter marks: 480
{'jaspreet': 490, 'arjun': 480}
{'aryaman': 437}
Q15. WAP to enter a number and display its
factorial.
Code:
num = int(input("Enter the number: "))
c = 1
for i in range(1, num+1):
c *= i
print(c, "is the factorial of the number")
Output:
Enter the number: 100
933262154439441526816992388562667004907159682643816214685929638952
175999932299156089414639761565182862536979208272237582511852109168
64000000000000000000000000 is the factorial of the number
Q16. WAP to enter a number and check
whether the number is Prime or not.
Code:
num = int(input("Enter the number: "))
c = 0
for i in range(2, num//2+1):
div = num/i
if num//i == div:
print("number is not a prime")
c = 1
break
if c == 0:
print("number is a prime")
Output:
Enter the number: 9
number is not a prime
Enter the number: 37
number is a prime
Q17. WAP to enter a number and check
whether the number is Perfect or not.
Code:
num = int(input("Enter the number: "))
c = 0
for i in range(1, num//2+1):
if num%i==0:
c+=i
if c == num:
print(num, "is a perfect number")
else:
print(num, "is not a perfect number")
Output:
Enter the number: 67
67 is not a perfect number
Enter the number: 6
6 is a perfect number
Q18. WAP to enter a number and reverse
the number.
Code:
num = input("Enter the number: ")
c = 0
k = 0
for i in num:
c+= int(i)*10**k
k+=1
print(c)
Output:
Enter the number: 1234
4321
Q19. WAP to enter a number and check
whether the number is Palindrome or not.
Code:
num = input("Enter the number: ")
c = 0
k = 0
for i in num:
c+= int(i)*10**k
k+=1
if c==int(num):
print("The number is a palindrome")
else:
print("The number is not a palindrome(or is it?), no...")
Output:
Enter the number: 1221
The number is a palindrome
Enter the number: 1234
The number is not a palindrome(or is it?), no…
Q20. WAP to enter a number and check
whether the number is Armstrong number
or not.
Code:
num = input("Enter the number: ")
c = 0
lenNum = len(num)
for i in num:
c+= (int(i))**lenNum
if c == int(num):
print("The number is an armstrong number")
else:
print("Number is not an armstrong number")
Output:
Enter the number: 153
The number is an armstrong number
Enter the number: 150
Number is not an armstrong number
Q21. WAP to enter N elements of integer
type in a list and rearrange the elements as
per the following:
if input list L1 contains:
3 7 9 6 10 8 14 5 4 12
Then output list L2 should contain the following:
7 3 6 9 8 10 5 14 12 4
Code:
num = int(input("Enter number of elements: "))
L1 = []
L2 = []
for ele in range(num):
ele = int(input(“Enter element: “))
L1.append(ele)
listLen = len(L1)
for i in range(0, listLen, 2):
L2.append(L1[i+1])
L2.append(L1[i])
print(L1)
print(L2)
Output:
[3, 7, 6, 9, 10, 8, 14, 5, 4, 12]
[7, 3, 9, 6, 8, 10, 5, 14, 12, 4]
Q22. WAP to enter N elements of integer
type in a list and rearrange the elements as
per the following:
if input list L1 contains:
3 7 9 6 10 8 14 5 4 12
Then output list L1 should contain the following:
8 14 5 4 12 3 7 9 6 10
Code:
num = int(input("Enter number of elements: "))
L1 = []
L2 = []
for ele in range(num):
ele = int(input(“Enter elements: “))
L1.append(ele)
for i in range(listLen//2,listLen):
L2.append(L1[i])
for i in range(0, listLen//2):
L2.append(L1[i])

print(L1)
print(L2)
Output:
[3, 7, 6, 9, 10, 8, 14, 5, 4, 12]
[8, 14, 5, 4, 12, 3, 7, 6, 9, 10]
Q23. WAP to enter N elements of integer
type in a list and rearrange the elements as
per the following
if input list L1 contains:
3 7 9 6 10 8 14 5 4 12
Then output list L1 should contain the following:
12 4 5 14 8 10 6 9 7 3
Code:
num = int(input("Enter number of elements: "))
L1 = []
L2 = []
for ele in range(num):
ele = int(input(“Enter elements: “))
L1.append(ele)
listLen = len(L1)
for i in range(-1, -listLen-1, -1):
L2.append(L1[i])
print(L1)
print(L2)
Output:
[3, 7, 6, 9, 10, 8, 14, 5, 4, 12]
[12, 4, 5, 14, 8, 10, 9, 6, 7, 3]
Q24. WAP to enter N elements of integer
type in a list and rearrange the elements as
per the following
if input list L1 contains:
3 7 9 6 10 8 14 5 4 12
Then output list L1 should contain the following:
6 14 18 18 30 24 42 10 12 36
Code:
num = int(input("Enter number of elements: "))
L1 = []
L2 = []
for ele in range(num):
ele = int(input(“Enter elements: “))
L1.append(ele)
listLen = len(L1)
for i in range(listLen):
if L1[i]%2==0:
L2.append[L1[i]*3]
else:
L2.append[L1[i]*2]
print(L1)
print(L2)
Output:
[3, 7, 6, 9, 10, 8, 14, 5, 4, 12]
[12, 4, 5, 14, 8, 10, 9, 6, 7, 3]
Q25. WAP to enter N elements of integer
type in a list and rearrange the elements as
per the following
if input list L1 contains:
3 7 9 6 10 8 14 5 4 12
Then output list L2 should contain the following:
3 7 9 5 12 4 14 8 10 6
Code:
num = int(input("Enter number of elements: "))
L1 = []
L2 = []
for ele in range(num):
ele = int(input(“Enter elements: “))
L1.append(ele)
listLen = len(L1)
for i in range(listLen):
if L1[i]%2==1:
L2.append(L1[i])
for i in range(-1, -listLen-1,-1):
if L1[i]%2==0:
L2.append(L1[i])
print(L1)
print(L2)
Output:
[3, 7, 6, 9, 10, 8, 14, 5, 4, 12]
[3, 7, 9, 5, 12, 4, 14, 8, 10, 6]

You might also like