[go: up one dir, main page]

0% found this document useful (0 votes)
15 views129 pages

Answer Key (Effective Computer Science-11)

The document contains a structured outline of topics in computer science, including chapters on number systems and flow of control with corresponding unsolved problems. Each chapter includes exercises for converting between different number systems (binary, octal, decimal, hexadecimal) and programming tasks in Python. The document serves as a resource for learning and practicing fundamental concepts in computer science.

Uploaded by

samarta653
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views129 pages

Answer Key (Effective Computer Science-11)

The document contains a structured outline of topics in computer science, including chapters on number systems and flow of control with corresponding unsolved problems. Each chapter includes exercises for converting between different number systems (binary, octal, decimal, hexadecimal) and programming tasks in Python. The document serves as a resource for learning and practicing fundamental concepts in computer science.

Uploaded by

samarta653
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 129

Effective Computer Science 1

Effective Computer Science 2

Table of Contents

Topics and Contents Page No.

Chapter 2 Number System 3

Chapter 6 Flow of Control 5

Chapter 7 Strings 41

Chapter 8 Tuples 51

Chapter 9 Lists 66

Chapter 10 Dictionaries 106

Chapter 12 Python Modules 126

Answers to Objective Type Questions 127

Answers of Assertion And Reasoning Based Questions 129


Effective Computer Science 3

Chapter : 2
Number System

 Unsolved Problems
1) Convert the following decimal number into its equivalent binary number.

(a) 6785 = (1101010000001)2 (f) 237.75 = (11101101.11)2


(b) 4506 = (1000110011010)2 (g) 183.8125 = (10110111.1101)2
(c) 0.21875 = (0.00111)2 (h) 3140.21875 = (110001000100.00111)2
(d) 0.3625 = (0.01011100110011001101)2 (i) 5649.625 = (1011000010001.101)2
(e) 45.3625 = (j) 8782.54 =
(01101.01011100110011001101)2 (10001001001110.10001010001111010111)2

2) Convert the following decimal number into its equivalent octal number.

(a) 82 = (122)8 (f) 7864.21875 = (17270.16)8


(b) 6723 = (15103)8 (g) 67205.125 = (203205.1)8
(c) 87903 = (253537)8 (h) 6627.25 = (14743.2)8
(d) 0.051625 = (0.03233513615237574733)8 (i) 6785.0625 = (15201.04)8
(e) 0.3625 = (0.27146314631463146315)8 (j) 8639.8125 = (20677.64)8

3) Convert the following decimal number into its equivalent hexadecimal.

(a) 31 = (1F)16 (f) 5642.512 = (160A.83126E978D4FDF3B645A)16


(b) 872 = (368)16 (g) 8793.0625 = (2259.1)16
(c) 54097 = (D351)16 (h) 6677.8125 = (1A15.D)16
(d) 0.21875 = (0.38)16 (i) 7638.125 = (1DD6.2)16
(e) 0.00125 = (0.0051EB851EB851EB851F)16 (j) 663.051625 = (297.0D374BC6A7EF9DB22D0E)16

4) Convert the following binary number into its equivalent decimal number.

(a) 110111 = (55)10 (f) 11100101.1101 = (229.8125)10


(b) 1000010001 = (529)10 (g) 10101.10101 = (21.65625)10
(c) 11001100 = (204)10 (h) 11001100.0101 = (204.3125)10
(d) 0.10101 = (0.65625)10 (i) 10001110.00111 = (142.21875)10
(e) 0.1111 = (0.9375)10 (j) 1111001.1101 = (121.8125)10

5) Convert the following octal number into its equivalent decimal number.

(a) 230 = (152)10 (f) 1537.26 = (863.34375)10


(b) 1023 = (531)10 (g) 352.552 = (234.70703125)10
(c) 237 = (159)10 (h) 367.21 = (247.265625)10
(d) 0.25 = (0.328125)10 (i) 563.33 = (371.421875)10
(e) 0.567 = (0.732421875)10 (j) 10101.0101 = (4161.015869140625)10

6) Convert the following hexadecimal number into its equivalent decimal number.

(a) 89 = (137)10 (f) 0.321 = 0.195556640625)10


(b) 906 = (2310)10 (g) ABC.DE = 2748.8671875)10
(c) ABC = (2748)10 (h) 101.101 = 257.062744140625)10
(d) 10101 = (65793)10 (i) 9F2.0B = 2546.04296875)10
(e) 0.21 = (0.12890625)10 (j) 1065.A5C = 4197.6474609375)10

7) Convert the following binary number into its equivalent octal number.
Effective Computer Science 4
(a) 110110111 = (667)8 (f) 1110011110.0010111 = (1636.134)8
(b) 1010111 = (127)8 (g) 100011111011.011101 = (4373.35)8
(c) 110010101 = (625)8 (h) 1001010010.1001001 = (1122.444)8
(d) 0.111001101 = (0.715)8 (i) 111110000.00011 = (760.06)8
(e) 0.1010001101 = (0.5064)8 (j) 100011100011.1110010011 = (4343.7114)8

8) Convert the following binary number into its equivalent hexadecimal number.

(a) 1001101011 = (26B)16 (f) 1110011110.010101011 = (39E.558)16


(b) 101001011 = (14B)16 (g) 1000110111011.01001101 = (11BB.4D)16
(c) 1110100011 = (3A3)16 (h) 1011001010.1001001 = (2CA.92)16
(d) 0.1011001101 = (0.B34)16 (i) 11100110001.10011 = (731.98)16
(e) 0.101011101 = (0.AE8)16 (j) 10001110101.110110101 = (475.DA8)16

9) Convert the following octal number into its hexadecimal decimal number.

(a) 23 = (13)16 (f) 6523.65242 = (D53.D544)16


(b) 653 = (1AB)16 (g) 66543.2231 = (6D63.499)16
(c) 7753 = (FEB)16 (h) 65360.0516 = (6AF0.14E)16
(d) 0.03673 = (0.0F76)16 (i) 627.2477 = (197.53F)16
(e) 0.5412 = (0.B0A)16 (j) 2323.4455 = (4D3.92D)16

10) Convert the following hexadecimal number into its equivalent octal number.

(a) 98A62 = (2305142)8 (f) ABCD.EF = (125715.736)8


(b) 5A7F2 = (1323762)8 (g) 789.ABC = (3611.5274)8
(c) 0.889977 = (0.42114567)8 (h) A6B7C9.D3E4 = (51533711.64762)8
(d) AABB = (125273)8 (i) 763D.F0A = (73075.7412)8
(e) 0.ABCD = (0.527464)8 (j) AB7.FE3 = (5267.7743)8
Effective Computer Science 5

Chapter : 6
Flow of Control

 Unsolved Problems
1) Write a program in Python to read two numbers from a user and display there sum, product, difference, quotient
and remainder.
Ans:
a = int(input('Enter a Number : '))
b = int(input('Enter another Number : '))
c = a + b
d = a - b
e = a * b
f = a / b
g = a % b
print('Sum = ', c)
print('Difference = ', d)
print('Product = ', e)
print('Division = ', f)
print('Remainder = ', g)

2) Write a program in Python to read length and breadth and display area and perimeter of the rectangle.
Ans:
l = int(input('Enter Length : '))
b = int(input('Enter Breadth : '))
a = l * b
p = 2 * ( l + b )
print('Area = ', a)
print('Perimeter = ', p)

3) Write a program in Python to read radius and display area and perimeter of the circle.
Ans:
r = int(input('Enter Radius of the Circle : '))
a = 3.14 * r * r
p = 2 * 3.14 * r
print('Area = ', a)
print('Perimeter = ', p)

4) Write a program in Python to read principal, rate and time and display simple interest.
Ans:
p = int(input('Enter Principle : '))
r = int(input('Enter Rate : '))
t = int(input('Enter Time : '))
i = p * r * t / 100
print('Simple Interest = ', i)

5) Write a program in Python to read five numbers and display there average.
Ans:
a = int(input('Enter 1st Nuumber : '))
b = int(input('Enter 2nd Number : '))
c = int(input('Enter 3rd Number : '))
d = int(input('Enter 4th Number : '))
e = int(input('Enter 5th Number : '))
avg = (a+b+c+d+e)/5
print('Average = ', avg)
Effective Computer Science 6
6) Write a program in Python to read a number n form user and display it n2 , n3 , n4 .
Ans:
n = int(input('Enter a number : '))
print(n,'to the power 2 is', n**2)
print(n,'to the power 3 is', n**3)
print(n,'to the power 4 is', n**4)
print(n,'to the power 5 is', n**5)

7) Write a program in Python that asks a user to enter your height in centimeters and then convert the height to
feet and inches. ( 1 foot = 12 inches, 1 inch = 2.54 cm).
Ans:
n = int(input('Enter height in cm : '))
f = n // 30
n = n % 30
i = n // 2.54
n = n % 2.54
print('Feets = ', f)
print('Inches = ', i)
print('Centimeters = ', n)

8) Write a program in Python to read temperature in Celsius and display it in Fahrenheit. (Tf = 1.8*Tc+32; where Tc
= Temp. Celsius, Tf = Fahrenheit).
Ans:
Tc = int(input('Enter Temperature in Celsius : '))
Tf = 1.8*Tc+32
print('Temperature in Fahrenheit : ', Tf)

9) Write a program in Python to read two numbers and display them after swapping their values.
Ans: a = int(input('Enter a Number : '))
a = int(input('Enter a Number : ')) b = int(input('Enter another Number : '))
b = int(input('Enter another Number : ')) print('Values Before Swapping :')
print('Values Before Swapping :') print('a = ', a , ' b = ', b)
print('a = ', a , ' b = ', b) a = a + b
a , b = b , a b = a - b
print('Values After Swapping :') a = a - b
print('a = ', a , ' b = ', b) print('Values After Swapping :')
print('a = ', a , ' b = ', b)

10) Write a program in Python to read three numbers from a user and display them by separating with @ . For
example 6@2@9.
Ans:
a = int(input('Enter First Number : '))
b = int(input('Enter Second Number : '))
c = int(input('Enter Third Number : '))
print(a,b,c, sep = '@')

11) Write a program in Python to read three sides of a triangle and find area of a triangle.
Ans: import math

a = int(input('Enter a First Side of the Triangle : '))


b = int(input('Enter a Second Side of the Triangle : '))
c = int(input('Enter a Third Side of the Triangle : '))
s = (a+b+c)//2
area = math.sqrt((s * (s-a) * (s-b) * (s-c)))
print('Area of the Triangle is : ', area)
Effective Computer Science 7
12) Write a program in Python to read principal, rate and time and calculate compound interest. ( I = P (1 + r / 100 )
t– P)
Ans:
p = int(input('Enter Principle : '))
r = int(input('Enter Rate : '))
t = int(input('Enter Time : '))
i = ( p * (1 + r / 100) ** t ) - p
print('Simple Interest = ', i)

13) Write a program in Python to read number of days and represent it in the form of years, months, weeks and days.
Ans:
n = int(input('Enter Number of Days : '))
a = n//365
n = n%365
b = n//30
n = n%30
c = n//7
n = n%7
print('years = ', a)
print('months = ', b)
print('weeks = ', c)
print('days = ', n)

14) Write a program in Python to read an amount of money and represent it in the form of 100, 50, 20, 10, 5, 2 and 1
rupee notes.
Ans:
n = int(input('Enter an amount of money : '))
a = n//100
n = n%100
b = n//50
n = n%50
c = n//20
n = n%20
d = n//10
n = n%10
e = n//5
n = n%5
f = n//2
n = n%2
print('100 = ',a)
print('50 = ',b)
print('20 = ',c)
print('10 = ',d)
print('5 = ',e)
print('2 = ',f)
print('1 = ',n)

15) Write a program in Python to read number of seconds from a user and represent it in the form of hours, minutes
and seconds.
Ans:
n = int(input('Enter Number of seconds : '))
a = n//3600
n = n%3600
b = n//60
n = n%60
print('Hours = ', a)
print('Miniuts = ', b)
print('Seconds = ', n)
Effective Computer Science 8
16) Write a program in Python to read a single digit(n) and print a 3 digit number created as n(n+1)(n+2). For example
if the given number is 7, the output should be 789. Assume that the input digit is in range 1 – 7.
Ans:
n = int(input('Enter a Number in the range of 1 to 7 : '))
a = n*100 + (n+1)*10 + (n+2)
print('Resultant Number is : ', a)

17) Write a program in Python to read a float number and display its integer and fractional part separately.
Ans:
import math
a = float(input('Enter a number : '))
i = int(a)
f = a-i
print('Integer Part', i)
print('Decimal Part', f)

18) Write a program in Python to read three numbers in three variables and swap first two variables with the sum of
first and second, second and third number respectively.
Ans:
a = int(input('Enter first number : '))
b = int(input('Enter second number : '))
c = int(input('Enter thrid number : '))
d = a
e = b
m = a + b
n = b + c
print(a, b, m, n)
a , b = m, n
m, n = d, e
print(a, b, m, n)

19) Write a program in Python to read a character and display its ASCII value. ( Hint : use ord( ) function )
Ans:
a = input('Enter a Character : ')
print('Ordinal Value of the character ', a , ' is ', ord(a))

20) Write a program in Python to read an ASCII value and display the character associated with it. (Hint : use chr( )
function )
Ans:
a = int(input('Enter an Ordinal Value : '))
print('Character of the Ordinal Value ', a , ' is ', chr(a))

21) Write a program in Python to read a character and display its next character and previous character.
Ans:
a = input('Enter an Character : ')
print('Next character of ', a , ' is ', chr(ord(a)+1))
print('Previous character of ', a , ' is ', chr(ord(a)-1))
Effective Computer Science 9
Problems of Selection Construct

1) Write a program in Python to read marks from a user and if it is greater than equal to 50 then display “Pass” else
display “Fail”.
Ans:
m = int(input("Enter Marks : "))
if m >= 50:
r = 'Pass'
else:
r = 'Fail'
print('Result : ', r)

2) Write a program in Python to read salary from a user and if it is greater than equal to 10000 then bonus is
calculated as 20% else 30% of the salary.
Ans:
s = int(input("Enter Salary : "))
if s >= 10000:
b = 0.2 * s
else:
b = 0.3 * s
print('Bonus : ', b)

3) Write a program in Python to read two numbers, if first is even and second is odd then display their sum else
display their product.
Ans:
a = int(input('Enter a number : '))
b = int(input('Enter another number : '))
if a%2 == 0 and b%2 != 0:
print(a+b)
else:
print(a*b)

4) Write a program in Python to read an year and display it is a leap year or not a leap year.
Ans:
a = int(input('Enter an year : '))
if a%4 == 0:
print('It is a leap year')
else:
print('It is not a leap year')

5) Write a program in Python to read a character and check it is a vowel or any other character.
Ans:
n = input('Enter an year : ')
if n in 'AEIOUaeiou':
print('It is a vowel')
else:
print('It is not a vowel')

6) Write a program in Python to read a character and change its case i.e. from upper case to lower case and vice
versa.
Ans:
n = input('Enter an alphabet : ')
if n >= 'A' and n <= 'Z':
print(chr(ord(n)+32))
elif n >= 'a' and n <= 'z':
print(chr(ord(n)-32))

7) Write a program in Python to read a character and change its case if it is a vowel i.e. from upper case vowel to
lower case vowel and vice versa.
Effective Computer Science 10
Ans:
n = input('Enter an alphabet : ')
if n in 'AEIOUaeiou':
if n >= 'A' and n <= 'Z':
print(chr(ord(n)+32))
elif n >= 'a' and n <= 'z':
print(chr(ord(n)-32))
else:
print('It is not a vowel')

8) Write a program in Python to read marks from a user and if it is greater than equal to 90 then display grade as ‘A’,
else if it is greater than equal to 70 then grade is ‘B’, else if it is greater than equal to 50 then grade is ‘C’, else if it
is greater than equal to 30 then grade is ‘D’, else grade is ‘E’.
Ans:
m = int(input("Enter Marks : "))
if m >= 90:
g = 'A'
elif m>=70:
g = 'B'
elif m>=50:
g = 'C'
elif m>=30:
g = 'D'
else:
g = 'E'
print('Grade : ', g)

9) Write a program in Python to read salary from a user and if it is greater than equal to 30000 then bonus is
calculated as 10% of the salary, else if it is greater than equal to 20000 then bonus is calculated as 20% of the
salary, else if it is greater than equal to 10000 then bonus is calculated as 30% of the salary, else bonus is
calculated as 40% of the salary.
Ans:
s = int(input('Enter Salary : '))
if s >= 30000:
b = 0.1 * s
elif s >= 20000:
b = 0.2 * s
elif s >= 10000:
b = 0.3 * s
else:
b = 0.4 * s
print('Bonus = ', b)

10) Write a program in Python to read salary from a user and calculate the Total Salary as Salary + DA + HRA + BONUS
– PF, where DA, HRA, BONUS, and PF is calculated as follows –
Salary DA HRA BONUS PF
<=10000 40% 5% 45% 15% of salary
>10000 and <=20000 30% 8% 35% 12% of salary
>20000 and <=30000 20% 10% 25% 10% of salary
>30000 10% 12% 15% 8% of salary

Ans:
s = int(input('Enter Basic Salary : '))
if s <= 10000:
d = 0.4 * s
h = 0.05 * s
b = 0.45 * s
p = 0.15 * s
Effective Computer Science 11
elif s > 10000 and s <= 20000:
d = 0.3 * s
h = 0.08 * s
b = 0.35 * s
p = 0.12 * s
elif s > 20000 and s <= 30000:
d = 0.2 * s
h = 0.1 * s
b = 0.25 * s
p = 0.1 * s
elif s > 30000:
d = 0.1 * s
h = 0.12 * s
b = 0.15 * s
p = 0.08 * s

t = s + d + h + b - p

print('Basic Salary = ', s)


print('DA = ', d)
print('HRA = ', h)
print('Bonu = ', b)
print('PF = ', p)
print('Total Salary = ', t)

11) Write a program in Python to read number of units from a user and calculate the electricity bill as follows –
Units Charges
<=100 500
>100 and <=200 2.5 per extra unit over 100
>200 and <=300 4.5 per extra unit over 200
>300 6.5 per extra unit over 300

Ans:
n = int(input('Enter Number of Units : '))
if n <= 100:
c = 500
elif n > 100 and n <= 200:
c = 500 + 2.5* (n-100)
elif n > 200 and n <= 300:
c = 500 + 2.5 * 100 + 4.5 * (n-200)
elif n > 300:
c = 500 + 2.5 * 100 + 4.5 * 100 + 6.5 * (n-300)
print('Total Electricity Bill is : ', c)

12) Write a program in Python to read number of telephone calls from a user and calculate the telephone bill as
follows –
Calls Charges
<=100 100
>100 and <=200 2 per extra calls over 100
>200 and <=300 3 per extra calls over 200
>300 4 per extra calls over 300

Ans:
n = int(input('Enter Number of Telephone Calls : '))
if n <= 100:
c = 100
elif n > 100 and n <= 200:
c = 100 + 2 * (n-100)
Effective Computer Science 12
elif n > 200 and n <= 300:
c = 100 + 2 * 100 + 3 * (n-200)
elif n > 300:
c = 100 + 2 * 100 + 3 * 100 + 4 * (n-300)
print('Total Telephone Bill is : ', c)

13) Write a program in Python to read income from a user and calculate the income tax as follows –
Income Tax
<=100000 Nil
>100000 and <=200000 10% of Income over 100000
>200000 and <=300000 20% of Income over 200000
>300000 30% of income over 300000
Ans:
n = int(input('Enter Your Income : '))
if n <= 100000:
t = 0
elif n > 100000 and n <= 200000:
c = 0.1 * (n-100000)
elif n > 200000 and n <= 300000:
c = 0.1 * 100000 + 0.2 * (n-200000)
elif n > 300000:
c = 0.1 * 100000 + 0.2 * 100000 + 0.3 * (n-300000)
print('Income Tax is : ', c)

14) Write a program in Python to read three numbers from a user and display the largest number.
Ans:
a = int(input('Enter Number 1 : '))
b = int(input('Enter Number 2 : '))
c = int(input('Enter Number 3 : '))
if a>b and a>c:
n = a
elif b>a and b>c:
n = b
elif c>a and c>b:
n = c
else:
n = 'All are equal'
print('Largest Number is : ', n)

15) Write a program in Python to read three numbers from a user and display the smallest number.
Ans:
a = int(input('Enter Number 1 : '))
b = int(input('Enter Number 2 : '))
c = int(input('Enter Number 3 : '))
if a<b and b<c:
n = a
elif b<a and b<c:
n = b
elif c<a and c<b:
n = c
else:
n = 'All are equal'
print('Smallest Number is : ', n)

16) Write a program in Python to read three numbers and display them by arranging them in the ascending order.
Ans:
a = int(input('Enter Number 1 : '))
b = int(input('Enter Number 2 : '))
c = int(input('Enter Number 3 : '))
Effective Computer Science 13
if a < b and b < c:
print(a, b, c)
n = a,b,c
elif b <c and c < a:
print(b, c, a)
elif c < a and a < b:
print(c, a, b)
elif c < b and b < a:
print(c, b, a)
elif b < a and a < c:
print(b, a, c)
elif a < c and c < b:
print(a, c, b)
else:
print('All are equal')

17) Write a program in Python to read three numbers and display them by arranging them in the descending order.
Ans:
a = int(input('Enter Number 1 : '))
b = int(input('Enter Number 2 : '))
c = int(input('Enter Number 3 : '))
if a > b and b > c:
print(a, b, c)
elif b > c and c > a:
print(b, c, a)
elif c > a and a > b:
print(c, a, b)
elif c > b and b > a:
print(c, b, a)
elif b > a and a > c:
print(b, a, c)
elif a > c and c > b:
print(a, c, b)
else:
print('All are equal')

18) Write a program in Python to read a digit from a user and print it in words. For example, if user gives 0 then the
output should be ‘Zero’.
Ans:
a = int(input('Enter a Digit : '))
if a == 0:
r = 'Zero'
elif a == 1:
r = 'One'
elif a == 2:
r = 'Two'
elif a == 3:
r = 'Three'
elif a == 4:
r = 'Four'
elif a == 5:
r = 'Five'
elif a == 6:
r = 'Six'
elif a == 7:
r = 'Seven'
elif a == 8:
r = 'Eight'
elif a == 9:
Effective Computer Science 14
r = 'Nine'
else:
r = 'Wrong Digit'
print('Output is : ', r)

19) Write a program in Python to read a day number from a user and print name of the day of the week.
Ans:
a = int(input('Enter a Day Number : '))
if a == 1:
print('Monday')
elif a == 2:
print('Tuesday')
elif a == 3:
print('Wednesday')
elif a == 4:
print('Thursday')
elif a == 5:
print('Friday')
elif a == 6:
print('Saturday')
elif a == 7:
print('Sunday')
else:
print('Invalid Day Number')

20) Write a program in Python to read a month number from a user and print name of the month.
Ans:
a = int(input('Enter a Mounth Number : '))
if a == 1:
print('January')
elif a == 2:
print('February')
elif a == 3:
print('March')
elif a == 4:
print('April')
elif a == 5:
print('May')
elif a == 6:
print('June')
elif a == 7:
print('July')
elif a == 8:
print('August')
elif a == 9:
print('September')
elif a == 10:
print('October')
elif a == 11:
print('November')
elif a == 12:
print('December')
else:
print('Invalid Mounth Number')

21) Write a program in Python to read grade from a user if grade is ‘A’ then display “Excellent”, if grade is ‘B’ then
display “Very Good”, if grade is ‘C’ then display “Good”, if grade is ‘D’ then display “Satisfactory”, if grade is ‘E’
then display “Un-Satisfactory” else display “Invalid Grade”.
Effective Computer Science 15
Ans:
a = input('Enter a Grade : ')
if a == 'A' or a == 'a':
print('Excellent')
elif a == 'B' or a == 'b':
print('Very Good')
elif a == 'C' or a == 'c':
print('Good')
elif a == 'D' or a == 'd':
print('Satisfactory')
elif a == 'E' or a == 'e':
print('Un-Satisfactory')
else:
print('Invalid Grade')

22) Write a program in Python to read two numbers and a mathematical operator ( + , – , * , / ) form user and
display the result according to operator.
Ans:
a = int(input('Enter a Number : '))
b = int(input('Enter another Number : '))
op = input('Enter a Mathematical Operator : ')
if op == '+':
r = a + b
elif op == '-':
r = a - b
elif op == '*':
r = a * b
elif op == '/':
r = a / b
else:
r = 'Wrong Operator'
print('Result is : ', r)

23) Write a program in Python to read choice from a user as 1, 2 or 3. If choice is 1 then display area of the circle, if
choice is 2 then display area of the rectangle, if choice is 3 then display area of the square else display “Invalid
Choice”.
Ans:
print('Press 1 to display area of the circle')
print('Press 2 to display area of the rectangle')
print('Press 3 to display area of the square')
ch = int(input('Enter Your Choice : '))
if ch == 1:
r = int(input('Enter Radius of the circle : '))
a = 3.14 * r * r
print('Area = ', a)
elif ch == 2:
l = int(input('Enter Length of the rectangle : '))
b = int(input('Enter Breadth of the rectangle : '))
a = l * b
print('Area = ', a)
elif ch == 3:
s = int(input('Enter Side of a square : '))
a = s * s
print('Area = ', a)
else:
print('Invalid Choice')
Effective Computer Science 16
Problems of Iteration Construct

1) Write a program in Python to display your name 10 times.


Ans:
i = 1 for i in range(10):
while i <= 10: print('Your Name')
print('Your Name')
i+=1

2) Write a program in Python to display numbers from 1 to 100.


Ans:
i = 1 for i in range(1, 101):
while i <= 100: print(i, end = ', ')
print(i, end = ', ')
i+=1

3) Write a program in Python to display number from 100 to 1.


Ans:
i = 100 for i in range(100, 0, -1):
while i >= 1: print(i, end = ', ')
print(i, end = ', ')
i-=1

4) Write a program in Python to display even numbers from 1 to 100.


Ans:
i = 2 for i in range(2, 101, 2):
while i <= 100: print(i, end = ', ')
print(i, end = ', ')
i+=2

5) Write a program in Python to display odd number from 100 to 1.


Ans:
i = 9 for i in range(99, 0, -2):
while i >= 1: print(i, end = ', ')
print(i, end = ', ')
i-=2

6) Write a program in Python to display numbers which are divisible by 5 in the range of two numbers given by a
user a and b where a < = b.
Ans:
a = int(input('Enter Start Value :')) a = int(input('Enter Start Value :'))
b = int(input('Enter End Value : ')) b = int(input('Enter End Value : '))
i = a for i in range(a, b+1):
while i <=b: if i%5 == 0:
if i%5 == 0: print(i, end = ', ')
print(i, end = ', ')
i+=1

7) Write a program in Python to print table of a number given by a user.


Ans: n = int(input('Enter a Number :'))
i = 1 n = int(input('Enter a Number :'))
while i <= 10: for i in range(1, 11):
print(n, 'X', i, '=', n*i) print(n, 'X', i, '=', n*i)
i+=1
Effective Computer Science 17
8) Write a program in Python to display sum of 1 to 100 numbers.
Ans:
s = 0 s = 0
i = 1 for i in range(1, 101):
while i <= 100: s = s + i
s = s + i print('Sum = ', s)
i+=1
print('Sum = ', s)

9) Write a program in Python to display factorial of a number given by a user.


Ans:
n = int(input('Enter a number : ')) n = int(input('Enter a number : '))
f = 1 f = 1
i = n for i in range(n, 0, -1):
while i >= 1: f = f * i
f = f * i print('Factorial is', f)
i-=1
print('Factorial is', f)

10) Write a program in Python to display sum of even numbers from 1 to 100.
Ans:
s = 0 s = 0
i = 2 for i in range(2, 101, 2):
while i <= 100: s = s + i
s = s + i print('Sum = ', s)
i+=2
print('Sum = ', s)

11) Write a program in Python to display product of odd number from 10 to 1.


Ans:
p = 1 p = 1
i = 9 for i in range(9, 0, -2):
while i >= 1: p = p * i
p = p * i print('Product = ', p)
i-=2
print('Product = ', p)

12) Write a program in Python to read two number from a user say m and n and display m to the power n (m n)
without using ** operator.
Ans:
m = int(input('Enter base : ')) m = int(input('Enter base : '))
n = int(input('Enter power : ')) n = int(input('Enter power : '))
p = 1 p = 1
i = 1 for i in range(n):
while i <= n: p = p * m
p = p * m print('m to the power n = ', p)
i+=1
print('m to the power n = ', p)

13) Write a program in Python to generate a Fibonacci series for n number of terms.
(Fibonacci series = 0, 1, 1, 2, 3, 5, 8, 13, 21, -------n)
Ans:
n=int(input('Enter number of terms : ')) n = int(input('Enter number of terms :
a = 0 '))
b = 1 a = 0
print(a, b, end = ' ') b = 1
i = 1 print(a, b, end = ' ')
while i <= n-2: for i in range(n-2):
c = a + b c = a + b
Effective Computer Science 18
print(c , end = ' ') print(c , end = ' ')
a = b a = b
b = c b = c
i+=1

14) Write a program in Python to generate the following series for n number of terms given by a user = 0, 1, 1, 2, 4, 7,
13, 24, -------------n)
Ans:
n=int(input('Enter number of terms : ')) n=int(input('Enter number of terms : '))
a = 0 a = 0
b = 1 b = 1
c = 1 c = 1
print(a, b, c, end = ' ') print(a, b, c, end = ' ')
i = 1 for i in range (n-3):
while i <= n-3: d = a + b + c
d = a + b + c print(d , end = ' ')
print(d , end = ' ') a = b
a = b b = c
b = c c = d
c = d
i+=1

15) Write a program in Python to display sum of the following series 1 ! + 2 ! + 3 ! + 4 ! + 5 ! + ---------n !
Ans:
n = int(input('Enter a number : ')) n = int(input('Enter a number : '))
f = 1 f = 1
i = n for i in range(n, 0, -1):
while i >= 1: f = f * i
f = f * i print('Factorial = ', f)
i-=1
print('Factorial = ', f)

16) Write a program in Python to display factors of a number given by a user.


Ans:
n = int(input('Enter a number : ')) n = int(input('Enter a number : '))
i = 1 for i in range(1, n+1):
while i <= n: if n%i == 0:
if n%i == 0: print(i, end = ' ')
print(i, end = ' ')
i+=1

17) Write a program in Python to check whether a number given by a user is Prime number or not.
Ans:
n = int(input('Enter a number : ')) n = int(input('Enter a number : '))
x = 0 x = 0
i = 1 for i in range(1, n//2+1):
while i <= n//2: if n%i == 0:
if n%i == 0: x+=1
x+=1 if x == 1:
i+=1 print(n , 'is a Prime Number')
if x == 1: else:
print(n , 'is a Prime Number') print(n , 'is not a Prime Number')
else:
print(n , 'is not a Prime Number')

18) Write a program in Python to print the pairs of twin prime numbers for 1 to n, where n is the last term of the
range and n>1. Twin prime numbers are those prime number which are having the difference of 2. For example,
(3, 5), (5, 7), (11, 13), (29, 31) are pairs of twin prime numbers.
Effective Computer Science 19
Ans:
a = int(input('Enter a number : '))
p = 1
q = 1
for n in range(1, a+1):
x = 0
for i in range(1, n//2+1):
if n%i == 0:
x+=1
if x == 1:
p = q
q = n
if q - p == 2:
print((p, q))

19) Write a program in Python to check whether a number given by a user is a Perfect number or not. (eg – 6 sum of
factors is equal to number )
Ans:
n = int(input('Enter a number : '))
s = 0
for i in range(1, n+1):
if n%i ==0 :
s = s + i
if s == n:
print(n, 'is a Perfect Number')
else:
print(n, 'is not a Perfect Number')

20) Write a program in Python to display HCF and LCM of two numbers given by a user.
Ans:
a = int(input('Enter a number : '))
b = int(input('Enter another number : '))
x = a
y = b
while b > 0:
r = a%b
a = b
b = r

l = x*y//a
print('HCF = ', a)
print('LCM = ', l)

21) Write a program in Python to read a number from a user and display the number by reversing its digits.
Ans:
n = int(input('Enter a number : '))
r = 0
while n > 0:
d = n % 10
n = n // 10
r = r * 10 + d
print('Reverse of the number = ', r)

22) Write a program in Python to check whether a number given by a user is a palindrome number or not.
Ans:
n = int(input('Enter a number : '))
m = n
r = 0
while n > 0:
Effective Computer Science 20
d = n % 10
n = n // 10
r = r * 10 + d
if r == m:
print('It is a Palindrome number')
else:
print('It is not a Palindrome number')

23) Write a program in Python to count how many odd and even digits are present in a number given by a user.
Ans:
n = int(input('Enter a number : '))
x = y = 0
while n > 0:
d = n % 10
n = n // 10
if d % 2 == 0:
x+=1
else:
y+=1
print('Number of Even digits are', x)
print('Number of Odd digits are', y)

24) Write a program in Python to display sum of even digits of a number given by a user
Ans:
n = int(input('Enter a number : '))
x = 0
while n > 0:
d = n % 10
n = n // 10
if d % 2 == 0:
x+=d
print('Sum of Even digits is', x)

25) Write a program in Python to display difference of sum of odd and sum of even digits of a number given by a
user.
Ans:
n = int(input('Enter a number : '))
x = y = 0
while n > 0:
d = n % 10
n = n // 10
if d % 2 == 0:
x+=d
else:
y+=d
print(y-x)

26) Write a program in Python to check whether a number given by a user is an Armstrong number or not.
(For example, 153 = 13 + 53 + 33 )
Ans:
n = int(input('Enter a number : '))
m = n
s = 0
while n > 0:
d = n % 10
n = n // 10
s = s + d**3
if s == m:
print('It is a Armstrong number')
Effective Computer Science 21
else:
print('It is not a Armstrong number')

27) Write a program in Python to check whether a number given by a user is an Automorphic number or not. ( eg –
25, equal to the last digits of its square)
Ans:
n = int(input('Enter a number : '))
m = n
sq = n*n
f = 1
while n > 0:
d1 = n % 10
n = n // 10
d2 = sq % 10
sq = sq // 10
if d1 != d2:
f = 0
break
if f == 1:
print('It is a Automorphic number')
else:
print('It is not a Automorphic number')

28) Write a program in Python to check whether a number given by a user is a Buzz number or not. (if number is
divisible by 7 or sum of the digits is 7 )
Ans:
n = int(input('Enter a number : '))
m = n
s = 0
while n > 0:
d = n % 10
n = n // 10
s = s + d
if m % 7 == 0 or s == 7:
print('It is a Buzz number')
else:
print('It is not a Buzz number')

29) Write a program in Python to display prime number from 1 to 1000.


Ans:
for n in range(1, 1001):
x = 0
for i in range(1, n//2+1):
if n%i == 0:
x+=1
if x == 1:
print(n , end = ',')

30) Write a program in Python to display Armstrong number from 1 to 1000.


Ans:
for n in range(1, 1001):
m = n
s = 0
while n > 0:
d = n % 10
n = n // 10
s = s + d**3
if s == m:
print(m, end = ',')
Effective Computer Science 22

31) Write a program in Python to display Automorphic number from 1 to 1000.


Ans:
for n in range(1, 1001):
m = n
sq = n*n
f = 1
while n > 0:
d1 = n % 10
n = n // 10
d2 = sq % 10
sq = sq // 10
if d1 != d2:
f = 0
break
if f == 1:
print(m, end = ',')

32) Write a program in Python to read decimal number from a user and convert the given number into its equivalent
binary number.
Ans:
n = int(input('Enter a number : '))
a = ()
while n > 0:
r = n % 2
a = a + (r,)
n = n // 2
print(a[::-1])

33) Write a program in Python to read numbers from a user until user gives zero, display the sum of the numbers
given by a user.
Ans:
s =0
while True:
n = int(input('Enter a number : '))
if n == 0:
break
s = s + n
print('Sum = ', s)

34) Write a program in Python to read numbers from a user until user gives zero, display the sum of odd numbers
entered by a user.
Ans:
s =0
while True:
n = int(input('Enter a number : '))
if n == 0:
break
if n % 2 != 0:
s = s + n
print('Sum = ', s)

35) Write a program in Python to read numbers from a user until user gives zero, display how many odd and even
numbers given by a user.
Ans:
x = y = 0
while True:
n = int(input('Enter a number : '))
if n == 0:
Effective Computer Science 23
break
if n % 2 == 0:
x+=1
else:
y+=1
print('Number of Even number given are ', x)
print('Number of Odd number given are ', y)

36) Write a program in Python to read numbers from a user until user’s choice is ‘Y’ and display the sum of even
numbers given by a user.
Ans:
s = 0
while True:
n = int(input('Enter a number : '))
if n % 2 == 0:
s = s + n
ch = input('Press Y/y to give more numbers : ')
if ch not in 'Yy':
break
print('Sum of even number given is ', s)

37) Write a program in Python to read numbers from a user until user presses ‘N’ / ‘n’. Display the largest and
smallest number entered by the user.
Ans:
n = int(input('Enter a number : '))
L = n
S = n
ch = input('Press N/n to stop : ')
while ch not in 'Nn':
n = int(input('Enter a number : '))
if n > L:
L = n
elif n < S:
S = n
ch = input('Press N/n to stop : ')
print('Largest Number is', L)
print('Smallest Number is', S)

38) Write a program in Python to read the value of x and n from a user and display sum of the following series –

𝑥 𝑥 𝑥 𝑥 𝑥 𝑥2 𝑥3 𝑥4 𝑥5 𝑥6
(1) x + + + + + …𝑛 (2) x+ + + + + …𝑛
2 3 4 5 6 2 3 4 5 6

x=int(input('Enter Value of x:')) x=int(input('Enter Value of x:'))


n=int(input('Enter Value of n:')) n=int(input('Enter Value of n:'))
s = 0 s = 0
Ans: Ans:
for i in range (1, n+1): for i in range (1, n+1):
s = s + x/i s = s + x**i/i
print('Sum = ', s) print('Sum = ', s)

𝑥 3 𝑥 5 𝑥 7 𝑥 9 𝑥 11 𝑥3 𝑥4 𝑥5 𝑥6 𝑥7
(3) x+ + + + + …𝑛 (4) x+ + + + + …𝑛
2 3 4 5 6 2 3 4 5 6
Effective Computer Science 24

x=int(input('Enter Value of x:')) x=int(input('Enter Value of x:'))


n=int(input('Enter Value of n:')) n=int(input('Enter Value of n:'))
s = 0 s = 0
Ans: Ans:
for i in range (1, n+1): for i in range (2, n+1):
s = s + x**(i*2-1)/i s = s + x**(i+1)/i
print('Sum = ', s) print('Sum = ', x+s)

𝑥2 𝑥3 𝑥4 𝑥5 𝑥6 𝑥2 𝑥3 𝑥4 𝑥5 𝑥6
(5) x+ + + + + …𝑛 (6) + + + + …𝑛
3 5 7 9 11 2 4 6 8 10

x=int(input('Enter Value of x:')) x=int(input('Enter Value of x:'))


n=int(input('Enter Value of n:')) n=int(input('Enter Value of n:'))
s = 0 s = 0
Ans: Ans:
for i in range (2, n+1): for i in range (2, n+1):
s = s + x**i/(i*2-1) s = s + x**i/(i*2-2)
print('Sum = ', s) print('Sum = ', s)

𝑥 3 𝑥 5 𝑥 7 𝑥 9 𝑥 11 𝑥3 𝑥4 𝑥5 𝑥6 𝑥7
(7) x− + − + − + ⋯𝑛 (8) −x + − + − + …𝑛
2 3 4 5 6 2 3 4 5 6

n=int(input('Enter Value of n:')) n=int(input('Enter Value of n:'))


x=int(input('Enter Value of x:')) x=int(input('Enter Value of x:'))
s = 0 s = 0
sign = 1 sign = 1
Ans: Ans:
for i in range (1, n+1): for i in range (2, n+1):
s = s + sign*(x**(i*2-1) )/i s = s + sign*(x**(i+1))/i
sign = sign * -1 sign = sign * -1
print('Sum = ', s) print('Sum = ', -x + s)

𝑥2 𝑥3 𝑥4 𝑥5 𝑥6 𝑥 2 𝑥 4 𝑥 6 𝑥 8 𝑥 10
(9) x− + − + − + ⋯𝑛 (10) − + − + …𝑛
3 5 7 9 11 2 4 6 8 10
n=int(input('Enter Value of n:')) n=int(input('Enter Value of n:'))
x=int(input('Enter Value of x:')) x=int(input('Enter Value of x:'))
s = 0 s = 0
sign = 1 sign = 1
Ans: Ans:
for i in range (1, n+1): for i in range (2, n*2, 2):
s = s + sign*(x**i)/(i*2-1) s = s + sign*(x**i)/i
sign = sign * -1 sign = sign * -1
print('Sum = ', s) print('Sum = ', s)
𝑥 𝑥 𝑥 𝑥 𝑥 𝑥2 𝑥3 𝑥4 𝑥5 𝑥6
(11) x+ + + + + …𝑛 (12) x+ + + + + …𝑛
2! 3! 4! 5! 6! 2! 3! 4! 5! 6!

n=int(input('Enter Value of n:')) n=int(input('Enter Value of n:'))


x=int(input('Enter Value of x:')) x=int(input('Enter Value of x:'))
s = 0 s = 0
for i in range (1, n+1): for i in range (1, n+1):
Ans: f = 1 Ans: f = 1
for j in range(i, 0, -1): for j in range(i, 0, -1):
f = f * j f = f * j
s = s + x/f s = s + x**i/f
print('Sum = ', s) print('Sum = ', s)
Effective Computer Science 25
𝑥 3 𝑥 5 𝑥 7 𝑥 9 𝑥 11 𝑥3 𝑥4 𝑥5 𝑥6 𝑥7
(13) x+ + + + + …𝑛 (14) x+ + + + + …𝑛
2! 3! 4! 5! 6! 2! 3! 4! 5! 6!

n=int(input('Enter Value of n:')) n=int(input('Enter Value of n:'))


x=int(input('Enter Value of x:')) x=int(input('Enter Value of x:'))
s = 0 s = 0
for i in range (1, n+1): for i in range (2, n+1):
Ans: f = 1 Ans: f = 1
for j in range(i, 0, -1): for j in range(i, 0, -1):
f = f * j f = f * j
s = s + x**(i*2-1)/f s = s + x**(i+1)/f
print('Sum = ', s) print('Sum = ', x+s)

𝑥2 𝑥3 𝑥4 𝑥5 𝑥6 𝑥2 𝑥3 𝑥4 𝑥5 𝑥6
(15) x+ + + + + …𝑛 (16) + + + + …𝑛
3! 5! 7! 9! 11! 2! 4! 6! 8! 10!

n=int(input('Enter Value of n:'))


n=int(input('Enter Value of n:'))
x=int(input('Enter Value of x:'))
x=int(input('Enter Value of x:'))
s = 0
s = 0
for i in range (2, n+1):
for i in range (2, n+1):
Ans: f = 1 Ans:
f = 1
for j in range(i*2-1, 0, -1):
for j in range(i*2-2, 0, -1):
f = f * j
f = f * j
s = s + x**i/f
s = s + x**i/f
print('Sum = ', s)
print('Sum = ', s)

𝑥 3 𝑥 5 𝑥 7 𝑥 9 𝑥 11 𝑥3 𝑥4 𝑥5 𝑥6 𝑥7
(17) x− + − + − + ⋯𝑛 (18) −x + − + − + … 𝑛
2! 3! 4! 5! 6! 2! 3! 4! 5! 6!

n=int(input('Enter Value of n:')) n=int(input('Enter Value of n:'))


x=int(input('Enter Value of x:')) x=int(input('Enter Value of x:'))
s = 0 s = 0
sign = 1 sign = 1
for i in range (1, n+1): for i in range (2, n+1):
Ans: Ans:
f = 1 f = 1
for j in range(i, 0, -1): for j in range(i, 0, -1):
f = f * j f = f * j
s = s + sign*(x**(i*2-1))/f s = s + sign*(x**(i+1))/f
sign = sign * -1 sign = sign * -1
print('Sum = ', s) print('Sum = ', -x + s)
𝑥2 𝑥3 𝑥4 𝑥5 𝑥6 𝑥 2 𝑥 4 𝑥 6 𝑥 8 𝑥 10
(19) x− + − + − + ⋯𝑛 (20) − + − + …𝑛
3! 5! 7! 9! 11! 2! 4! 6! 8! 10!

n=int(input('Enter Value of n:')) n=int(input('Enter Value of n:'))


x=int(input('Enter Value of x:')) x=int(input('Enter Value of x:'))
s = 0 s = 0
sign = 1 sign = 1
for i in range (1, n+1): for i in range (2, n*2, 2):
Ans: Ans:
f = 1 f = 1
for j in range(i*2-1, 0, -1): for j in range(i, 0, -1):
f = f * j f = f * j
s = s + sign*(x**i))/f s = s + sign*(x**i))/f
sign = sign * -1 sign = sign * -1
print('Sum = ', s) print('Sum = ', s)
Effective Computer Science 26

(21) (22)

n=int(input('Enter Value of n:'))


x=int(input('Enter Value of x:'))
n=int(input('Enter Value of n:'))
s = 0
x=int(input('Enter Value of x:'))
for i in range (2, n+1):
s = 0
Ans: Ans: f = 1
for i in range (2, n+1):
for j in range(i+1, 0, -1):
s = s + x**(1//i)
f = f * j
print('Sum = ', s)
s = s + x**(1//i)
print('Sum = ', s)

(23) (2) (2 + 4) (2 + 4 + 6) ----n (24) (2) (2 + 42) (2 + 42 + 63) ------n

n=int(input('Enter Value of n:')) n=int(input('Enter Value of n:'))


p = 1 p = 1
for i in range (2, n*2+1, 2): for i in range (2, n*2+1, 2):
s = 0 s = 0
for j in range(2, i, 2): for j in range(2, i, 2):
s = s + j s = s + j**(j//2)
p = p * s p = p * s
print('Product = ', p) print('Product = ', p)

39) Write a program in Python to read the value of x and n from a user and display sum of the following series –
1
1
2 1
1 2
(1) (2) 3 2 1
1 2 3
4 3 2 1
1 2 3 4
5 4 3 2 1
1 2 3 4 5

for i in range(1, 6): for i in range(1, 6):


Ans: for j in range(1, i+1): Ans: for j in range(i, 0, -1):
print(j, end = ' ') print(j, end = ' ')
print() print()
1 2 3 4 5
1 2 3 4 5
2 3 4 5
1 2 3 4
(3) (4) 3 4 5
1 2 3
4 5
1 2
5
1

for i in range(5, 0, -1): for i in range(1, 6):


for j in range(1, i+1): for j in range(i, 6):
Ans: Ans:
print(j, end = ' ') print(j, end = ' ')
print() print()

5
5
4 5
5 4
(5) (6) 3 4 5
5 4 3
2 3 4 5
5 4 3 2
1 2 3 4 5
5 4 3 2 1
Effective Computer Science 27

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


for j in range(5, i-1, -1): for j in range(i, 6):
Ans: Ans:
print(j, end = ' ') print(j, end = ' ')
print() print()

5 4 3 2 1 5 4 3 2 1
5 4 3 2 4 3 2 1
(7) 5 4 3 (8) 3 2 1
5 4 2 1
5 1

for i in range(1, 6): for i in range(5, 0, -1):


for j in range(5, i-1, -1): for j in range(i, 0, -1):
Ans: Ans:
print(j, end = ' ') print(j, end = ' ')
print() print()

1 1 1 1 1
1
2 2 2 2
2 2
(9) (10) 3 3 3
3 3 3
4 4
4 4 4 4
5
5 5 5 5 5

for i in range(1, 6): for i in range(1, 6):


for j in range(i, 0, -1): for j in range(i, 6):
Ans: Ans:
print(i, end = ' ') print(i, end = ' ')
print() print()

5 5 5 5 5
5
4 4 4 4
4 4
(11) (12) 3 3 3
3 3 3
2 2
2 2 2 2
1
1 1 1 1 1

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


for j in range(i, 6): for j in range(i, 0, -1):
Ans: Ans:
print(i, end = ' ') print(i, end = ' ')
print() print()

A
A
A B
B A
(13) A B C (14)
C B A
A B C D
D C B A
A B C D E
E D C B A

for i in range(1, 6):


for j in range(1, i+1): for i in range(1, 6):
print(chr(64+j), end = ' ') for j in range(i, 0, -1):
Ans: Ans:
print() print(chr(64+j), end=' ')
print()
Effective Computer Science 28
A B C D E
A B C D E
B C D E
A B C D
(15) (16) C D E
A B C
D E
A B
E
A

for i in range(5, 0, -1): for i in range(1, 6):


for j in range(1, i+1): for j in range(i, 6):
Ans: Ans:
print(chr(64+j), end = ' ') print(chr(64+j), end=' ')
print() print()

E
E
D E
E D
(17) (18) C D E
E D C
B C D E
E D C B
A B C D E
E D C B A

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


for j in range(5, i-1, -1): for j in range(i , 6):
Ans: Ans:
print(chr(64+j), end = ' ') print(chr(64+j), end=' ')
print() print()

E D C B A E D C B A
E D C B D C B A
(19) E D C (20) C B A
E D B A
E A

for i in range(1, 6): for i in range(5, 0, -1):


for j in range(5, i-1, -1): for j in range(i, 0, -1):
Ans: Ans:
print(chr(64+j), end = ' ') print(chr(64+j), end=' ')
print() print()

* * * * *
*
* * * *
* *
(21) (22) * * *
* * *
* *
* * * *
*
* * * * *

for i in range(1, 6): for i in range(5, 0, -1):


for j in range(1, i+1): for j in range(1, i+1):
Ans: Ans:
print('*', end = ' ') print('*', end = ' ')
print() print()

*
* # * # * # *
* # * * # * #
(23) * # * # (24) * # *
* # * # * * #
*
Effective Computer Science 29

for i in range(1, 6): for i in range(5, 0, -1):


for j in range(1, i+1): for j in range(1, i+1):
if j%2 != 0: if j%2 != 0:
Ans: print('*', end = ' ') Ans: print('*', end = ' ')
else: else:
print('#', end = ' ') print('#', end = ' ')
print() print()

* * * * *
*
# # # #
# #
(25) (26) * * *
* * *
# #
# # # #
*
* * * * *

for i in range(1, 6): for i in range(5, 0, -1):


for j in range(1, i+1): for j in range(1, i+1):
if i%2 != 0: if i%2 != 0:
Ans: print('*', end = ' ') Ans: print('*', end = ' ')
else: else:
print('#', end = ' ') print('#', end = ' ')
print() print()

* # * # * *
# * * # * #
(27) * # * (28) # * #
# * # * * #
* # * # * #

for i in range(1, 6): for i in range(5, 0, -1):


for j in range(1, i+1): for j in range(1, i+1):
if i%2 != 0 and j%2 != 0: if i%2 != 0 and j%2 != 0:
print('*', end = ' ') print('*', end = ' ')
if i%2 != 0 and j%2 == 0: if i%2 != 0 and j%2 == 0:
Ans: print('#', end = ' ') Ans: print('#', end = ' ')
if i%2 == 0 and j%2 != 0: if i%2 == 0 and j%2 != 0:
print('#', end = ' ') print('#', end = ' ')
if i%2 == 0 and j%2 == 0: if i%2 == 0 and j%2 == 0:
print('*', end = ' ') print('*', end = ' ')
print() print()

1
1
2 1
1 2
(29) (30) 3 2 1
1 2 3
4 3 2 1
1 2 3 4
5 4 3 2 1
1 2 3 4 5

for i in range(1, 6): for i in range(1, 6):


for j in range(5-i): for j in range(5-i):
print(' ', end = ' ') print(' ', end = ' ')
Ans: Ans:
for j in range(1, i+1): for j in range(i, 0, -1):
print(j, end = ' ') print(j, end = ' ')
print() print()
Effective Computer Science 30

1 2 3 4 5 1 2 3 4 5
1 2 3 4 2 3 4 5
(31) 1 2 3 (32) 3 4 5
1 2 4 5
1 5

for i in range(5, 0, -1): for i in range(1, 6):


for j in range(5-i): for j in range(i-1):
print(' ', end = ' ') print(' ', end = ' ')
Ans: Ans:
for j in range(1, i+1): for j in range(i, 6):
print(j, end = ' ') print(j, end = ' ')
print() print()

5 5
5 4 4 5
(33) 5 4 3 (34) 3 4 5
5 4 3 2 2 3 4 5
5 4 3 2 1 1 2 3 4 5

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


for j in range(i-1): for j in range(i-1):
print(' ', end = ' ') print(' ', end = ' ')
Ans: Ans:
for j in range(5, i-1, -1): for j in range(i, 6):
print(j, end = ' ') print(j, end = ' ')
print() print()

5 4 3 2 1
5 4 3 2 1
4 3 2 1
5 4 3 2
(35) (36) 3 2 1
5 4 3
2 1
5 4
1
5

for i in range(1, 6): for i in range(5, 0, -1):


for j in range(i-1): for j in range(5-i):
print(' ', end = ' ') print(' ', end = ' ')
Ans: Ans:
for j in range(5, i-1, -1): for j in range(i, 0, -1):
print(j, end = ' ') print(j, end = ' ')
print() print()

1 1 1 1 1
1
2 2 2 2
2 2
(37) (38) 3 3 3
3 3 3
4 4
4 4 4 4
5
5 5 5 5 5

for i in range(1, 6): for i in range(1, 6):


for j in range(5-i): for j in range(i-1):
print(' ', end = ' ') print(' ', end = ' ')
Ans: Ans:
for j in range(i, 0, -1): for j in range(i, 6):
print(i, end = ' ') print(i, end = ' ')
print() print()
Effective Computer Science 31
5 5 5 5 5
5
4 4 4 4
4 4
(39) (40) 3 3 3
3 3 3
2 2
2 2 2 2
1
1 1 1 1 1

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


for j in range(i-1): for j in range(5-i):
print(' ', end = ' ') print(' ', end = ' ')
Ans: Ans:
for j in range(i, 6): for j in range(i, 0, -1):
print(i, end = ' ') print(i, end = ' ')
print() print()

A A
A B B A
(41) A B C (42) C B A
A B C D D C B A
A B C D E E D C B A

for i in range(1, 6): for i in range(1, 6):


for j in range(5-i): for j in range(5-i):
print(' ', end = ' ') print(' ', end = ' ')
Ans: Ans:
for j in range(1, i+1): for j in range(i, 0, -1):
print(chr(64+j), end = ' ') print(chr(64+j), end=' ')
print() print()

A B C D E
A B C D E
B C D E
A B C D
(43) (44) C D E
A B C
D E
A B
E
A

for i in range(5, 0, -1): for i in range(1, 6):


for j in range(5-i): for j in range(i-1):
print(' ', end = ' ') print(' ', end = ' ')
Ans: Ans:
for j in range(1, i+1): for j in range(i, 6):
print(chr(64+j), end = ' ') print(chr(64+j), end=' ')
print() print()

E
E
D E
E D
(45) (46) C D E
E D C
B C D E
E D C B
A B C D E
E D C B A

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


for i in range(5, 0, -1):
for j in range(i-1):
for j in range(i-1):
print(' ', end = ' ')
print(' ', end = ' ')
Ans: for j in range(5, i-1, -1): Ans:
for j in range(i, 6):
print(chr(64+j), end = ' ')
print(chr(64+j), end=' ')
print()
print()
Effective Computer Science 32
E D C B A
E D C B A
D C B A
E D C B
(47) (48) C B A
E D C
B A
E D
A
E

for i in range(1, 6): for i in range(5, 0, -1):


for j in range(i-1): for j in range(5-i):
print(' ', end = ' ') print(' ', end = ' ')
Ans: Ans:
for j in range(5, i-1, -1): for j in range(i, 0, -1):
print(chr(64+j), end = ' ') print(chr(64+j), end=' ')
print() print()

* * * * *
*
* * * *
* *
(49) (50) * * *
* * *
* *
* * * *
*
* * * * *

for i in range(1, 6): for i in range(5, 0, -1):


for j in range(5-i): for j in range(5-i):
print(' ', end = ' ') print(' ', end = ' ')
Ans: Ans:
for j in range(1, i+1): for j in range(1, i+1):
print('*', end = ' ') print('*', end = ' ')
print() print()

A A A A A
A
B B B B
B B
(51) (52) C C C
C C C
D D
D D D D
E
E E E E E

for i in range(1, 6): for i in range(1, 6):


for j in range(5-i): for j in range(i-1):
print(' ', end = ' ') print(' ', end = ' ')
Ans: Ans:
for j in range(i, 0, -1): for j in range(i, 6):
print(chr(64+i), end = ' ') print(chr(64+i), end=' ')
print() print()

E E E E E E
D D D D D D
(53) C C C (54) C C C
B B B B B B
A A A A A A

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


for i in range(5, 0, -1):
for j in range(i-1):
for j in range(5-i):
print(' ', end = ' ')
print(' ', end = ' ')
Ans: for j in range(i, 6): Ans:
for j in range(i, 0, -1):
print(chr(64+i), end = ' ')
print(chr(64+i), end=' ')
print()
print()
Effective Computer Science 33

1 1
1 2 1 2 1 2
(55) 1 2 3 2 1 (56) 3 2 1 2 3
1 2 3 4 3 2 1 4 3 2 1 2 3 4
1 2 3 4 5 4 3 2 1 5 4 3 2 1 2 3 4 5

for i in range(1, 6): for i in range(1, 6):


for j in range(5-i): for j in range(5-i):
print(' ', end = ' ') print(' ', end = ' ')
for j in range(1, i+1): for j in range(i, 0, -1):
Ans: Ans:
print(j, end = ' ') print(j, end = ' ')
for j in range(i-1, 0, -1): for j in range(2, i+1):
print(j, end = ' ') print(j, end = ' ')
print() print()

5 4 3 2 1 2 34 5
1 2 3 4 5 4 3 2 1
4 3 2 1 2 3 4
1 2 3 4 3 2 1
(57) (58) 3 2 1 2 3
1 2 3 2 1
2 1 2
1 2 1
1
1

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


for j in range(5-i): for j in range(5-i):
print(' ', end = ' ') print(' ', end = ' ')
for j in range(1, i+1): for j in range(i, 0, -1):
Ans: Ans:
print(j, end = ' ') print(j, end = ' ')
for j in range(i-1, 0,-1): for j in range(2, i+1):
print(j, end = ' ') print(j, end = ' ')
print() print()

5 5
5 4 5 4 5 4
(59) 5 4 3 4 5 (60) 3 4 5 4 3
5 4 3 2 3 4 5 2 3 4 5 4 3 2
5 4 3 2 1 2 3 4 5 1 2 3 4 5 4 3 2 1

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


for j in range(i-1): for j in range(i-1):
print(' ', end = ' ') print(' ', end = ' ')
for j in range(5, i-1, -1): for j in range(i, 6):
Ans: Ans:
print(j, end = ' ') print(j, end = ' ')
for j in range(i+1, 6): for j in range(4, i-1, -1):
print(j, end = ' ') print(j, end = ' ')
print() print()

5 4 3 2 1 2 3 4 5
1 2 3 4 5 4 3 2 1
5 4 3 2 3 4 5
2 3 4 5 4 3 2
5 4 3 4 5
(61) (62) 3 4 5 4 3
5 4 5
4 5 4
5
5
Effective Computer Science 34

for i in range(1, 6): for i in range(1, 6):


for j in range(i-1): for j in range(i-1):
print(' ', end = ' ') print(' ', end = ' ')
for j in range(5, i-1, -1): for j in range(i, 6):
Ans: Ans:
print(j, end = ' ') print(j, end = ' ')
for j in range(i+1, 6): for j in range(4, i-1, -1):
print(j, end = ' ') print(j, end = ' ')
print() print()

1 2 3 4 5 6 7 8 9
1
1 2 3 4 5 6 7
1 2 3
(63) (64) 1 2 3 4 5
1 2 3 4 5
1 2 3
1 2 3 4 5 6 7
1
1 2 3 4 5 6 7 8 9

for i in range(1, 6): for i in range(5, 0, -1):


for j in range(5-i): for j in range(5-i):
print(' ', end = ' ') print(' ', end = ' ')
Ans: Ans:
for j in range(1, i*2): for j in range(1, i*2):
print(j, end = ' ') print(j, end = ' ')
print() print()

1 0 1 0 1 0 1 0 1
1
1 0 1 0 1 0 1
1 0 1
(65) (66) 1 0 1 0 1
1 0 1 0 1
1 0 1
1 0 1 0 1 0 1
1
1 0 1 0 1 0 1 0 1

for i in range(1, 6): for i in range(5, 0, -1):


for j in range(5-i): for j in range(5-i):
print(' ', end = ' ') print(' ', end = ' ')
for j in range(1, i*2): for j in range(1, i*2):
Ans: if j%2 != 0: Ans: if j%2 != 0:
print(1, end = ' ') print(1, end = ' ')
else: else:
print(0, end = ' ') print(0, end = ' ')
print() print()

1 1 1 1 1 1 1 1 1
1
0 0 0 0 0 0 0
0 0 0
(67) (68) 1 1 1 1 1
1 1 1 1 1
0 0 0
0 0 0 0 0 0 0
1
1 1 1 1 1 1 1 1 1

for i in range(1, 6): for i in range(5, 0, -1):


for j in range(5-i): for j in range(5-i):
print(' ', end = ' ') print(' ', end = ' ')
for j in range(1, i*2): for j in range(1, i*2):
Ans: if i%2 != 0: Ans: if i%2 != 0:
print(1, end = ' ') print(1, end = ' ')
else: else:
print(0, end = ' ') print(0, end = ' ')
print() print()
Effective Computer Science 35

* * # * # * # * # *
* # * * # * # * # *
(69) * # * # * (70) * # * # *
* # * # * # * * # *
* # * # * # * # * *

for i in range(1, 6): for i in range(5, 0, -1):


for j in range(5-i): for j in range(5-i):
print(' ', end = ' ') print(' ', end = ' ')
for j in range(1, i*2): for j in range(1, i*2):
Ans: if j%2 != 0: Ans: if j%2 != 0:
print('*', end = ' ') print('*', end = ' ')
else: else:
print('#', end = ' ') print('#', end = ' ')
print() print()

1 2 3 4 5 5 4 3 2 1 5 4 3 2 1 1 2 3 4 5
1 2 3 4 4 3 2 1 4 3 2 1 1 2 3 4
(71) 1 2 3 3 2 1 (72) 3 2 1 1 2 3
1 2 2 1 2 1 1 2
1 1 1 1

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


for j in range(1, i+1): for j in range(i, 0, -1):
print(j, end = ' ') print(j, end = ' ')
for j in range((5-i)*2): for j in range((5-i)*2):
Ans: Ans:
print(' ', end = ' ') print(' ', end = ' ')
for j in range(i, 0, -1): for j in range(1, i+1):
print(j, end = ' ') print(j, end = ' ')
print() print()

1 1 5 5
1 2 2 1 4 5 5 4
(73) 1 2 3 3 2 1 (74) 3 4 5 5 4 3
1 2 3 4 4 3 2 1 2 3 4 5 5 4 3 2
1 2 3 4 5 5 4 3 2 1 1 2 3 4 5 5 4 3 2 1

for i in range(1, 6): for i in range(5, 0, -1):


for j in range(1, i+1): for j in range(i, 6):
print(j, end = ' ') print(j, end = ' ')
for j in range((5-i)*2): for j in range((i-1)*2):
Ans: Ans:
print(' ', end = ' ') print(' ', end = ' ')
for j in range(i, 0, -1): for j in range(5, i-1, -1):
print(j, end = ' ') print(j, end = ' ')
print() print()

1 1 5 5
1 2 2 1 4 5 5 4
(75) 1 2 3 3 2 1 (76) 3 4 5 5 4 3
1 2 3 4 4 3 2 1 2 3 4 5 5 4 3 2
1 2 3 4 5 4 3 2 1 1 2 3 4 5 4 3 2 1
Effective Computer Science 36

for i in range(1, 6): for i in range(5, 0, -1):


for j in range(1, i+1): for j in range(i, 6):
print(j, end = ' ') print(j, end = ' ')
for j in range((5-i)*2-1): for j in range((i-1)*2-1):
print(' ', end = ' ') print(' ', end = ' ')
if i == 5: if i == 1:
Ans: Ans:
for j in range(i-1, 0, -1): for j in range(4,i-1,-1):
print(j, end = ' ') print(j, end = ' ')
else: else:
for j in range(i, 0, -1): for j in range(5,i-1,-1):
print(j, end = ' ') print(j, end = ' ')
print() print()

1 2 3 4 5 4 3 2 1 1 2 3 4 5 4 3 2 1
1 2 3 4 4 3 2 1 2 3 4 5 5 4 3 2
(77) 1 2 3 3 2 1 (78) 3 4 5 5 4 3
1 2 2 1 4 5 5 4
1 1 5 5

for i in range(5, 0, -1): for i in range(1, 6):


for j in range(1, i+1): for j in range(i, 6):
print(j, end = ' ') print(j, end = ' ')
for j in range((5-i)*2-1): for j in range((i-1)*2-1):
print(' ', end = ' ') print(' ', end = ' ')
if i == 5: if i == 1:
Ans: Ans:
for j in range(i-1, 0, -1): for j in range(4,i-1,-1):
print(j, end = ' ') print(j, end = ' ')
else: else:
for j in range(i, 0, -1): for j in range(5,i-1,-1):
print(j, end = ' ') print(j, end = ' ')
print() print()

1 1 5 5 5 5 5 5 5 5 5
2 2 2 2 4 4 4 4 4 4 4 4
(79) 3 3 3 3 3 3 (80) 3 3 3 3 3 3
4 4 4 4 4 4 4 4 2 2 2 2
5 5 5 5 5 5 5 5 5 1 1

for i in range(1, 6):


for j in range(i, 0, -1): for i in range(5, 0, -1):
print(i, end = ' ') for j in range(1, i+1):
for j in range((5-i)*2-1): print(i, end = ' ')
print(' ', end = ' ') for j in range((5-i)*2-1):
if i == 5: print(' ', end = ' ')
for j in range(i-1, 0, -1): if i == 5:
Ans: Ans:
print(i, end = ' ') for j in range(i-1,0,-1):
else: print(i, end = ' ')
for j in range(i, 0, -1): else:
print(i, end = ' ') for j in range(i, 0, -1):
print() print(i, end = ' ')
print()
Effective Computer Science 37

*
*
* *
* * * *
* * * * *
(81) * * (82) * * * * * * * * *
* * * * *
* * * * * * * * * * * *
* * *
*

for i in range(9):
for i in range(5): for j in range(9):
for j in range(9): if i+j==4 or j-i==4 or
if i+j==4 or j-i==4 or i==4: i+j==12 or i-j==4 or i==4 or
Ans: print('*', end = ' ') Ans: j==4:
else: print('*', end = ' ')
print(' ', end = ' ') else:
print() print(' ', end = ' ')
print()

* * * * * * * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
(83) * * * * * * * * * (84) * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * * * * * * *

for i in range(9):
for i in range(9): for j in range(9):
for j in range(9): if i==j or i+j==8 or i==0
if i==j or i+j==8 or i==4 or i==8 or j==0 or j==8:
or j==4: print('*', end = ' ')
Ans: Ans:
print('*', end = ' ') else:
else: print(' ', end = ' ')
print(' ', end = ' ') print()
print()

* * * * * * * * *
* * * *
* * * * * *
* * * * * *
* * * * *
(85) * * (86) * * *
* * * * * *
* * * * * *
* * * * * * * * * * * * *
* *
Effective Computer Science 38

for i in range(9): for i in range(9):


for j in range(9): for j in range(9):
if i==j or i+j==8 or i==0 if i==j or i+j==8 or j==0
or i==8: or j==8:
Ans: Ans:
print('*', end = ' ') print('*', end = ' ')
else: else:
print(' ', end = ' ') print(' ', end = ' ')
print() print()

2 1
2 4 1 3
(87) 2 4 6 (88) 1 3 5
2 4 6 8 1 3 5 7
2 4 6 8 10 1 3 5 7 9

for i in range(2, 11, 2): for i in range(1, 10, 2):


for j in range(2, i+1, 2): for j in range(1, i+1, 2):
Ans: Ans:
print(j, end = ' ') print(j, end = ' ')
print() print()

1 1
22 12
333 12
(89) (90)
333 123
333 123
------------n -------------------n

n = int(input('Enter a number : ')) n=int(input('Enter a number : '))


for i in range(1,n+1): for i in range(1,n+1):
for j in range(1, i+1): for j in range(1, i+1):
Ans: Ans:
for k in range(1, i+1): for k in range(1, i+1):
print(i, end = ' ') print(k, end = ' ')
print() print()

1 9 1
2 8 2
3 7 3
4 6 4
(91) 5 (92) 1 2 3 4 5 6 7 8 9
4 6 6
3 7 7
2 8 8
1 9 9

for i in range(1, 10):


for i in range(1, 10): for j in range(1, 10):
for j in range(1, 10): if i==5:
if i==j or i+j==10: print(j, end = ' ')
Ans: print(j, end = ' ') Ans: elif j==5:
else: print(i, end = ' ')
print(' ', end = ' ') else:
print() print(' ', end = ' ')
print()
Effective Computer Science 39
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 # 1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 1 # 0 1 0 0 0 0 0 0 0 1
1 1 1 1 1 1 # 0 0 1 0 0 0 0 0 0 0 1
1 1 1 1 1 # 0 0 0
1 0 0 0 0 0 0 0 1
(93) 1 1 1 1 # 0 0 0 0 (94)
1 0 0 0 0 0 0 0 1
1 1 1 # 0 0 0 0 0
1 0 0 0 0 0 0 0 1
1 1 # 0 0 0 0 0 0
1 # 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1
# 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1

for i in range(9):
for i in range(9):
for j in range(9):
for j in range(9):
if i+j==8:
if i==0 or i==8 or j==0
print('#', end = ' ')
or j==8:
elif i+j<=8:
print(1, end = ' ')
Ans: print(1, end = ' ') Ans:
else:
else:
print(0, end = ' ')
print(0, end = ' ')
print()
print()

1 2 3 4 5 4 3 2 1
# 1 1 1 1 1 1 1 # 1 2 3 4 3 2 1
0 # 1 1 1 1 1 # 0 1 2 3 2 1
0 0 # 1 1 1 # 0 0 1 2 1
0 0 0 # 1 # 0 0 0 1
(95) 0 0 0 0 # 0 0 0 0 (96) 1 2 1
0 0 0 # 1 # 0 0 0 1 2 3 2 1
0 0 # 1 1 1 # 0 0 1 2 3 4 3 2 1
0 # 1 1 1 1 1 # 0 1 2 3 4 5 4 3 2 1
# 1 1 1 1 1 1 1 #

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


for j in range(5-i):
print(' ', end = ' ')
for j in range(1, i+1):
print(j, end = ' ')
for i in range(9):
for j in range(i-1, 0,-1):
for j in range(9):
print(j, end = ' ')
if i==j or i+j==8:
print()
print('#', end = ' ')
elif i>j and i+j<8:
for i in range(2, 6):
Ans: print(0, end = ' ') Ans:
for j in range(5-i):
elif i<j and i+j>8:
print(' ', end = ' ')
print(0, end = ' ')
for j in range(1, i+1):
else:
print(j, end = ' ')
print(1, end = ' ')
for j in range(i-1, 0, -1):
print()
print(j, end = ' ')
print()
Effective Computer Science 40

1
1 1
1 2 1
1 2 2 1
1 2 3 2 1
1 2 3 3 2 1
1 2 3 4 3 2 1
1 2 3 4 4 3 2 1
(97) 1 2 3 4 5 4 3 2 1 (98)
1 2 3 4 5 4 3 2 1
1 2 3 4 3 2 1
1 2 3 4 4 3 2 1
1 2 3 2 1
1 2 3 3 2 1
1 2 1
1 2 2 1
1
1 1

for i in range(1, 6):


for j in range(1, i+1):
print(j, end = ' ')
for j in range((5-i)*2-1):
for i in range(1, 6): print(' ', end = ' ')
for j in range(5-i): if i == 5:
print(' ', end = ' ') for j in range(i-1,0,-1):
for j in range(1, i+1): print(j, end = ' ')
print(j, end = ' ') else:
for j in range(i-1, 0, -1): for j in range(i, 0, -1):
print(j, end = ' ') print(j, end = ' ')
print() print()
Ans: Ans:
for i in range(4, 0, -1): for i in range(4, 0, -1):
for j in range(5-i): for j in range(1, i+1):
print(' ', end = ' ') print(j, end = ' ')
for j in range(1, i+1): for j in range((5-i)*2-1):
print(j, end = ' ') print(' ', end = ' ')
for j in range(i-1, 0,-1): if i == 5:
print(j, end = ' ') for j in range(i-1,0,-1):
print() print(j, end = ' ')
else:
for j in range(i, 0, -1):
print(j, end = ' ')
print()

1 A B C D E ABCDE5
1 2 A B C D ABCD45
(99) 1 2 3 A B C (100) ABC345
1 2 3 4 A B AB2345
1 2 3 4 5 A A12345

i = 5
i = 1 j = 5
j = 5 while i>=1 and j>=1:
while i<=5 and j>=1: for k in range(5-i):
for k in range(1, i+1): print(' ', end = ' ')
Ans: print(k, end = ' ') Ans: for k in range(1, i+1):
for k in range(1, j+1): print(chr(64+k), end=' ')
print(chr(64+k), end = ' ') for k in range(j, 6):
print() print(k, end = ' ')
i+=1 print()
j-=1 i-=1
j-=1
Effective Computer Science 41

Chapter : 7
Strings

 Unsolved Problems
1) Write a program in Python to read a string from a user and count how many vowels are present in the string.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
x = 0 x = 0
for i in a: for i in range(len(a)):
if i in 'AEIOUaeiou': if a[i] in 'AEIOUaeiou':
x+=1 x+=1
print('Number of vowels are', x) print('Number of vowels are', x)

2) Write a program in Python to read a string from a user and count how many special characters (non-
alphanumeric) are present in the string.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
x = 0 x = 0
for i in a: for i in range(len(a)):
if not i.isalnum(): if not a[i].isalnum():
x+=1 x+=1
print('No.of special characters are', x) print('No.of special characters are', x)

3) Write a program in Python to read a string from a user and display the string after replacing upper case vowel
with ‘@’ and lower case vowel with ‘#’.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = '' b = ''
for i in a: for i in range(len(a)):
if i in 'AEIOU': if a[i] in 'AEIOU':
i = '@' b = b + '@'
elif i in 'aeiou': elif a[i] in 'aeiou':
i = '#' b = b + '#'
b = b + i else:
print('Original String :') b = b + a[i]
print(a) print('Original String :')
print('Updated String :') print(a)
print(b) print('Updated String :')
print(b)

4) Write a program in Python to read a string from a user and delete all the vowels from the string.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = '' b = ''
for i in a: for i in range(len(a)):
if i not in 'AEIOUaeiou': if a[i] not in 'AEIOUaeiou':
b = b + i b = b + a[i]
print('Original String :') print('Original String :')
print(a) print(a)
print('Updated String :') print('Updated String :')
print(b) print(b)
Effective Computer Science 42
5) Write a program in Python to read a phone number of 10 digits and two dashes from a user, with dashes after
the area code and the next three numbers. For example, 021-334-2543 is a legal input. Display whether the
phone number valid and is in valid format or not.
Ans:
a = input('Enter a Telephone No. in nnn-nnn-nnnn format : ')
if a[:3].isdigit() and a[3]=='-' and a[4:7].isdigit() and a[7]=='-' and a[8:].isdigit() and len(a)==12:
print('Telephone number has a valid format')
else:
print('Telephone number has not a valid format')

6) Write a program in Python to read a string from a user and extract all the digits from the string. If there are digits
then collect the digits together and also calculate their sum. Display the original string, collection of digits and
their sum.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = '' b = ''
s = 0 s = 0
for i in a: for i in range(len(a)):
if i.isdigit(): if a[i].isdigit():
b = b + i b = b + a[i]
s = s + int(i) s = s + int(a[i])
print('Original String :') print('Original String :')
print(a) print(a)
print('Collected Digits :') print('Collected Digits :')
print(b) print(b)
print('Sum of the Digits : ', s) print('Sum of the Digits : ', s)

7) Write a program in Python to read a string from a user and display it by changing it character to its next 2
character with change in its case.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = '' b = ''
s = 0 s = 0
for i in a: for i in range(len(a)):
if i=='Y': if a[i]=='Y':
b = b + 'a' b = b + 'a'
elif i=='Z': elif a[i]=='Z':
b = b + 'b' b = b + 'b'
elif i=='y': elif a[i]=='y':
b = b + 'A' b = b + 'A'
elif i=='z': elif a[i]=='z':
b = b + 'B' b = b + 'B'
elif i.isupper(): elif a[i].isupper():
b = b + chr(ord(i)+34) b = b + chr(ord(a[i])+34)
elif i.islower(): elif a[i].islower():
b = b + chr(ord(i)-30) b = b + chr(ord(a[i])-30)
else: else:
b = b + i b = b + a[i]

print('Original String :') print('Original String :')


print(a) print(a)
print('New String :') print('New String :')
print(b) print(b)

8) Write a program in Python to read a string from a user and display it in reverse order.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = '' b = a[::-1]
Effective Computer Science 43
for i in range(len(a)-1, -1, -1): print('Original String :')
b = b + a[i] print(a)
print('New String :')
print('Original String :') print(b)
print(a)
print('New String :')
print(b)

9) Write a program in Python to read a string and display the longest substring of the given string having just the
consonants.
Ans:
a = input('Enter a string : ')
b = a.split()
L = b[0]
for i in range(1, len(b)):
r = False
for j in b[i]:
if j in 'AEIOUaeiou':
r = True
break
if r == False:
if len(b[i]) > len(L):
L = b[i]

print('Original String :')


print(a)
print('Longest Sub String having consonent only :')
print(L)

10) Write a program in Python to read email-id of a person in the form of a string and ensures that it belongs to
domain @gmail.com
Ans:
a = input('Enter an email id: ')
if a[-10:] == '@gmail.com':
print('It is a valid email id')
else:
print('It is not a valid email id')

11) Write a program in Python to read a string from a user and shift all the vowels of the string towards left hand
side and other characters towards right hand side.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = '' b = ''
c = '' c = ''
for i in a: for i in range(len(a)):
if i in 'AEIOUaeiou': if a[i] in 'AEIOUaeiou':
b = b + i b = b + a[i]
else: else:
c = c + i c = c + a[i]
d = b + c d = b + c
print('Original String :') print('Original String :')
print(a) print(a)
print('New String :') print('New String :')
print(d) print(d)

12) Write a program in Python to read a string from a user and display those words which are having length of more
than 3 characters.
Effective Computer Science 44
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
print('Original String :') print('Original String :')
print(a) print(a)
print('Words more than 3 length are :') print('Words more than 3 length are :')
for i in b: for i in range(len(b)):
if len(i) > 3: if len(b[i]) > 3:
print(i, end = ' ') print(b[i], end = ' ')

13) Write a program in Python to read a string from a user and count how many words are present which starts with
an uppercase character.
Ans:

a = input('Enter a string : ') a = input('Enter a string : ')


b = a.split() b = a.split()
print('Original String :') print('Original String :')
print(a) print(a)
print('Words starts with uppercase :') print('Words starts with uppercase :')
x = 0 x = 0
for i in b: for i in range(len(b)):
if i[0].isupper(): if b[i][0].isupper():
print(i, end = ' ') print(b[i], end = ' ')
x+=1 x+=1
print('\nNo. of Words such are :', x) print('\nNo. of Words such are :', x)

14) Write a program in Python to read a string from a user and count how many words are present which ends with
‘e’ or ‘i’.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
print('Original String :') print('Original String :')
print(a) print(a)
print('Words end with e or i :') print('Words end with e or i :')
x = 0 x = 0
for i in b: for i in range(len(b)):
if i[-1] in 'ei': if b[i][-1] in 'ei':
print(i, end = ' ') print(b[i], end = ' ')
x+=1 x+=1
print('\nNo. of Words such are :', x) print('\nNo. of Words such are :', x)

15) Write a program in Python to read a string from a user and display those words which end with a vowel.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
print('Original String :') print('Original String :')
print(a) print(a)
print('Words end with e or i :') print('Words end with a vowel are :')
x = 0 x = 0
for i in b: for i in range(len(b)):
if i[-1] in 'AEIOUaeiou': if b[i][-1] in 'AEIOUaeiou':
print(i, end = ' ') print(b[i], end = ' ')
x+=1 x+=1
print('\nNo. of Words such are :', x) print('\nNo. of Words such are :', x)

16) Write a program in Python to read a string from a user and convert the string after removing first character of
each word. Display the converted string.
Effective Computer Science 45
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
c = '' c = ''
for i in b: for i in range(len(b)):
c = c + i[1:] + ' ' c = c + b[i][1:] + ' '
print('Original String :') print('Original String :')
print(a) print(a)
print('New String : ') print('New String : ')
print(c) print(c)

17) Write a program in Python to read a string from a user and display in the following pattern. For example, if given
string is “ Car Is White ” then the output should be “raC sI etihW”.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
c = '' c = ''
for i in b: for i in range(len(b)):
c = c + i[::-1] + ' ' c = c + b[i][::-1] + ' '
print('Original String :') print('Original String :')
print(a) print(a)
print('New String : ') print('New String : ')
print(c) print(c)

18) Write a program in Python to read a string from a user and display in the following pattern. For example, if given
string is “ Car Is White ” then the output should be “White Is Car”.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
b = b[::-1] c = ''
b = ' '.join(b) for i in range(len(b)-1, -1, -1):
print('Original String :') c = c + b[i] + ' '
print(a) print('Original String :')
print('New String : ') print(a)
print(b) print('New String : ')
print(c)

19) Write a program in Python to read a string from a user and display in the following pattern. For example, if given
string is “Pundit Jawahar Lal Nehru” then the output should be “P. J. L. Nehru”.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
c = '' c = ''
for i in b[:-1]: for i in range(len(b)-1):
c = c + i[0] + '.' c = c + b[i][0] + '.'
c = c + b[-1] c = c + b[-1]
print('Original String :') print('Original String :')
print(a) print(a)
print('New String : ') print('New String : ')
print(c) print(c)

20) Write a program in Python to read a string from a user and arrange the string in the lexicological order
(dictionary order) of the words present the string.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
Effective Computer Science 46
for i in range(len(b)-1): b.sort()
for j in range(len(b)-1-i): b = ' '.join(b)
if b[j] > b[j+1]: print('Original String :')
b[j], b[j+1] = b[j+1], b[j] print(a)
print('Output String :')
b = ' '.join(b) print(b)
print('Original String :')
print(a)
print('Output String :')
print(b)

21) Write a program in Python to read a string from a user and display those words of the given string which do not
contain any vowel.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
print('Original String :') print('Original String :')
print(a) print(a)
print('Words not having any vowel :') print('Words not having any vowel :')
for i in b: for i in range(len(b)):
x = False x = False
for j in i: for j in range(len(b[i])):
if j in 'AEIOUaeiou': if b[i][j] in 'AEIOUaeiou':
x = True x = True
break break
if x == False: if x == False:
print(i, end = ' ') print(b[i], end = ' ')

22) Write a program in Python to read a string from a user and display those works of the given string which contain
more than two vowels.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
print('Original String :') print('Original String :')
print(a) print(a)
print('Words with more than 2 vowel :') print('Words not having any vowel :')
for i in b: for i in range(len(b)):
x = 0 x = 0
for j in i: for j in range(len(b[i])):
if j in 'AEIOUaeiou': if b[i][j] in 'AEIOUaeiou':
x+=1 x+=1
if x >= 2: if x >= 2:
print(i, end = ' ') print(b[i], end = ' ')

23) Write a program in Python to read a string from a user and shift all the vowels towards left hand side and other
characters towards right hand side in each word.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
print('Original String :') print('Original String :')
print(a) print(a)
print('Output String :') print('Output String :')
c = '' c = ''
for i in b: for i in range(len(b)):
x = '' x = ''
y = '' y = ''
Effective Computer Science 47
for j in i: for j in range(len(b[i])):
if j in 'AEIOUaeiou': if b[i][j] in 'AEIOUaeiou':
x = x + j x = x + b[i][j]
else: else:
y = y + j y = y + b[i][j]
c = c + x + y + ' ' c = c + x + y + ' '
print(c) print(c)

24) Write a program in Python to read a string from a user and count frequency of each alphabet present in the
string i.e., you have to display ‘A’/‘a’ to ‘Z’/’z’ alphabets appear how many times in the given string.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
print('Original String :') print('Original String :')
print(a) print(a)
print('Output is :') print('Output is :')
b = '' b = ''
for i in range(26): for i in range(26):
x = 0 x = 0
for j in a: for j in range(len(a)):
if j==chr(65+i) or j==chr(97+i): if a[j]==chr(65+i) or a[j]==chr(97+i):
x+=1 x+=1
print(chr(65+i), 'appeared', x, 'times') print(chr(65+i), 'appeared', x, 'times')

25) Write a program in Python to read a string from a user and display frequency of each word of the given string.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
print('Original String :') print('Original String :')
print(a) print(a)
print('Output String :') print('Output String :')
c = () c = ()
for i in b: for i in range(len(b)):
if i not in c: if b[i] not in c:
c = c + (i,) c = c + (b[i],)
n = b.count(i) n = b.count(b[i])
print(i, 'appeared', n, 'times') print(b[i],'appeared',n,times')

26) Write a program in Python to read a string from a user and display longest and shortest word present of the
given string.
Ans:
a = input('Enter a string : ')
b = a.split()
L = b[0]
S = b[0]
for i in range(1, len(b)):
if len(b[i]) > len(L):
L = b[i]
if len(b[i]) < len(S):
S = b[i]

print('Original String :')


print(a)
print('Longest word in the string :', L)
print('Smallest word in the string :', S)

27) Write a program in Python to read a string from a user and display the words which appear first and last in the
dictionary from the given dictionary.
Effective Computer Science 48
Ans:
a = input('Enter a string : ')
b = a.split()
L = b[0]
S = b[0]
for i in range(1, len(b)):
if b[i] > L:
L = b[i]
if b[i] < S:
S = b[i]

print('Original String :')


print(a)
print('Word appear first in the dictionary :', S)
print('Word appear last in the dictionary :', L)

28) Write a program in Python to read a string and sub-string from a user and display how many times the substring
occurs in the string.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = input('Enter a word : ') b = input('Enter a word : ')
c = a.split() c = a.split()
x = 0 x = 0
for i in c: for i in range(len(c)):
if i == b: if c[i] == b:
x+=1 x+=1
print(b, 'appeared', x, 'times') print(b, 'appeared', x, 'times')

29) Write a program in Python to read a string from a user and display length of each word.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
for i in b: for i in range(len(b)):
print(len(i), end = ' ') print(len(b[i]), end = ' ')

30) Write a program in Python to read a string from a user and display how many vowels are present in each word.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
print('Original String :') print('Original String :')
print(a) print(a)
print('Words and number of vowels:') print('Words not having any vowel :')
for i in b: for i in range(len(b)):
x = 0 x = 0
for j in i: for j in range(len(b[i])):
if j in 'AEIOUaeiou': if b[i][j] in 'AEIOUaeiou':
x+=1 x+=1
print(i, x, sep = ', ') print(b[i], x, sep = ', ')

31) Write a program in Python to read a string from a user and display the words which are having consecutive same
characters. For example, the word success or good.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
print('Original String :') print('Original String :')
print(a) print(a)
print('Output Words:') print('Words not having any vowel :')
Effective Computer Science 49
for i in b: for i in range(len(b)):
for j in range(len(i)-1): for j in range(len(b[i])-1):
if i[j] == i[j+1]: if b[i][j] == b[i][j+1]:
print(i, end = ' ') print(b[i], end = ' ')
break break

32) Write a program in Python to read a string from a user and display the palindrome words present in the string.
For example, the word madam or malayalam.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
print('Original String :') print('Original String :')
print(a) print(a)
print('Output Words:') print('Output Words:')
for i in b: for i in range( len(b)):
if i == i[::-1]: if b[i] == b[i][::-1]:
print(i, end = ' ') print(b[i], end = ' ')

33) Write a program in Python to read a string from a user and display the string after replacing the word ‘school’
with ‘college’.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() print('Original String :')
print('Original String :') print(a)
print(a) b = a.replace('school', 'college')
c = '' print('Output String :')
for i in b: print(b)
if i == 'school':
c = c + 'college' + ' '
else:
c = c + i + ' '
print('Output String :')
print(c)

34) Write a program in Python to read a string from a user and display the string after swapping first character of
each word with last character of the word.
Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
b = a.split() b = a.split()
print('Original String :') print('Original String :')
print(a) print(a)
c = '' c = ''
for i in b: for i in range(len(b)):
c = c + i[-1] + i[1:-1] + i[0] + ' ' c=c+b[i][-1]+b[i][1:-1]+b[i][0]+' '
print('Output String :') print('Output String :')
print(c) print(c)

35) Write a program in Python to read a string from a user and display in the following pattern. For example, if given
string is “ PYTHON ” then the output should be –

(1) P (2) PYTHON


PY YTHON
PYT THON
PYTH HON
PYTHO ON
PYTHON N
Effective Computer Science 50
Ans: Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
for i in range(len(a)): for i in range(len(a)):
for j in range(i+1): for j in range(i, len(a)):
print(a[j], end = ' ') print(a[j], end = ' ')
print() print()

(3) N (4) PYTHON


ON PYTHO
HON PYTH
THON PYT
YTHON PY
PYTHON P

Ans: Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
for i in range(len(a)-1, -1, -1): for i in range(len(a)-1, -1, -1):
for j in range(i, len(a)): for j in range(0, i+1):
print(a[j], end = ' ') print(a[j], end = ' ')
print() print()

(5) PYTHONOHTYP (6) P


PYTHOHTYP PYP
PYTHTYP PYTYP
PYTYP PYTHTYP
PYP PYTHOHTYP
P PYTHONOHTYP

Ans: Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
for i in range(len(a)-1, -1, -1): for i in range(len(a)):
for j in range(len(a)-1-i): for j in range(len(a)-1-i):
print(' ', end = ' ') print(' ', end = ' ')
for j in range(0, i+1): for j in range(0, i+1):
print(a[j], end = ' ') print(a[j], end = ' ')
for j in range(i-1, -1, -1): for j in range(i-1, -1, -1):
print(a[j], end = ' ') print(a[j], end = ' ')
print() print()

(7) N (8) PYTHONOHTYP


ONO YTHONOHTY
HONOH THONOHT
THONOHT HONOH
YTHONOHTY ONO
PYTHONOHTYP N

Ans: Ans:
a = input('Enter a string : ') a = input('Enter a string : ')
for i in range(len(a)-1, -1, -1): for i in range(len(a)):
for j in range(i): for j in range(i):
print(' ', end = ' ') print(' ', end = ' ')
for j in range(i, len(a)): for j in range(i, len(a)):
print(a[j], end = ' ') print(a[j], end = ' ')
for j in range(len(a)-2, i-1, -1): for j in range(len(a)-2, i-1,-1):
print(a[j], end = ' ') print(a[j], end = ' ')
print() print()
Effective Computer Science 51

Chapter : 8
Tuples

 Unsolved Problems

1) Create the following tuples using a for loop :


a. The tuple containing integers from 0 to 100.
Ans:
a = ()
for i in range(101):
a = a + (i,)
print(a)

b. The tuple containing the squares of the integers 1 to 50.


Ans:
a = ()
for i in range(1, 51):
a = a + (i*i,)
print(a)

c. The tuple containing the value of the integers from 1 to 10 by raising the power of the integer by itself. For
example, the tuple formed will contain the value of (11, 22, 33, 44, ….., 1010).
Ans:
a = ()
for i in range(1, 51):
a = a + (i**i,)
print(a)

d. The tuple ( ‘a’, ‘b’, ‘c’, …………) that ends with 26 copies of the letters z.
Ans:
a = ()
for i in range(26):
a = a + (chr(97+i),)
print(a)

e. The tuple ( ‘Aa’, ‘Bb’, ‘Cc’, …………) that ends with 26 copies of the letters z.
Ans:
a = ()
for i in range(26):
a = a + (chr(65+i)+chr(97+i),)
print(a)

f. The tuple ( ‘a’, ‘bb’, ‘ccc’, …………) that ends with 26 copies of the letters z.
Ans:
a = ()
for i in range(1, 27):
a = a + (chr(96+i)*i,)
print(a)

g. The tuple ( 1, 22, 333, 4444, …………) that ends with 9 as 9 times.
Ans: a = ()
for i in range(1, 10):
a = a + (int(str(i)*i),)
print(a)
Effective Computer Science 52
h. The tuple ( 1, 12, 123, 1234, ……….., 123456789)
Ans:
a = ()
for i in range(1, 10):
b = ''
for j in range(1, i+1):
b = b + str(j)
a = a + (int(b),)
print(a)

i. The tuple ( (1), (1,2), (1,2,3), (1,2,3,4), …………) that ends with 9 as 1,2,3,4,5,6,7,8,9 times.
Ans:
a = ()
for i in range(1, 10):
b = ()
for j in range(1, i+1):
b = b + (j,)
a = a + (b,)
print(a)

j. The tuple ( ‘a’, ‘ab’, ‘abc’, …………) that ends with 26 copies of the letters z.
Ans:
a = ()
for i in range(26):
b = ''
for j in range(i+1):
b = b + chr(97+j)
a = a + (b,)
print(a)

k. The tuple containing n number of terms of a Fibonaccii series.


Ans:
n = int(input('Enter number of terms : '))
a = (0, 1)
for i in range(n-2):
a = a + (a[i]+a[i+1],)
print(a)

2) Write a program in Python to read a tuple of n number of integers from a user and display how many even and
odd elements are present in the tuple.
Ans:
n=int(input('Enter number of elements:')) n=int(input('Enter number of elements:'))
a = () a = ()
for i in range(n): for i in range(n):
ele=int(input('Enter an element : ')) ele=int(input('Enter an element : '))
a = a + (ele,) a = a + (ele,)
print('Original Tuple :') print('Original Tuple :')
print(a) print(a)
x = y = 0 x = y = 0
for i in a: for i in range(len(a)):
if i%2 == 0: if a[i]%2 == 0:
x+=1 x+=1
else: else:
y+=1 y+=1
print('Number of even elements : ', x) print('Number of even elements : ', x)
print('Number of odd elements : ', y) print('Number of odd elements : ', y)

3) Write a program in Python to read a tuple of n number of integers from a user and display sum of even and odd
elements are present in the tuple.
Effective Computer Science 53
Ans:
n=int(input('Enter number of elements:'))
a = () n=int(input('Enter number of elements:'))
for i in range(n): a = ()
ele=int(input('Enter an element : ')) for i in range(n):
a = a + (ele,) ele=int(input('Enter an element : '))
print('Original Tuple :') a = a + (ele,)
print(a) print('Original Tuple :')
x = y = 0 print(a)
for i in a: x = y = 0
if i%2 == 0: for i in range(len(a)):
x = x + i if a[i]%2 == 0:
else: x = x + a[i]
y = y + i else:
print('Sum of even elements : ', x) y = y + a[i]
print('Sum of odd elements : ', y) print('Sum of even elements : ', x)
print('Sum of odd elements : ', y)

4) Write a program in Python to read a tuple of n number of integers from a user and display the tuple after dividing
even elements by 2 and multiplying odd elements by 3.
Ans:
n=int(input('Enter number of elements:')) n=int(input('Enter number of elements:'))
a = () a = ()
for i in range(n): for i in range(n):
ele=int(input('Enter an element : ')) ele=int(input('Enter an element : '))
a = a + (ele,) a = a + (ele,)
print('Original Tuple :') print('Original Tuple :')
print(a) print(a)
b = () b = ()
for i in a: for i in range(len(a)):
if i%2 == 0: if a[i]%2 == 0:
b = b + (i//2,) b = b + (a[i]//2,)
else: else:
b = b + (i*3,) b = b + (a[i]*3,)
print('Output Tuple :') print('Output Tuple :')
print(b) print(b)

5) Write a program in Python to read a tuple of n number of integers from a user and display those elements which
are ended with 7.
Ans:
n=int(input('Enter number of elements:')) n=int(input('Enter number of elements:'))
a = () a = ()
for i in range(n): for i in range(n):
ele=int(input('Enter an element : ')) ele=int(input('Enter an element : '))
a = a + (ele,) a = a + (ele,)
print('Original Tuple :') print('Original Tuple :')
print(a) print(a)
print('Elements ended with 7:') print('Elements ended with 7:')
for i in a: for i in range(len(a)):
if i%10 == 7: if a[i]%10 == 7:
print(i, end = ' ') print(a[i], end = ' ')

6) Write a program in Python to read a tuple of n number of integers from a user and display average of the
elements divisible by 5.
Ans:
n=int(input('Enter number of elements:')) n=int(input('Enter number of elements:'))
a = () a = ()
Effective Computer Science 54
for i in range(n): for i in range(n):
ele=int(input('Enter an element : ')) ele=int(input('Enter an element : '))
a = a + (ele,) a = a + (ele,)
print('Original Tuple :') print('Original Tuple :')
print(a) print(a)
s = 0 s = 0
x = 0 x = 0
for i in a: for i in range(len(a)):
if i%5 == 0: if a[i]%5 == 0:
s = s + i s = s + a[i]
x+=1 x+=1
print('Average = ', s/x) print('Average = ', s/x)

7) Write a program in Python to read a tuple of n number of integers from a user and display prime elements
present in the tuple.
Ans:
n=int(input('Enter number of elements:')) n=int(input('Enter number of elements:'))
a = () a = ()
for i in range(n): for i in range(n):
ele=int(input('Enter an element : ')) ele=int(input('Enter an element : '))
a = a + (ele,) a = a + (ele,)
print('Original Tuple :') print('Original Tuple :')
print(a) print(a)
print('Prime elements :') print('Prime elements :')
for i in a: for i in range(len(a)):
x = 0 x = 0
for j in range(1, i//2+1): for j in range(1, a[i]//2+1):
if i%j ==0 : if a[i]%j ==0 :
x+=1 x+=1
if x == 1: if x == 1:
print(i, end = ' ') print(a[i], end = ' ')

8) Write a program in Python to read a tuple of n number of integers from a user and display the tuple after deleting
odd elements from the tuple.
Ans:
n=int(input('Enter number of elements:')) n=int(input('Enter number of elements:'))
a = () a = ()
for i in range(n): for i in range(n):
ele=int(input('Enter an element : ')) ele=int(input('Enter an element : '))
a = a + (ele,) a = a + (ele,)
print('Original Tuple :') print('Original Tuple :')
print(a) print(a)
b = () b = ()
for i in a: for i in range(len(a)):
if i%2 == 0: if a[i]%2 == 0:
b = b + (i,) b = b + (a[i],)
print('Output Tuple :') print('Output Tuple :')
print(b) print(b)

9) Write a program in Python to read a tuple of n number of integers from a user and display the tuple after
replacing odd elements by 0.
Ans:
n=int(input('Enter number of elements:')) n=int(input('Enter number of elements:'))
a = () a = ()
for i in range(n): for i in range(n):
ele=int(input('Enter an element : ')) ele=int(input('Enter an element : '))
a = a + (ele,) a = a + (ele,)
print('Original Tuple :') print('Original Tuple :')
print(a) print(a)
Effective Computer Science 55
b = () b = ()
for i in a: for i in range(len(a)):
if i%2 == 0: if a[i]%2 == 0:
b = b + (i,) b = b + (a[i],)
else: else:
b = b + (0,) b = b + (0,)
print('Output Tuple :') print('Output Tuple :')
print(b) print(b)

10) Write a program in Python to read a tuple of n number of country names given by a user. Display the created
tuple and count how many countries are their which contains ‘A’ as first letter in their name.
Ans:
n=int(input('Enter number of elements:')) n=int(input('Enter number of elements:'))
a = () a = ()
for i in range(n): for i in range(n):
ele=input('Enter a Country Name : ') ele=input('Enter a Country Name : ')
a = a + (ele,) a = a + (ele,)
print('Original Tuple :') print('Original Tuple :')
print(a) print(a)
x = 0 x = 0
for i in a: for i in range(len(a)):
if i[0] == 'A': if a[i][0] == 'A':
x+=1 x+=1
print('Number of countries :', x) print('Number of countries :', x)

11) Write a program in Python to read a tuple of n number of name of the students given by a user. Display the
created tuple and count how many names are their which are having length of the name more than 5 characters.
Ans:
n=int(input('Enter number of elements:')) n=int(input('Enter number of elements:'))
a = () a = ()
for i in range(n): for i in range(n):
ele=input('Enter an integer: ') ele=input('Enter an integer: ')
a = a + (ele,) a = a + (ele,)
print('Original Tuple :') print('Original Tuple :')
print(a) print(a)
x = 0 x = 0
for i in a: for i in range(len(a)):
if len(i) > 4: if len(a[i]) > 4:
x+=1 x+=1
print('Number of names :', x) print('Number of names :', x)

12) Write a program in Python to read a tuple of n number of integers from a user and display the largest element of
the tuple with its position in the Tuple.
Ans: n=int(input('Enter number of elements:'))
a = ()
for i in range(n):
ele=int(input('Enter an integer : '))
a = a + (ele,)
print('Original Tuple :')
print(a)
n = a[0]
p = 0
for i in range(1, len(a)):
if a[i] > n:
n = a[i]
p = i
print('Largest element of the Tuple :', n)
print('Position of Largest element of the Tuple :', p+1)
Effective Computer Science 56
13) Write a program in Python to read a tuple of n number of integers from a user and display the smallest element
of the tuple with its position in the Tuple.
Ans:
n=int(input('Enter number of elements:'))
a = ()
for i in range(n):
ele=int(input('Enter an integer: '))
a = a + (ele,)
print('Original Tuple :')
print(a)
n = a[0]
p = 0
for i in range(1, len(a)):
if a[i] < n:
n = a[i]
p = i
print('Smallest element of the Tuple :', n)
print('Position of Smallest element of the Tuple :', p+1)

14) Write a program in Python to read a tuple of n number of integers from a user and display the first largest and
second largest element of the tuple.
Ans:
n=int(input('Enter number of elements:'))
a = ()
for i in range(n):
ele=int(input('Enter an integer: '))
a = a + (ele,)
print('Original Tuple :')
print(a)
L = a[0]
S = a[0]
for i in range(1, len(a)):
if a[i] > L:
L = a[i]
for i in range(1, len(a)):
if a[i] > S and a[i] < L:
S = a[i]
print('Largest element of the Tuple :', L)
print('Second Largest element of the Tuple :', S)

15) Write a program in Python to read a tuple of n number of integers from a user and create two tuples containing
unique and duplicate of the given tuple.
Ans:
n=int(input('Enter number of elements:')) n=int(input('Enter number of elements:'))
a = () a = ()
for i in range(n): for i in range(n):
ele=int(input('Enter an integer: ')) ele=int(input('Enter an integer: '))
a = a + (ele,) a = a + (ele,)
print('Original Tuple :') print('Original Tuple :')
print(a) print(a)
u = () u = ()
d = () d = ()
for i in a: for i in range(len(a)):
if i not in u and i not in d: if a[i] not in u and a[i] not in d:
n = a.count(i) n = a.count(a[i])
if n == 1: if n == 1:
u = u + (i,) u = u + (a[i],)
else: else:
d = d + (i,) d = d + (a[i],)
Effective Computer Science 57
print('Tuple of Unique Elements :', u) print('Tuple of Unique Elements :', u)
print('Tuple of Duplicate Elements :', d) print('Tuple of Duplicate Elements :', d)

16) Write a program in Python to read a tuple of n number of integers from a user and check whether all the
elements of the given tuple are present in ascending order or not.
Ans:
n=int(input('Enter number of elements:'))
a = ()
for i in range(n):
ele=int(input('Enter an integer : '))
a = a + (ele,)
print('Original Tuple :')
print(a)

x = 0
for i in range(len(a)-1):
if a[i] <= a[i+1]:
x+=1

if x == n-1:
print('Tuple in ascending order')
else:
print('Tuple in ascending order')

17) Write a program in Python to read a tuple of n number of integers from a user and check whether elements of
first half of the tuple are present in ascending order and elements of second half of the tuple are present in
descending order or not.
Ans:
n=int(input('Enter number of elements:'))
a = ()
for i in range(n):
ele=int(input('Enter an integer: '))
a = a + (ele,)
print('Original Tuple :')
print(a)

x = 0
for i in range(len(a)//2-1):
if a[i] <= a[i+1]:
x+=1

y = 0
for i in range(len(a)//2, len(a)-1):
if a[i] >= a[i+1]:
y+=1

if x == n//2-1 and y == n//2-1:


print('Tuple in correct order')
else:
print('Tuple not in correct oreder')

18) Write a program in Python to read a tuple of the marks from 0 to 100 for n number of students from a user.
Create another tuple containing tuples as (Marks, Grade) after calculating the grade on the bases of the marks
present in the given tuple. For example, if given tuple of marks is (99, 44, 77, 11, 130, 88) then the resultant tuple
should be ((99, ‘A’), (44, ‘C’), (77, ‘B’), (11, ‘D’), (130, ‘N’), (88, ‘A’)). Grade will be calculated as follows:
Marks Grade
0 to 30 D
31 to 50 C
51 to 80 B
Effective Computer Science 58
More than 80 A
For invalid marks N
Ans:
n=int(input('Enter number of students:')) n=int(input('Enter number of students:'))
a = () a = ()
for i in range(n): for i in range(n):
ele=int(input('Enter marks : ')) ele=int(input('Enter marks : '))
a = a + (ele,) a = a + (ele,)
print('Tuple of Marks :') print('Tuple of Marks :')
print(a) print(a)

b = () b = ()
for i in a: for i in range(len(a)):
if i>=0 and i<=30: if a[i]>=0 and a[i]<=30:
b = b + ((i,'D'),) b = b + ((a[i],'D'),)
elif i>=31 and i<=50: elif a[i]>=31 and a[i]<=50:
b = b + ((i,'C'),) b = b + ((a[i],'C'),)
elif i>=51 and i<=80: elif a[i]>=51 and a[i]<=80:
b = b + ((i,'B'),) b = b + ((a[i],'B'),)
elif i>=81 and i<=100: elif a[i]>=81 and a[i]<=100:
b = b + ((i,'A'),) b = b + ((a[i],'A'),)
else: else:
b = b + ((i,'N'),) b = b + ((a[i],'N'),)

print('Output Tuple:') print('Output Tuple:')


print(b) print(b)

19) Given a tuple T(1, 2, “a”, “b”). There is a list L with some elements. Replace first four elements of list with all four
elements of the tuple in single statement. Print the list before and after the list is modified.
Ans:
L = eval(input('Enter a List : '))
T = (1, 2, 'a', 'b')
print('Original List : ')
print(L)

L[:4] = T

print('Updated List : ')


print(L)

20) Write a program in Python to read a tuple of n number of elements. Show that both contain same elements or
not.
Ans:
n=int(input('Enter size of the tuples:'))
a = ()
b = ()

print('Enter elements for first tuple:')


for i in range(n):
ele=int(input('Enter an element : '))
a = a + (ele,)

print('Enter elements for second tuple:')


for i in range(n):
ele=int(input('Enter an element : '))
b = b + (ele,)

print('First Tuple: ', a)


print('Second Tuple: ', b)
Effective Computer Science 59

x = 0
for i in a:
if i in b:
x+=1

if x == n:
print('Both tuples contains same elements')
else:
print('Both tuples contains different elements')

21) Write a program that create a tuple containing first n terms of Fibonacci series. (i) Write a program to receive the
index and return the corresponding value. (ii) Write a program that receives a Fibonacci term and return a
number telling which term it is.
Ans:
n = int(input('Enter number of terms : '))
a = (0, 1)
for i in range(n-2):
a = a + (a[i]+a[i+1],)

print('Fibonacci Series : ', a)

i = int(input('Enter index number : '))


if i < n:
print(a[i])
else:
print('Index out of range')

x = int(input('Enter term value : '))


if x in a:
print('It is term number : ', a.index(x)+1)
else:
print('Term not found')

22) Write a program in Python that interactively creates a nested tuple to store the marks in three subjects for five
students and display total marks of each student and average marks of all the students.
Ans:
a = ()
for i in range(5):
b = ()
for j in range(3):
n = int(input('Enter Marks : '))
b = b + (n,)
a = a + (b,)
print('Tuple of Marks : ')
print(a)

s = 0
for i in range(5):
t = 0
for j in a[i]:
t = t + j
print('Sum of the Tuple', i+1, 'is', t)
s = s + t

print('Average Marks of all the students is', s//5)

23) Write a program in Python to read two tuples of size m and n given by a user and merge them into a single tuple
in the following way –
Effective Computer Science 60
Suppose A(1, 2, 3) and B(5, 6, 7, 8) then the output C(1, 2, 3, 5, 6, 7, 8)
Ans:
m=int(input('Enter size of first tuples:'))
a = ()
print('Enter elements for first tuple:')
for i in range(m):
ele=int(input('Enter an element : '))
a = a + (ele,)

n=int(input('Enter size of first tuples:'))


b = ()
print('Enter elements for second tuple:')
for i in range(n):
ele=int(input('Enter an element : '))
b = b + (ele,)

print('First Tuple: ', a)


print('Second Tuple: ', b)

print('Merged tuple : ', a+b)

24) Write a program in Python to read a tuple of strings and display the largest and the smallest string present in the
tuple and also length of both the strings.
Ans:
n = int(input('Enter size of the tuples:'))
a = ()
print('Enter elements for tuple:')
for i in range(n):
ele=input('Enter a string : ')
a = a + (ele,)

print('Tuple of strings: ')

L = a[0]
S = a[0]
for i in range(1, len(a)):
if len(a[i]) > len(L):
L = a[i]
elif len(a[i]) < len(S):
S = a[i]

print('Longest String : ', L)


print('Length of Longest String : ', len(L))
print('Shortest String : ', S)
print('Length of Shortest String : ', len(S))

25) Write a program in Python to read a nested tuple of n elements from a user, each element of the tuple having
two integers values like ( (2, 5), (4, 8), (2, 6), (4, 1) ) and count the number of pairs such that both the values are
even.
Ans:
n = int(input('Enter Size of the Tuple : '))
a = ()
for i in range(n):
b = ()
for j in range(2):
ele = int(input('Enter an integer : '))
b = b + (ele,)
a = a + (b,)
print('Tuple of Integers : ')
Effective Computer Science 61
print(a)

x = 0
for i in a:
y = 0
for j in i:
if j%2 == 0:
y+=1
if y == 2:
x+=1

print('Number of tuples having both even elements are:', x)

26) Write a program in Python to read a nested tuple of n elements from a user, each sub-tuple has 3 numeric values.
Display those sub-tuples which is having sum of the elements more than 100.
Ans:
n = int(input('Enter Size of the tuple: '))
a = ()
for i in range(n):
b = ()
for j in range(3):
ele = int(input('Enter an integer : '))
b = b + (ele,)
a = a + (b,)
print('Tuple of Integers : ')
print(a)

for i in a:
s = 0
for j in i:
s = s + j
if s > 100:
print(i)

27) Write a program in Python to read a nested tuple of n elements from a user, each element can have m number of
values. Check how many tuples are there which are having three elements.
Ans:
n = int(input('Enter Size of the tuple: '))
a = ()
for i in range(n):
m = int(input('Enter size of inner tuple : '))
b = ()
for j in range(m):
ele = int(input('Enter an integer : '))
b = b + (ele,)
a = a + (b,)
print('Tuple of Integers : ')
print(a)

x = 0
for i in a:
if len(i) == 3:
x+=1

print('Number of tuples having three elements are ', x)

28) Write a program in Python to read a nested tuple of n elements from a user, each element can have m number of
values. Display the mean of all the tuples elements.
Effective Computer Science 62
Ans:
n = int(input('Enter Size of the tuple: '))
a = ()
for i in range(n):
m = int(input('Enter size of inner tuple : '))
b = ()
for j in range(m):
ele = int(input('Enter an integer : '))
b = b + (ele,)
a = a + (b,)
print('Tuple of Integers : ')
print(a)
for i in a:
s = 0
for j in i:
s = s + j
print('Mean of the tuple :', s//m)

29) Write a program in Python to read a nested tuple of n elements from a user, each element can have m number of
values. Display those inner tuples in which all the elements are even.
Ans:
n = int(input('Enter Size of the tuple: '))
a = ()
for i in range(n):
m = int(input('Enter size of inner tuple : '))
b = ()
for j in range(m):
ele = int(input('Enter an integer : '))
b = b + (ele,)
a = a + (b,)
print('Tuple of Integers : ')
print(a)

for i in a:
x = 0
for j in i:
if j%2 == 0:
x+=1
if x == len(i):
print(i)

30) Write a program in Python to read a nested tuple of n elements from a user, each element can have m number of
values. Display those inner tuples in which all the elements are equal.
Ans:
n = int(input('Enter Size of the tuple: '))
a = ()
for i in range(n):
m = int(input('Enter size of inner tuple : '))
b = ()
for j in range(m):
ele = int(input('Enter an integer : '))
b = b + (ele,)
a = a + (b,)
print('Tuple of Integers : ')
print(a)

for i in a:
x = 1
ele = i[0]
Effective Computer Science 63
for j in range(1, len(i)):
if ele == i[j]:
x+=1
if x == len(i):
print(i)

31) Write a program in Python to read a nested tuple of n elements from a user, each element contains 5 integers.
Display sum of integers present in each element.
Ans:
n = int(input('Enter Size of the tuple: '))
a = ()
for i in range(n):
b = ()
for j in range(5):
ele = int(input('Enter an integer : '))
b = b + (ele,)
a = a + (b,)
print('Tuple of Integers : ')
print(a)

for i in a:
s = 0
for j in i:
s = s + j
print(s)

32) Write a program in Python to read a string from a user and create a tuple of the length of each word of the given
string.
Ans:
a = input('Enter a string : ')
b = a.split()
t = ()
for i in b:
t = t + (len(i),)
print('String is : ', a)
print('Tuple of the length of each word is : ', t)

33) Write a program in Python to read a string from a user and create a tuple of those words which starts with a
vowel.
Ans:
a = input('Enter a string : ')
b = a.split()

t = ()
for i in b:
if i[0] in 'AEIOUaeiou':
t = t + (i,)

print('String is : ', a)
print('Tuple of the words is : ', t)

34) Write a program in Python to read a string from a user and create a tuple of those words which are having length
of more than 4 characters.
Ans:
a = input('Enter a string : ')
b = a.split()

t = ()
Effective Computer Science 64
for i in b:
if len(i) > 4:
t = t + (i,)

print('String is : ', a)
print('Tuple of the words is : ', t)

35) Write a program in Python to read a string from a user and create a tuple of the reverse of each words from the
given string.
Ans:
a = input('Enter a string : ')
b = a.split()

t = ()
for i in b:
t = t + (i[::-1],)

print('String is : ', a)
print('Tuple of the words is : ', t)

36) Write a program in Python to read a string from a user and create a tuple of those words which do not contain
any vowel.
Ans:
a = input('Enter a string : ')
b = a.split()

t = ()
for i in b:
f = 0
for j in i:
if j in 'AEIOUaeiou':
f = 1
break
if f == 0:
t = t + (i,)

print('String is : ', a)
print('Tuple of the words is : ', t)

37) Write a program in Python to read a tuple containing name of n number of students from a user. Display those
names which starts with ‘A’ or ‘M’.
Ans:
n = int(input('Enter number of students : '))
a = ()
for i in range(n):
ele = input('Enter a Name : ')
a = a + (ele,)
print('Tuple of Names :')
print(a)

print('Names starts with A or M are : ')


for i in a:
if i[0] in 'AMam':
print(i)

38) Write a program in Python to read a tuple containing name of n number of students from a user. Create a tuple
of those names which are having consecutive same characters. For example the name ‘Poonam’ or ‘Meenu’ or
‘Deepa’, etc.
Effective Computer Science 65
Ans:
n = int(input('Enter number of students : '))
a = ()
for i in range(n):
ele = input('Enter a Name : ')
a = a + (ele,)
print('Tuple of Names :')
print(a)

t = ()
for i in a:
for j in range(len(i)-1):
if i[j] == i[j+1]:
t = t + (i,)
break

print('Tuples of names having consecutive same characters : ')


print(t)
Effective Computer Science 66

Chapter : 9
Lists

 Unsolved Problems (Linear List)

1) Create the following lists using a for loop :


a. A list consisting of the integers 0 to 100
Ans:
a = []
for i in range(101):
a.append(i)
print(a)

b. A list containing the squares of the integers 1 to 50


Ans:
a = []
for i in range(1, 51):
a.append(i*i)
print(a)

c. The list [ ‘Aa’, ‘Bb’, ‘Cc’, …………] that ends with 26 copies of the letters z.
Ans:
a = []
for i in range(1, 27):
a.append(chr(64+i)+chr(96+i))
print(a)

d. The list [ ‘a’, ‘bb’, ‘ccc’, …………] that ends with 26 copies of the letters z.
Ans:
a = []
for i in range(1, 27):
a.append(chr(64+i)*i)
print(a)

e. The list [ ‘1’, ‘22’, ‘333’, ‘4444’, …………] that ends with 9 as 9 times.
Ans:
a = []
for i in range(1, 10):
a.append(str(i)*i)
print(a)

f. The list [ [1], [1,2], [1,2,3], [1,2,3,4], …………] that ends with 9 as 1,2,3,4,5,6,7,8,9 times.
Ans:
a = []
for i in range(1, 10):
b = []
for j in range(1, i+1):
b.append(j)
a.append(b)
print(a)

g. The list [ ‘a’, ‘ab’, ‘abc’, …………] that ends with 26 copies of the letters z.
Effective Computer Science 67
Ans:
a = []
for i in range(1, 27):
b = ''
for j in range(1, i+1):
b = b + chr(96+j)
a.append(b)
print(a)

2) Write a program in Python to read a List of integers of size n given by a user and display the List after dividing the
even elements by 2 and multiplying odd elements by 3.
Ans:
n= int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

for i in range(len(a)):
if a[i]%2 == 0:
a[i] = a[i]//2
else:
a[i] = a[i]*3

print('Output List is : ', a)

3) Write a program in Python to read a List of integers of size n given by a user and display the List after dividing the
elements by 5 which are divisible by 5 and multiply rest of the elements by 5.
Ans:
n= int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

for i in range(len(a)):
if a[i]%5 == 0:
a[i] = a[i]//5
else:
a[i] = a[i]*5

print('Output List is : ', a)

4) Write a program in Python to read a List of integers of size n given by a user and display the List after replacing
the element divisible by 10 with 1 and replacing other elements by 0.
Ans:
n= int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)
Effective Computer Science 68
for i in range(len(a)):
if a[i]%10 == 0:
a[i] = 1
else:
a[i] = 0

print('Output List is : ', a)

5) Write a program in Python to read a List of integers of size n given by a user and increment the elements as
follows in the range of 0 – 100, 5%, 101 – 200, 8%, 201 – 300, 10%, >300, 15%.
Ans:
n= int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

for i in range(len(a)):
if a[i]>=0 and a[i]<=100:
a[i] = a[i] + 0.05 * a[i]
elif a[i]>=101 and a[i]<=200:
a[i] = a[i] + 0.08 * a[i]
elif a[i]>=201 and a[i]<= 300:
a[i] = a[i] + 0.1 * a[i]
else:
a[i] = a[i] + 0.15 * a[i]

print('Output List is : ', a)

6) Write a program in Python to read a List of integers of size n given by a user and display the List after adding the
element present at odd index with the element present at the next index and adding 1 in the elements present
at the even index.
Ans:
n= int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

if n%2 == 0:
n = n-1

for i in range(n):
if i%2 != 0:
a[i] = a[i] + a[i+1]
else:
a[i] = a[i] + 1

print('Output List is : ', a)

7) Write a program in Python to read a List of integers of size n given by a user and display the elements which are
of two digits.
Ans:
n= int(input('Enter size of the List : '))
a = []
Effective Computer Science 69
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

for i in range(len(a)):
if a[i]>=10 and a[i]<=99:
print(a[i])

8) Write a program in Python to read a List of integers of size n given by a user and display the elements which are
ended with 2 or 33.
Ans:
n= int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

for i in range(len(a))
if a[i]%10 == 2 or a[i]%100 == 33:
print(a[i])

9) Write a program in Python to read a List of integers of size n given by a user and display sum of the elements
given by the user.
Ans:
n= int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

s = 0
for i in range(len(a)):
s = s + a[i]
print('Sum = ', s)

10) Write a program in Python to read a List of integers of size n given by a user and display sum of the odd and even
elements of the list separately.
Ans:
n= int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

s = t = 0
for i in range(len(a)):
if a[i]%2 == 0:
s = s + a[i]
else:
t = t + a[i]
print('Sum of even elements = ', s)
Effective Computer Science 70
print('Sum of odd elements = ', t)

11) Write a program in Python to read a List of integers of size n given by a user and display how many odd and even
elements are present in List.
Ans:
n= int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

x = y = 0
for i in range(len(a)):
if a[i]%2 == 0:
x+=1
else:
y+=1
print('Number of even elements = ', x)
print('Number of odd elements = ', y)

12) Write a program in Python to read a List of integers of size n given by a user and display mean of the elements of
the List.
Ans:
n= int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

s = 0
for i in range(len(a)):
s = s + a[i]
print('Mean of the elements = ', s//len(a))

13) Write a program in Python to read a List of integers of size n given by a user and display average of elements
which are divisible by 5 of the List.
Ans:
n= int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

s = 0
x = 0
for i in range(len(a)):
if a[i]%5 == 0:
s = s + a[i]
x+=1
print('Average of the elements = ', s//x)

14) Write a program in Python to read a List of integers of size n given by a user and display sum of odd elements
present at the even index in List.
Effective Computer Science 71
Ans:
n= int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

s = 0
x = 0
for i in range(len(a)):
if a[i]%2 != 0 and i%2 == 0:
s = s + a[i]
print('Sum of the elements = ', s//x)

15) Write a program in Python to read a List of integers of size n given by a user and count the elements in the range
of 0 – 100, 101 – 200, 201 – 300, >300.
Ans:
n= int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

p = q = r = s = 0
for i in range(len(a)):
if a[i]>=0 and a[i]<=100:
p+=1
elif a[i]>=101 and a[i]<=200:
q+=1
elif a[i]>=201 and a[i]<= 300:
r+=1
else:
s+=1

print(p, q, r, s)

16) Write a program in Python to read a List of integers of size n given by a user and display the List after reversing
its elements.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

for i in range(len(a)//2):
a[i], a[len(a)-1-i] = a[len(a)-1-i], a[i]

print('Output List is : ', a)

17) Write a program in Python to read a List of integers of size n given by a user and display the List after arranging
the value of first half with second half of a List if List is of even size. For e.g. given- 1,2,3,4,5,6 output- 4,5,6,1,2,3.
Effective Computer Science 72
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

for i in range(len(a)//2):
a[i], a[len(a)//2 + i] = a[len(a)//2 + i], a[i]

print('Output List is : ', a)

18) Write a program in Python to read a List of integers of size n given by a user and display the List after swapping
the elements present at the odd position with its next element. For e.g. if given List is 1, 2, 3, 4, 5, 6, 7 then
output should be 2, 1, 4, 3, 6, 5, 7.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

for i in range(len(a)):
if i%2 == 0:
a[i], a[i+1] = a[i+1], a[i]

print('Output List is : ', a)

19) Write a program in Python to read a List of integers of size n given by a user and display the List after swapping
the element which is divisible by 10 with its very next element.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

for i in range(len(a)):
if a[i]%10 == 0:
a[i], a[i+1] = a[i+1], a[i]

print('Output List is : ', a)

20) Write a program in Python to read a List of integers of size n given by a user and display the List after swapping
the element which is divisible by 5 with its very previous element.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)
Effective Computer Science 73

for i in range(1, len(a)):


if a[i]%5 == 0:
a[i], a[i-1] = a[i-1], a[i]

print('Output List is : ', a)

21) Write a program in Python to read a List of integers of size n given by a user and convert the List into two
separate Lists containing the even and odd elements from the given list.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

b = []
c = []
for i in range(len(a)):
if a[i]%2 == 0:
b.append(a[i])
else:
c.append(a[i])

print('Output List is even elements: ', b)


print('Output List is odd elements: ', c)

22) Write a program in Python to read a List of integers of size n given by a user and display prime terms of the list.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

for i in range(len(a)):
x = 0
for j in range(1, a[i]+1):
if a[i]%j == 0:
x+=1
if x == 2:
print(a[i])

23) Write a program in Python to read a List of integers of size n given by a user and shift the element towards right
had side and last element at first.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)

print('Given List is : ', a)


Effective Computer Science 74
a = a[-1:] + a[:-1]

print('Output List is : ', a)

24) Write a program in Python to read a List of integers of size n given by a user and shift the element towards left
had side and first element at last.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

a = a[1:] + a[:1]

print('Output List is : ', a)

25) Write a program in Python to read a List of integers of size n given by a user and display maximum and minimum
element with their position in List.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

L = a[0]
p = 0
S = a[0]
q = 0
for i in range(1, len(a)):
if a[i] > L:
L = a[i]
p = i
elif a[i] < S:
S = a[i]
q = i

print('Largest element of the List is : ', a[p])


print('Position of largest element of the List is : ', p+1)
print('Smallest element of the List is : ', a[q])
print('Position of smallest element of the List is : ', q+1)

26) Write a program in Python to read a List of integers of size n given by a user and swap largest and smallest
element of the list.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)
Effective Computer Science 75
L = a[0]
p = 0
S = a[0]
q = 0
for i in range(1, len(a)):
if a[i] > L:
L = a[i]
p = i
elif a[i] < S:
S = a[i]
q = i

a[p], a[q] = a[q], a[p]

print('Output List is : ', a)

27) Write a program in Python to read a List of integers of size n given by a user and divide the largest element by
two and multiply the smallest element by 2.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

L = a[0]
p = 0
S = a[0]
q = 0
for i in range(1, len(a)):
if a[i] > L:
L = a[i]
p = i
elif a[i] < S:
S = a[i]
q = i

a[p] = a[p]//2
a[q] = a[p]*2

print('Output List is : ', a)

28) Write a program in Python to read a List of integers of size n given by a user and display the largest and second
largest element of the List.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

L = a[0]
p = 0
S = a[0]
q = 0
Effective Computer Science 76
for i in range(1, len(a)):
if a[i] > L:
L = a[i]
p = i
for i in range(1, len(a)):
if a[i] > S and a[i] < L:
S = a[i]
q = i

print('First Largest element of the List is : ', a[p])


print('Second Largest element of the List is : ', a[q])

29) Write a program in Python to read a List of integers of size n given by a user and swap first largest and second
largest element of the list.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

L = a[0]
p = 0
S = a[0]
q = 0
for i in range(1, len(a)):
if a[i] > L:
L = a[i]
p = i
for i in range(1, len(a)):
if a[i] > S and a[i] < L:
S = a[i]
q = i

a[p], a[q] = a[q], a[p]

print('Output List is : ', a)

30) Write a program in Python to read a List of integers of size n given by a user and check if the List is in ascending
order or descending order or all elements are same or it is not sorted.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter Elements in the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

x = y = 0
for i in range(len(a)-1):
if a[i] <= a[i+1]:
x+=1
if a[i] >= a[i+1]:
y+=1
Effective Computer Science 77
if x == len(a)-1 and y == len(a)-1:
print('List contain same element')
elif x == len(a)-1:
print('List is in ascending order')
elif y == len(a)-1:
print('List is in descending order')
else:
print('List is in unsorted order')

31) Write a program in Python to read a List of integers of size n given by a user in sorted order and an element to
be inserted from a user and insert the element at the appropriate place in the list.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter elements of the List in ascending order : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

x = int(input('Enter an element to be inserted : '))

p = None
for i in range(len(a)):
if x <= a[i]:
p = i
break

if p == None:
a.append(x)
else:
a.insert(p, x)

print('List after insertion : ', a)

32) Write a program in Python to read a List of integers of size n given by a user and an element to be inserted and
Display the List after inserting the element at the 5th position in the given List.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter elements of the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

x = int(input('Enter an element to be inserted : '))

a.insert(4, x)

print('List after insertion : ', a)

33) Write a program in Python to read a List of integers of size n given by a user and element and position to be
inserted in the list. Insert the element at the given position in the list.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter elements of the List : ')
Effective Computer Science 78
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

x = int(input('Enter an element to be inserted : '))


p = int(input('Enter position of insertion : '))

a.insert(p-1, x)

print('List after insertion : ', a)

34) Write a program in Python to read a List of integers of size n given by a user and an element to be deleted from
the list. Delete that element from the list.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter elements of the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

x = int(input('Enter an element to be deleted : '))

i = 0
while i <= len(a)-1:
if a[i] == x:
a.pop(i)
i-=1
i+=1

if len(a) == n:
print('Element not found in the List')
else:
print('List after deletion is : ', a)

35) Write a program in Python to read a List of integers of size n given by a user and delete all the even elements
from the list.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter elements of the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

i = 0
while i <= len(a)-1:
if a[i]%2 == 0:
a.pop(i)
i-=1
i+=1
if len(a) == n:
print('Element not found in the List')
else:
print('List after deletion is : ', a)
Effective Computer Science 79
36) Write a program in Python to read a List of integers of size n given by a user and display the List after deleting all
the even element from the List and shift the elements towards right had side and put 0 at the beginning of the
List for each deleted element. For example, give List is 1, 2, 3, 4, 5 then output should be 0, 0, 1, 3, 5.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter elements of the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

i = 0
while i <= len(a)-1:
if a[i]%2 == 0:
a.pop(i)
a.insert(0,0)
i+=1

print('List after deletion is : ', a)

37) Write a program in Python to read a List of integers of size n given by a user and Display the List after deleting all
the negative element from the List and shift the elements towards left had side and put 0 at the end of the List
for each deleted element. For example, give List is 1, -2, 3, -4, -5, 6 then output should be 1, 3, 6, 0, 0, 0.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter elements of the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

i = 0
x = 0
while i <= len(a)-1-x:
if a[i] < 0:
a.pop(i)
a.append(0)
x+=1
i-=1
i+=1

print('List after deletion is : ', a)

38) Write a program in Python to read a List of integers of size n given by a user and display it in ascending and
descending order.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter elements of the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

a.sort()
print('List in ascending order : ', a)
Effective Computer Science 80
a.sort(reverse = True)
print('List in descending order : ', a)

39) Write a program in Python to read two lists of integers of size n given by a user and add their elements present
at same index to from a new list.
Ans:
n = int(input('Enter size of the List : '))
a = []
b = []
c = []

print('Enter elements of the First List : ')


for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)

print('Enter elements of the Second List : ')


for i in range(n):
ele = int(input('Enter an Element : '))
b.append(ele)

print('First List is : ', a)


print('Second List is : ', b)

for i in range(len(a)):
c.append(a[i]+b[i])

print('Output List is : ', c)

40) Merge two lists to from a third list in following pattern :


For e.g. A {3,2,1,7,6,3} and B{9,3,5,6,2,8,7} the resultant C {3,1,7,3,9,3,5,7,2,6,6,2,8}.
Ans:
n = int(input('Enter size of the List : '))
a = []
b = []
c = []
d = []

print('Enter elements of the First List : ')


for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)

print('Enter elements of the Second List : ')


for i in range(n):
ele = int(input('Enter an Element : '))
b.append(ele)

print('First List is : ', a)


print('Second List is : ', b)

for i in range(len(a)):
if a[i]%2 == 0:
c.append(a[i])
else:
d.append(a[i])
Effective Computer Science 81
for i in range(len(b)):
if b[i]%2 == 0:
c.append(b[i])
else:
d.append(b[i])

a = d + c

print('Output List is : ', a)

41) Write a program in Python to read a List of integers of size n given by a user and shift all the 0 towards left and
other numbers towards right hand side in the List.
Ans:
n = int(input('Enter size of the List : '))
a = []
b = []
c = []

print('Enter elements of the First List : ')


for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)

print('Given List is : ', a)

for i in range(len(a)):
if a[i] == 0:
b.append(a[i])
else:
c.append(a[i])

a = b + c

print('Output List is : ', a)

42) Write a program in Python to read a List of integers of size n given by a user and shift all the negative numbers
towards left and the positive numbers towards right hand side in the List. For example, if List is 3,-5, 1, 3, 7, 0,-
15, 3,-7, -8. then the resultant List will look like : -5, -15, -7, -8, 3, 1, 3, 7, 0, 3
Ans:
n = int(input('Enter size of the List : '))
a = []
b = []
c = []

print('Enter elements of the First List : ')


for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)

print('Given List is : ', a)


for i in range(len(a)):
if a[i] < 0:
b.append(a[i])
else:
c.append(a[i])
a = b + c

print('Output List is : ', a)


Effective Computer Science 82
43) Write a program in Python to read a List of integers of size n given by a user and copy the index number of odd
and even elements of the give list List into two separate Lists.
Ans:
n = int(input('Enter size of the List : '))
a = []
b = []
c = []

print('Enter elements of the First List : ')


for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)

print('Given List is : ', a)

for i in range(len(a)):
if a[i]%2 == 0:
b.append(i)
else:
c.append(i)

a = b + c

print('List containing indexes of even elements is : ', b)


print('List containing indexes of odd elements is : ', c)

44) Write a program in Python to read a List of integers of size n given by a user and element to be searched. Search
for the given number in a given List and display its position if it is found else display the appropriate message.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter elements of the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

x = int(input('Enter an element to be searched : '))

p = None
for i in range(len(a)):
if x == a[i]:
p = i
break

if p == None:
print('Element no found in the list')
else:
print('Element found at the position ', p+1)

45) Write a program in Python to read a List of integers of size n given by a user and an element. Count the
frequency of the given element in the List.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter elements of the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
Effective Computer Science 83
print('Given List is : ', a)

x = int(input('Enter an element to be searched : '))

c = 0
for i in range(len(a)):
if x == a[i]:
c+=1

if c == 0:
print('Element no found in the list')
else:
print('Element counted in the List : ', c)

46) Write a program in Python to read a List of integers of size n given by a user and find the frequency of all the
elements of the list. Also print the list of unique elements in the list and duplicate elements in the list.
Ans:
n = int(input('Enter size of the List : '))
a = []
print('Enter elements of the List : ')
for i in range(n):
ele = int(input('Enter an Element : '))
a.append(ele)
print('Given List is : ', a)

u = []
d = []
for i in range(len(a)):
if a[i] not in u and a[i] not in d:
x = 1
for j in range(i+1, len(a)):
if a[i] == a[j]:
x+=1
if x == 1:
u.append(a[i])
else:
d.append(a[i])
print(a[i], 'appeared', x, 'times')

print('List of unique elements : ', u)


print('List of duplicate elements : ', d)

47) Write a program in Python to read a List of integers of size n given by a user and check that they contain
identical elements or not.
Ans:
n=int(input('Enter size of the Lists:'))
a = []
b = []

print('Enter elements for first list:')


for i in range(n):
ele=int(input('Enter an element : '))
a.append(ele)

print('Enter elements for second list:')


for i in range(n):
ele=int(input('Enter an element : '))
b.append(ele)
Effective Computer Science 84
print('First List: ', a)
print('Second List: ', b)

x = 0
for i in range(len(a)):
if a[i] in b:
x+=1

if x == n:
print('Both List contains same elements')
else:
print('Both List contains different elements')

48) Write a program in Python to generate a list containing n terms of Fibonacci series.
Ans:
n=int(input('Enter number of terms :'))

a = [0, 1]
for i in range(n-2):
a.append(a[i]+a[i+1])

print(a)

49) Write a program in Python to read a List of the names of n number of countries and arrange them in alphabetical
order i.e., in lexicological order.
Ans:
n=int(input('Enter number of names : '))
a = []
for i in range(n):
ele = input('Enter a name : ')
a.append(ele)

print('Given List : ', a)

a.sort()

print('Output List : ', a)

50) Write a program in Python to create a list of strings after reading five lines of a story from a user. Display the
longest line given by a user from the list.
Ans:
print('Enter five lines of a story : ')
a = []
for i in range(5):
ele = input()
a.append(ele)

print('Given List : ')


print(a)

L = a[0]
for i in range(1, len(a)):
if len(a[i]) > len(L):
L = a[i]

print('Longest line is : ')


print(L)
Effective Computer Science 85
51) Write a program in Python to create a list of strings after reading ten lines of a story from a user. Display the only
those line which starts with ‘The’.
Ans:
print('Enter ten lines of a story : ')
a = []
for i in range(10):
ele = input()
a.append(ele)

print('Given List : ')


print(a)

for i in range(len(a)):
if a[i][0]=='T' and a[i][1]=='h' and a[i][2]=='e' and a[i][3]==' ':
print(a[i])

52) Write a program in Python to read a string from a user and create a list containing length of each word of the
given string.
Ans:
a = input('Enter a string : ')
print('Given String is : ')
print(a)
a = a.split()
b = []
for i in a:
b.append(len(i))

print('Output List is : ')


print(b)

53) Write a program in Python to read a string from a user and create a list of those words which starts with a vowel.
Ans:
a = input('Enter a string : ')
print('Given String is : ')
print(a)
a = a.split()
b = []
for i in a:
if i[0] in 'AEIOUaeiou':
b.append(len(i))

print('Output List is : ')


print(b)

54) Write a program in Python to read a string from a user and create a list of those words which are having length
of more than 4 characters.
Ans:
a = input('Enter a string : ')
print('Given String is : ')
print(a)
a = a.split()
b = []
for i in a:
if if len(i) > 4:
b.append(i)

print('Output List is : ')


print(b)
Effective Computer Science 86
55) Write a program in Python to read a string from a user and create a list of the reverse of each words from the
given string.
Ans:
a = input('Enter a string : ')
print('Given String is : ')
print(a)
a = a.split()
b = []
for i in a:
b.append(i[::-1])

print('Output List is : ')


print(b)

56) Write a program in Python to read a string from a user and create a list of those words which do not contain any
vowel.
Ans:
a = input('Enter a string : ')
print('Given String is : ')
print(a)
a = a.split()
b = []
for i in a:
f = 0
for j in i:
if j in 'AEIOUaeiou':
f = 1
break
if f == 0:
b.append(i)

print('Output List is : ')


print(b)

57) Write a program in Python to read a list containing name of n number of students from a user. Display those
names which starts with ‘A’ or ‘M’.
Ans:
n=int(input('Enter number of names : '))
a = []
for i in range(n):
ele = input('Enter a name : ')
a.append(ele)

print('Given List : ', a)

for i in range(len(a)):
if a[i][0] in 'AaMm':
print(a[i])

58) Write a program in Python to read a list containing name of n number of students from a user. Create a list of
those names which are having consecutive same characters. For example, the name ‘Poonam’ or ‘Meenu’ or
‘Deepa’, etc.
Ans:
n=int(input('Enter number of names : '))
a = []
for i in range(n):
ele = input('Enter a name : ')
a.append(ele)
Effective Computer Science 87
print('Given List : ', a)

for i in a:
f = 0
for j in range(len(i)-1):
if i[j] == i[j+1]:
print(i)
break

 Unsolved Problems (Nested List)

1) Write a program in Python to create a matrix of integers with m rows and n columns. Display the matrix of the
elements of the nested List after Replace the element by 7 which are divisible by 7 and replace other elements by
1.
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix :')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

for i in range(m):
for j in range (n):
if a[i][j] % 7 == 0:
a[i][j] = 7
else:
a[i][j] = 1

print ('Output Matrix : ')

for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

2) Write a program in Python to create a matrix of integers with m rows and n columns. Display the elements ended
with 5 or 55.
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


Effective Computer Science 88
a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix :')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

for i in range(m):
for j in range (n):
if a[i][j]%10 == 5 or a[i][j]%100 == 55:
print(a[i][j])

3) Write a program in Python to create a matrix of integers with m rows and n columns. Display sum of the even and
odd elements separately.
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix :')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

s = t = 0
for i in range(m):
for j in range (n):
if a[i][j]%2 == 0:
s = s + a[i][j]
else:
t = t + a[i][j]

print('Sum of even elements : ', s)


print('Sum of odd elements : ', t)

4) Write a program in Python to create a matrix of integers with m rows and n columns. Display the largest and
smallest element of the List.
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(m):
b = [ ]
Effective Computer Science 89
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix :')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

L = a[i][j]
S = a[i][j]
for i in range(m):
for j in range (n):
if a[i][j] > L:
L = a[i][j]
elif a[i][j] < S:
S = a[i][j]

print('Largest elements : ', L)


print('Smallest elements : ', S)

5) Write a program in Python to create a matrix of integers with m rows and n columns. Display Sum of the even
elements present on each row of a matrix.
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix :')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

for i in range(m):
s = 0
for j in range (n):
if a[i][j]%2 == 0:
s = s + a[i][j]
print(s)

6) Write a program in Python to create a matrix of integers with m rows and n columns. Display Product of odd
elements present on each row of a matrix.
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(m):
b = [ ]
Effective Computer Science 90
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix :')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

for i in range(m):
p = 1
for j in range (n):
if a[i][j]%2 != 0:
p = p * a[i][j]
print(p)

7) Write a program in Python to create a matrix of integers with m rows and n columns. Display Sum of the even
elements present on each column of a matrix.
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix :')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

for i in range(n):
s = 0
for j in range (m):
if a[j][i]%2 == 0:
s = s + a[j][i]
print(s)

8) Write a program in Python to create a matrix of integers with m rows and n columns. Display largest element of
each row.
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix :')


Effective Computer Science 91
for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

for i in range(m):
L = a[i][0]
for j in range (1, n):
if a[i][j] > L:
L = a[i][j]
print(L)

9) Write a program in Python to create a matrix of integers with m rows and n columns. Display smallest element of
column.
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix :')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

for i in range(n):
S = a[0][i]
for j in range (1, m):
if a[j][i] < S:
S = a[j][i]
print(S)

10) Write a program in Python to create a matrix of integers with m rows and n columns. Convert a 2D into a linear
List containing elements in row major form. For example, List is :
1 2 3 Then the output should be a linear List : 1 2 3 4 5 6 7 8 9
4 5 6
7 8 9
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix :')


for i in range(m):
for j in range(n):
Effective Computer Science 92
print(a[i][j], end = ' ')
print()

b = []
for i in range(m):
for j in range (n):
b.append(a[i][j])

print('Output List : ')


print(b)

11) Write a program in Python to create a matrix of integers with m rows and n columns. Convert a 2D into a linear
List containing elements Column wise.
1 2 3 Then the output should be a linear List : 1 4 7 2 5 8 3 6 9
4 5 6
7 8 9
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix :')


for i in range(mz):
for j in range(n):
print(a[i][j], end = ' ')
print()

b = []
for i in range(n):
for j in range (m):
b.append(a[j][i])

print('Output List : ')


print(b)

12) Write a program in Python to create a matrix of integers with m rows and n columns. Convert a 2D into two linear
List containing even and odd elements.
1 2 3 Then the Even linear List : 2 4 6 8
4 5 6 Then the Odd linear List : 1 3 5 7 9
7 8 9
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)
Effective Computer Science 93
print('Original Matrix :')
for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

b = []
c = []
for i in range(m):
for j in range (n):
if a[i][j]%2 == 0:
b.append(a[i][j])
else:
c.append(a[i][j])

print('Output List : ')


print(b)
print(c)

13) Write a program in Python to create a square matrix of integers with n rows and n columns. Display the elements
of Upper left triangle. . For example – if the 2D List is given as :
1 2 3 4 5 then output should come as sum of the elements : 1 2 3 4 5
1 2 3 4 5 1 2 3 4
1 2 3 4 5 1 2 3
1 2 3 4 5 1 2
1 2 3 4 5 1
Ans:
n = int(input('Enter Number of Rows and Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(n):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix : ')


for i in range(n):
for j in range(n):
print(a[i][j], end = ' ')
print()

print('Elements of Upper Left Triangle of the Matrix : ')


for i in range(n):
for j in range (n):
if i+j <= n-1:
print(a[i][j], end = ' ')
else:
print(' ', end = ' ')
print()

14) Write a program in Python to create a square matrix of integers with n rows and n columns. Display the elements
of Lower right triangle. For example – if the 2D List is given as :
1 2 3 4 5 then output should come as : 5
1 2 3 4 5 4 5
1 2 3 4 5 3 4 5
1 2 3 4 5 2 3 4 5
1 2 3 4 5 1 2 3 4 5
Effective Computer Science 94
Ans:
n = int(input('Enter Number of Rows and Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(n):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix : ')


for i in range(n):
for j in range(n):
print(a[i][j], end = ' ')
print()

print('Elements of Lower Right Triangle of the Matrix : ')


for i in range(n):
for j in range (n):
if i+j >= n-1:
print(a[i][j], end = ' ')
else:
print(' ', end = ' ')
print()

15) Write a program in Python to create a square matrix of integers with n rows and n columns. Display the elements
of Lower left triangle. For example – if the 2D List is given as :
1 2 3 4 5 then output should come as sum of the elements of : 1
1 2 3 4 5 1 2
1 2 3 4 5 1 2 3
1 2 3 4 5 1 2 3 4
1 2 3 4 5 1 2 3 4 5
Ans:
n = int(input('Enter Number of Rows and Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(n):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix : ')


for i in range(n):
for j in range(n):
print(a[i][j], end = ' ')
print()

print('Elements of Lower Left Triangle of the Matrix : ')


for i in range(n):
for j in range (n):
if i>= j:
print(a[i][j], end = ' ')
else:
print(' ', end = ' ')
print()
Effective Computer Science 95
16) Write a program in Python to create a square matrix of integers with n rows and n columns. Display the elements
of Middle row and middle column. For example – if the 2D List is given as :
1 2 3 4 5 then output should come as : 3
1 2 3 4 5 3
1 2 3 4 5 1 2 3 4 5
1 2 3 4 5 3
1 2 3 4 5 3
Ans:
n = int(input('Enter Number of Rows and Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(n):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix : ')


for i in range(n):
for j in range(n):
print(a[i][j], end = ' ')
print()

print('Elements of middle row and middle column of the Matrix : ')


for i in range(n):
for j in range (n):
if i == n//2 or j == n//2:
print(a[i][j], end = ' ')
else:
print(' ', end = ' ')
print()

17) Write a program in Python to create a square matrix of integers with n rows and n columns. Display Sum of the
elements present on the Boundary. For example – if the 2D List is given as:
1 1 1 1 1 then output should come as sum of the elements is : 16
1 5 5 5 1
1 5 5 5 1
1 5 5 5 1
1 1 1 1 1
Ans:
n = int(input('Enter Number of Rows and Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(n):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix : ')


for i in range(n):
for j in range(n):
print(a[i][j], end = ' ')
print()

s = 0
for i in range(n):
Effective Computer Science 96
for j in range (n):
if i==0 or i==n-1 or j==0 or j==n-1:
s = s + a[i][j]
print('Sum of the boundry elements of the Matrix : ')
print(s)

18) Write a program in Python to create a square matrix of integers with n rows and n columns and display the sum
of the elements which are not present on the boundary. For example – if the 2D List is :
1 1 1 1 1 then output should come as sum of the elements is : 45
1 5 5 5 1
1 5 5 5 1
1 5 5 5 1
1 1 1 1 1
Ans:
n = int(input('Enter Number of Rows and Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(n):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix : ')


for i in range(n):
for j in range(n):
print(a[i][j], end = ' ')
print()

s = 0
for i in range(n):
for j in range (n):
if i!=0 and i!=n-1 and j!=0 and j!=n-1:
s = s + a[i][j]
print('Sum of the elements not present on the boundry of the Matrix : ')
print(s)

19) Write a program in Python to create a square matrix of integers with n rows and n columns. Display the elements
present at the Corners. For example the 2D List of odd dimension is as follows –
1 2 3 Then the output should be – 1 3
4 5 6
7 8 9 7 9
Ans:
n = int(input('Enter Number of Rows and Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(n):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix : ')


for i in range(n):
for j in range(n):
print(a[i][j], end = ' ')
print()
Effective Computer Science 97

print('Elements at the corner of the Matrix : ')


for i in range(n):
for j in range (n):
if (i==0 and j==0) or (i==n-1 and j==0) or (i==0 and j==n-1) or (i==n-1 and
j==n-1):
print(a[i][j], end = ' ')
else:
print(' ', end = ' ')
print()

20) Write a program in Python to create a square matrix of integers with n rows and n columns. Display the elements
of Transpose of the matrix. For example – if the 2D List is given as :
1 2 3 then the output should come as : 1 1 1
1 2 3 2 2 2
1 2 3 3 3 3
Ans:
n = int(input('Enter Number of Rows and Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(n):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix : ')


for i in range(n):
for j in range(n):
print(a[i][j], end = ' ')
print()

print('Elements of Upper Right Triangle of the Matrix : ')


for i in range(n):
for j in range (n):
if i <=j:
a[i][j], a[j][i] = a[j][i], a[i][j]

print('Transpose of the Matrix : ')


for i in range(n):
for j in range(n):
print(a[i][j], end = ' ')
print()

21) Write a program in Python to create a matrix of the integer elements, in the form of a square matrix of odd size
with n number of rows and columns and display the elements of second quarter of the 2D List. For example – if
the 2D List is given as:
1 2 3 4 5 then output should come as : 3 4 5
5 4 3 2 1 3 2 1
5 5 5 5 5 5 5 5
4 5 3 7 8
9 8 7 6 5
Ans:
n = int(input('Enter Number of Rows and Columns : '))

print('Enter Elements of the List :')


a = [ ]
Effective Computer Science 98
for i in range(n):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix : ')


for i in range(n):
for j in range(n):
print(a[i][j], end = ' ')
print()

print('Elements of Second Quarter of the Matrix : ')


for i in range(n):
for j in range (n):
if i<=n//2 and j>=n//2:
print(a[i][j], end = ' ')
else:
print(' ', end = ' ')
print()

22) Write a program in Python to create a matrix of the integer elements, in the form of a square matrix of odd size
with n number of rows and columns and display the elements of third quarter of the 2D List. For example – if the
2D List is given as:
1 2 3 4 5 then output should come as : 5 5 5
5 4 3 2 1 4 5 3
5 5 5 5 5 9 8 3
4 5 3 7 8
9 8 3 6 5
Ans:
n = int(input('Enter Number of Rows and Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(n):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix : ')


for i in range(n):
for j in range(n):
print(a[i][j], end = ' ')
print()

print('Elements of Third Quarter of the Matrix : ')


for i in range(n):
for j in range (n):
if i>=n//2 and j<=n//2:
print(a[i][j], end = ' ')
else:
print(' ', end = ' ')
print()

23) Write a program in Python to create a matrix of the integer elements, in the form of a square matrix of odd size
with n number of rows and columns and display the elements of fourth quarter of the 2D List. For example – if
the 2D List is given as:
Effective Computer Science 99
1 2 3 4 5 then output should come as : 5 5 5
5 4 3 2 1 3 7 8
5 5 5 5 5 7 6 5
4 5 3 7 8
9 8 7 6 5
Ans:
n = int(input('Enter Number of Rows and Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(n):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix : ')


for i in range(n):
for j in range(n):
print(a[i][j], end = ' ')
print()

print('Elements of Fourth Quarter of the Matrix : ')


for i in range(n):
for j in range (n):
if i>=n//2 and j>=n//2:
print(a[i][j], end = ' ')
else:
print(' ', end = ' ')
print()

24) Write a program in Python to create a matrix of integers with m rows and n columns. Display the elements of
Middle row and Middle column separately. For example, the 2D List of odd dimension is as follows –
1 2 3 Then the output should be –
4 5 6 Middle Row :4 5 6
7 8 9 Middle Column : 2 5 8
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix :')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

print('\nElements of middle row : ')


for i in range(n):
print(a[m//2][i], end = ' ')
Effective Computer Science 100
print('\nElements of middle column : ')
for i in range(m):
print(a[i][n//2], end = ' ')

25) Write a program in Python to create a matrix of integers with m rows and n columns. Display the elements of
Both the diagonals separately. For example, the 2D List of odd dimension is as follows –
1 2 3 Then the output should be –
4 5 6 Diagonal One : 1 5 9
7 8 9 Diagonal Two : 3 5 7
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix :')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

print('\nElements of left diagnol : ')


for i in range(n):
print(a[i][i], end = ' ')

print('\nElements of right diagnol : ')


for i in range(m):
print(a[i][n-1-i], end = ' ')

26) Write a program in Python to create a matrix of integers with m rows and n columns. Display the matrix after
swapping middle row elements with the middle column elements.
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix :')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

for i in range(n):
a[m//2][i],a[i][n//2] = a[i][n//2], a[m//2][i]
Effective Computer Science 101
print('Output Matrix :')
for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

27) Write a program in Python to create a matrix of integers with m rows and n columns. Display the elements of
Swap both the diagonals. For example – if the 2D List is given as:
1 2 3 4 5 then output should come as : 5 2 3 4 1
6 1 8 5 1 6 5 8 1 1
8 4 1 3 5 8 4 1 3 5
4 5 8 1 6 4 1 8 5 6
5 4 3 2 1 1 4 3 2 5
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Original Matrix :')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

for i in range(n):
a[i][i],a[i][n-1-i] = a[i][n-1-i], a[i5][i]

print('Output Matrix :')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

28) Write a program in Python to create a matrix of integers with m rows and n columns and display the elements of
by swapping first column with last column. For example – if the 2D List is given as:
1 2 3 4 5 then output should come as : 5 2 3 4 1
6 7 8 9 1 1 7 8 9 6
8 4 2 4 5 5 4 2 4 8
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element of the List : ')))
a.append(b)

print('Matrix of the element is : ')


Effective Computer Science 102
for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

for i in range(m):
a[i][0] , a[i][n-1] = a[i][n-1] , a[i][0]

print('Resultant Matrix is : ')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

29) Write a program in Python to create a matrix of integers with m rows and n columns. Display whether both the
matrices are identical or not.
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))
print('Enter elements of the first matrix :')
a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element : ')))
a.append(b)

print('Enter elements of the second matrix :')


x = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element : ')))
x.append(b)

print('First Matrix of the element is : ')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

print('Second Matrix of the element is : ')


for i in range(m):
for j in range(n):
print(x[i][j], end = ' ')
print()

if a == x:
print('Both matrices are identical')
else:
print('Both matrices are not identical')

30) Write a program in Python to create a matrix of integers with m rows and n columns. Display the List after
reversing each row elements. For example – if the 2D List is given as:
1 2 3 4 5 then output should come as the sum of the elements of : 5 4 3 2 1
6 7 8 9 1 1 9 8 7 6
8 4 2 4 5 8 4 2 4 5
1 9 8 7 6 6 7 8 9 1
5 4 3 2 1 1 2 3 4 5
Effective Computer Science 103
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the Matrix :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element : ')))
a.append(b)

print('Matrix of the element is : ')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

for i in range(m//2):
for j in range(n):
a[i][j] , a[m-1-i][j] = a[m-1-i][j] , a[i][j]

print('Resultant Matrix is : ')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

31) Write a program in Python to create a matrix of integers with m rows and n columns. Display the List after
reversing each column elements. For example – if the 2D List is given as:
1 2 3 4 5 then output should come as : 5 4 3 2 1
6 7 8 9 1 1 9 8 7 6
8 4 2 4 5 8 4 2 4 5
1 9 8 7 6 6 7 8 9 1
5 4 3 2 1 1 2 3 4 5
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the Matrix :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element : ')))
a.append(b)

print('Matrix of the element is : ')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

for i in range(n//2):
for j in range(m):
a[j][i] , a[j][n-1-i] = a[j][n-1-i] , a[j][i]

print('Resultant Matrix is : ')


Effective Computer Science 104
for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

32) Write a program in Python to create a matrix of integers with m rows and n columns. Display the List after
swapping the elements of first half with second half on each rows. For example, if the 2D List is given as:
1 2 3 4 5 6 then output should come as : 4 5 6 1 2 3
6 7 8 9 1 2 9 1 2 6 7 8
8 4 2 4 5 3 4 5 3 8 4 2
Ans:
m = int(input('Enter Rows : '))
n = int(input('Enter Columns : '))

print('Enter Elements of the Matrix :')


a = [ ]
for i in range(m):
b = [ ]
for j in range(n):
b.append(int(input('Enter an Element : ')))
a.append(b)

print('Matrix of the element is : ')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

for i in range(m):
a[i] = a[i][n//2:] + a[i][:n//2]

print('Resultant Matrix is : ')


for i in range(m):
for j in range(n):
print(a[i][j], end = ' ')
print()

33) Write a program in Python to read a list of n number of integer elements. Write down a program in Python to
convert the linear List into a 2 – D List in the following format :
For example, the linear List A = [ 1, 2, 3, 4, 5 ] then the 2 – D List will be –
1 2 3 4 5
0 1 2 3 4
0 0 1 2 3
0 0 0 1 2
0 0 0 0 1
Ans:
n = int(input('Enter Size : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(n):
a.append(int(input('Enter an Element : ')))

print('Given List is : ')


print(a)

b = []
for i in range(n):
c = []
Effective Computer Science 105
for j in range(n):
if i<=j:
c.append(a[j-i])
else:
c.append(0)
b.append(c)

print('Resultant Matrix is : ')


for i in range(n):
for j in range(n):
print(b[i][j], end = ' ')
print()

34) Write a program in Python to read a list of n number of integer elements. Write down a program in Python to
convert the linear List into a 2 – D List in the following format :
For example, the linear List A = [ 1, 2, 3, 4, 5 ] then the 2 – D List will be –

1 2 3 4 5
2 0 0 0 4
3 0 0 0 3
4 0 0 0 2
5 4 3 2 1
Ans:
n = int(input('Enter Size : '))

print('Enter Elements of the List :')


a = [ ]
for i in range(n):
a.append(int(input('Enter an Element : ')))

print('Given List is : ')


print(a)

b = []
for i in range(n):
c = []
for j in range(n):
if i==0:
c.append(a[j])
elif i==n-1:
c.append(a[i-j])
elif j==0:
c.append(a[i])
elif j==n-1:
c.append(a[j-i])
else:
c.append(0)
b.append(c)

print('Resultant Matrix is : ')


for i in range(n):
for j in range(n):
print(b[i][j], end = ' ')
print()
Effective Computer Science 106

Chapter : 10
Dictionaries

 Unsolved Problems

1) Create a Dictionary for n number of books from a user in such a way that it contains names of books as key and
their price as values. Display name and price of those books which are having price more than 500.
Ans:
n = int(input('Enter Size : '))

a = {}
for i in range(n):
k = input('Enter Name : ')
v = int(input('Enter Price : '))
a[k] = v

print('Given Dictionary is : ')


print(a)

for i in a:
if a[i] > 500:
print('Book Name : ', i)
print('Price : ', a[i])

2) Create a Dictionary for n number of books from a user in such a way that it contains names of books as key and
their price as values. Display the details of all the books after increasing the price of all the books by 10%.
Ans:
n = int(input('Enter Size : '))

a = {}
for i in range(n):
k = input('Enter Name : ')
v = int(input('Enter Price : '))
a[k] = v

print('Given Dictionary is : ')


print(a)

for i in a:
a[i] = a[i] + 0.1*a[i]

print('Updated Dictionary is : ')


print(a)
3) Create a Dictionary for n number of employees from a user in such a way that it contains names of employee as
key and salary as their values. Display average salary of the employees.
Ans:
n = int(input('Enter Size : '))

a = {}
for i in range(n):
k = input('Enter Name : ')
v = int(input('Enter Salary : '))
a[k] = v
Effective Computer Science 107

print('Given Dictionary is : ')


print(a)

for i in a:
s = s + a[i]

print('Average Salary of the employees : ', s//n)

4) Create a Dictionary for n number of students from a user in such a way that it contains names of students as
key and marks as their values. Display the details of that student who has scored minimum marks.
Ans:
n = int(input('Enter Size : '))

a = {}
for i in range(n):
k = input('Enter Name : ')
v = int(input('Enter Marks : '))
a[k] = v

print('Given Dictionary is : ')


print(a)

m = list(a.values())[0]
for i in a:
if a[i] < m:
m = a[i]
x = i

print('Name of the student : ', x)


print('Marks of the student : ', m)

5) Create a Dictionary for n number of employees from a user in such a way that it contains names of employee as
key and salary as their values. Display the detail of the employee who is getting maximum salary.
Ans:
n = int(input('Enter Size : '))

a = {}
for i in range(n):
k = input('Enter Name : ')
v = int(input('Enter Salary : '))
a[k] = v

print('Given Dictionary is : ')


print(a)

m = list(a.values())[0]
for i in a:
if a[i] > m:
m = a[i]
x = i

print('Name of the employee : ', x)


print('Salary of the employee: ', m)

6) Create a Dictionary for n number of employees from a user in such a way that it contains names of employee as
key and their department as their values. Display the count and detail of the employees who is are working in
the department “Accounts”.
Effective Computer Science 108
Ans:
n = int(input('Enter Size : '))

a = {}
for i in range(n):
k = input('Enter Name : ')
v = input('Enter Department : ')
a[k] = v

print('Given Dictionary is : ')


print(a)

x = 0
for i in a:
if a[i] == 'Accounts':
x+=1
print('Name : ', i)

print('Number of employees are ', x)

7) Create a Dictionary for n number of students from a user in such a way that it contains names of students as
key and grade as their values. Display how many students have got grade ‘A’, ‘B’ and ‘C’.
Ans:
n = int(input('Enter Size : '))

a = {}
for i in range(n):
k = input('Enter Name : ')
v = input('Enter Grade : ')
a[k] = v

print('Given Dictionary is : ')


print(a)

x = 0
for i in a:
if a[i] in 'ABCabc':
x+=1
print('Name : ', i)

print('Number of students are ', x)

8) Create a Dictionary for n number of classes from a user in such a way that it contains names of class as key and
class strength as their values. Display how many class have strength more than 35.
Ans:
n = int(input('Enter Size : '))

a = {}
for i in range(n):
k = int(input('Enter Class : '))
v = int(input('Enter Strength : '))
a[k] = v

print('Given Dictionary is : ')


print(a)
Effective Computer Science 109
x = 0
for i in a:
if a[i] > 35:
x+=1

print('Number of classes are ', x)

9) Write a program that repeatedly asks a user to enter book name and author name and create a Dictionary of all
the given values with keys as ‘book’ and values as ‘author’. When the user is done entering book name and
author name, now allow the user to repeatedly enter a book name and print the corresponding author name or
a message of the book is not in present in the dictionary.
Ans:
a = {}
while True:
k = input('Enter Book Name : ')
v = input('Enter Author Name : ')
a[k] = v
ch = input('Press Y to enter more records : ')
if ch not in 'Yy':
break

print('Given Dictionary is : ')


print(a)
while True:
k = input('Enter Book Name : ')
if k in a:
print('Author Name : ', a[k])
else:
print('Book not found')
ch = input('Press Y to next record : ')
if ch not in 'Yy':
break

10) Write a program that repeatedly asks a user to enter product names and prices. Store all of these in a
dictionary whose keys are the product name and whose values are the prices. When the user is done entering
products and prices, allow them to repeatedly enter a product name and print the corresponding price or a
message of the product is not in the dictionary.
Ans:
a = {}
while True:
k = input('Enter Product Name : ')
v = int(input('Enter Price : '))
a[k] = v
ch = input('Press Y to enter more records : ')
if ch not in 'Yy':
break
print('Given Dictionary is : ')
print(a)

while True:
k = input('Enter Product Name : ')
if k in a:
print('Price : ', a[k])
else:
print('Product not found')
ch = input('Press Y to next record : ')
if ch not in 'Yy':
break
Effective Computer Science 110
11) Write a program that takes a value and check whether the given value is part of given dictionary or not.
Ans:
n = int(input('Enter Size : '))

a = {}
for i in range(n):
k = input('Enter Key : ')
v = input('Enter Value : ')
a[k] = v

print('Given Dictionary is : ')


print(a)

v = input('Enter Value : ')


if v in a.values():
print('Value is present in the dictionary')
else:
print('Value is not present in the dictionary')

12) Write a program to create a dictionary with name of the students as keys and their marks as values. Display
name and marks of top two scorer.
Ans:
a = {}
while True:
k = input('Enter Student Name : ')
v = int(input('Enter Marks : '))
a[k] = v
ch = input('Press Y to enter more records : ')
if ch not in 'Yy':
break

print('Given Dictionary is : ')


print(a)

L = list(a.items())
L.sort(reverse=True)
print('Name of Top Scorer : ', L[0][0])
print('Marks of Top Scorer : ', L[0][1])
print('Name of Second Top Scorer : ', L[1][0])
print('Marks of Second Top Scorer : ', L[1][1])

13) Create dictionary whose keys are month’s names and whose values are the number of days in the
corresponding months.
a. Ask a user to enter a month name and use the dictionary to tell how many days are in the month.
b. Print out the keys in alphabetical order.
c. Print out the months with 31 days.
d. Print out the (key – value) pairs sorted by the number of days in each month.
Ans:
a = {}
for i in range(12):
m = input('Enter name of the month : ')
d = input('Enter number of days in that month : ')
a[m] = d

print('Given Dictionary is : ')


print(a)

m = input('Enter name of the month : ')


if m in a:
Effective Computer Science 111
print('Number of days in the month', m, 'are', a[m])
else:
print('Invalid month name')

print('\nKeys of the dictionary in alphabetical order :')


L = list(a.keys())
L.sort()
for i in L:
print(i, end=',')

print('\nMonths with 31 days:')


L = list(a.items())
for i in L:
if i[1] == 31:
print(i[0], end=',')

print('\nkey–value pairs sorted by the number of days in each month:')


L = list(a.items())
L.sort()
for i in range(len(L)-1):
for j in range(len(L)-1-i):
print(L[j][1])
if L[j][1] > L[j+1][1]:
L[j], L[j+1] = L[j+1], L[j]
print(L)

14) Repeatedly ask a user to enter a team name and how many games the team has win and how many they lost.
Store this information in a dictionary where the keys are the team names and the values are lists of the form
[wins, losses].
a. Using the dictionary allow user to enter a team name and print out the team’s winning percentage.
b. Using the dictionary, create a list whose entries are the number of wins of each team.
c. Using the dictionary, create a list of all those teams that have winning more than loosing.
Ans:
a = {}
while True:
t = input('Enter Team Name : ')
w = int(input('Enter number of winnings : '))
l = int(input('Enter number of loosings : '))
a[t] = [w, l]
ch = input('Press Y to enter more records : ')
if ch not in 'Yy':
break

print('Given Dictionary is : ')


print(a)

t = input('Enter team name to print winning percentage : ')


if t in a:
p = a[t][0]/(a[t][0]+a[t][1])*100
print('Winning percentage = ', p)

print('List containing number of wins of each team : ')


L = []
for i in a.values():
L.append(i[0])
print(L)

print('List containing teams having winning more than loosing : ')


L = []
Effective Computer Science 112
for i in a.items():
if i[1][0] > i[1][1]:
L.append(i[0])
print(L)

15) Given the dictionary A = { 1:‘a’, 2:‘b’, 3:‘c’, 4:‘d’}, create a dictionary with the opposite
mapping, i.e. B = { ‘a’:1, ‘b’:2, ‘c’:3, ‘d’:4 }.
Ans:
A = { 1:'a', 2:'b', 3:'c', 4:'d'}
print('Original Dictionary:')
print(B)

B = {}
for i in A:
B[A[i]] = i

print('Output Dictionary:')
print(B)

16) Write a program that checks if two same values in a dictionary have different keys, i.e.
For dictionary D1 = { ‘a’ : 10, ‘b’ : 20, ‘c’ : 10 }, the program should print “2 keys have same values” and For
dictionary D2 = { ‘a’ : 10, ‘b’ : 20, ‘c’ : 30 }, the program should print “No keys have same values”.
Ans:
n = int(input('Enter Size : '))

a = {}
for i in range(n):
k = input('Enter Key : ')
v = input('Enter Value : ')
a[k] = v

print('Given Dictionary is : ')


print(a)

L = list(a.items())

C = []
y = 0
for i in range(len(L)):
x = 1
if L[i][1] not in C:
C.append(L[i][1])
for j in range(i+1, len(L)):
if L[i][1] == L[j][1]:
x+=1
if x >= 2:
y+=1
if y > 1:
print(y, 'keys have same values')
else:
print('No keys have same values')

17) Create a dictionary with some key:value pairs. Display those keys which are having same values.
Ans:
n = int(input('Enter Size : '))

a = {}
for i in range(n):
k = input('Enter Key : ')
Effective Computer Science 113
v = input('Enter Value : ')
a[k] = v

print('Given Dictionary is : ')


print(a)

L = list(a.items())

C = []
x = 0
M = []
for i in range(len(L)):

if L[i][1] not in C:
y = 0
C.append(L[i][1])
for j in range(i+1, len(L)):
if L[i][1] == L[j][1]:
M.append(L[j][0])
y+=1
print(L[j])
x+=1
if y >=1:
M.append(L[i][0])

if x > 1:
print('Keys having same values are: ')
print(M)
else:
print('No keys have same values')

18) Given two dictionaries day D1 and D2. Write a program that lists the overlapping keys of the two dictionaries,
i.e. if a key of D1 is also a key of D2, list it.
Ans:
m = int(input('Enter Size of first dictionary : '))
print('Enter elements of first dictionary')
a = {}
for i in range(m):
k = input('Enter Key : ')
v = input('Enter Value : ')
a[k] = v

n = int(input('Enter Size of second dictionary : '))


print('Enter elements of second dictionary')
b = {}
for i in range(n):
k = input('Enter Key : ')
v = input('Enter Value : ')
b[k] = v

print('Given Dictionaries are : ')


print(a)
print(b)
L = []
for i in a:
if i in b:
L.append(i)
Effective Computer Science 114

if L == []:
print('No overlapping key found')
else:
print('List of overlapping keys are : ', L)

19) Consider a dictionary points (point={‘a’:(4,3), ‘b’:(1,2), ‘c’:(5,1)}) with single-letter keys, each
followed by a 2-element tuple representing the coordinates of a point in x-y coordinate plane. Write a program
to print maximum value from within all of the values tuples at same index. For example, maximum for 0 th and
1st index is computed in the value-tuple.
Ans:
n = int(input('Enter Size of the dictionary : '))
print('Enter elements of the dictionary')
a = {}
for i in range(n):
k = input('Enter an alphabet : ')
x = int(input('Enter an integer : '))
y = int(input('Enter another integer : '))
a[k] = (x, y)

print('Given Dictionary is : ')


print(a)

M = list(a.values())
L1 = M[0][0]
L2 = M[0][1]
for i in range(1, len(M)):
if M[i][0]> L1:
L1 = M[i][0]
if M[i][1]> L2:
L2 = M[i][1]

print('Largest value at 0th index of the tuple : ', L1)


print('Largest value at 1st index of the tuple : ', L2)

20) Create a Dictionary containing detail of n number of employees with their name as keys and other details i.e.
age, salary and department as value in the form of a List. Display name of those employees how are working in
the department ‘Sales’ and having salary more than 80000.
Ans:
n = int(input('Enter number of employees : '))
print('Enter details of the employees')
a = {}
for i in range(n):
nm = input('Enter Name : ')
ag = int(input('Enter Age : '))
sl = int(input('Enter Salary : '))
dp = input('Enter Department : ')
a[nm] = [ag, sl, dp]

print('Dictionary of employees is : ')


print(a)

for i in a:
if a[i][2] == 'Sales' and a[i][1] > 80000:
print('Name : ', i)

21) Create a Dictionary containing detail of n number of employees with their name as keys and other details i.e.
age, salary and department as value in the form of a List. Display information of the employees after increasing
the salary of the employees by 10% who are working in the department ‘Accounts’.
Effective Computer Science 115
Ans:
n = int(input('Enter number of employees : '))
print('Enter details of the employees')
a = {}
for i in range(n):
nm = input('Enter Name : ')
ag = int(input('Enter Age : '))
sl = int(input('Enter Salary : '))
dp = input('Enter Department : ')
a[nm] = [ag, sl, dp]

print('Dictionary of employees is : ')


print(a)

for i in a:
if a[i][2] == 'Accounts':
a[i][1] = a[i][1] + 0.1*a[i][1]

print('Updated Dictionary is : ')


print(a)

22) Create a Dictionary containing detail of n number of employees with their name as keys and other details i.e.
age, salary and department as value in the form of a List. Display age of those employees whose name starts
with ‘A’ and consists of more than 4 alphabets.
Ans:
n = int(input('Enter number of employees : '))
print('Enter details of the employees')
a = {}
for i in range(n):
nm = input('Enter Name : ')
ag = int(input('Enter Age : '))
sl = int(input('Enter Salary : '))
dp = input('Enter Department : ')
a[nm] = [ag, sl, dp]

print('Dictionary of employees is : ')


print(a)

for i in a:
if i[0] == 'A' and len(i) > 4:
print('Age : ', a[i][0])

23) Create a Dictionary for n number of students having roll numbers from 1 to n as keys and a List of marks of
Physics, Chemistry and Maths subjects as values. After creating the dictionary, display total marks of each
student.
Ans:
n = int(input('Enter number of students : '))
a = {}
for i in range(1, n+1):
print('Enter details of the student', i)
ph = int(input('Enter Marks of Physics : '))
ch = int(input('Enter Marks of Chemistry : '))
ma = int(input('Enter Marks of Maths : '))
a[i] = [ph, ch, ma]

print('Dictionary of students is : ')


print(a)
Effective Computer Science 116
for i in a:
s = 0
for j in a[i]:
s = s + j
print('Roll Number : ', i)
print('Total Marks : ', s)

24) Create a Dictionary for n number of students having their name as keys and a List containing the sequence of
marks of Physics, Chemistry, Maths, English, and Computer subjects as values. Display how many students have
got 100 marks in two or more subjects.
Ans:
n = int(input('Enter number of students : '))
a = {}
for i in range(1, n+1):
print('Enter details of the student', i)
nm = input('Enter name : ')
ph = int(input('Enter Marks of Physics : '))
ch = int(input('Enter Marks of Chemistry : '))
ma = int(input('Enter Marks of Maths : '))
en = int(input('Enter Marks of English : '))
co = int(input('Enter Marks of Computer : '))
a[nm] = [ph, ch, ma, en, co]

print('Dictionary of students is : ')


print(a)

x = 0
for i in a:
y = 0
for j in a[i]:
if j == 100:
y+=1
if y >= 2:
x+=1

print('Number of students have got 100 marks in two or more subjects are : ', x)

25) Create a Dictionary for n number of students having their name as keys and an another dictionary with keys
‘phy’, ‘chem’, ‘math’, ‘eng’, and ‘comp’ as value to store the marks of the subjects of Physics, Chemistry,
Mathematics, English and Computer. Display how many students have failed in two or more subjects (consider
41 as passing marks) also display details of such students.
Ans:
n = int(input('Enter number of students : '))
a = {}
for i in range(1, n+1):
print('Enter details of the student', i)
nm = input('Enter name : ')
ph = int(input('Enter Marks of Physics : '))
ch = int(input('Enter Marks of Chemistry : '))
ma = int(input('Enter Marks of Maths : '))
en = int(input('Enter Marks of English : '))
co = int(input('Enter Marks of Computer : '))
a[i] = {'phy':ph, 'chem':ch, 'math':ma, 'eng':en, 'comp':co}

print('Dictionary of students is : ')


print(a)

print('Students failed in two or more subjects are : ')


Effective Computer Science 117
x = 0
for i in a:
y = 0
for j in a[i]:
if a[i][j] <= 40:
y+=1

if y >= 2:
x+=1
print('Name : ', i)
print('Marks : ', a[i])

print('Number of Students failed in two or more subjects are : ',x)

26) Read marks of Physics, Chemistry, Maths, English, and Computer subjects for n number of students and display
how many students have passed in all the subjects (consider 41 as passing marks) also display details of such
students. read name and make it as key for outer dictionary and subject names as keys for the inner dictionary.
Ans:
n = int(input('Enter number of students : '))
a = {}
for i in range(1, n+1):
print('Enter details of the student', i)
nm = input('Enter name : ')
ph = int(input('Enter Marks of Physics : '))
ch = int(input('Enter Marks of Chemistry : '))
ma = int(input('Enter Marks of Maths : '))
en = int(input('Enter Marks of English : '))
co = int(input('Enter Marks of Computer : '))
a[i] = {'phy':ph, 'chem':ch, 'math':ma, 'eng':en, 'comp':co}

print('Dictionary of students is : ')


print(a)
print('Students failed in two or more subjects are : ')
x = 0
for i in a:
y = 0
for j in a[i]:
if a[i][j] >= 40:
y+=1

if y == 5:
x+=1
print('Name : ', i)
print('Marks : ', a[i])

print('Number of Students failed in two or more subjects are : ',x)

27) Create a Dictionary by reading detail for n number of students having roll numbers from 1 to n as keys of the
dictionary and another dictionary as value with keys as Name, Class and Marks. After creating the dictionary,
display the dictionary and average marks of all the students.
Ans:
n = int(input('Enter number of students : '))
a = {}
for i in range(1, n+1):
print('Enter details of the student', i)
nm = input('Enter Name : ')
cl = int(input('Enter Class : '))
mk = int(input('Enter Marks : '))
a[i] = {'name': nm, 'class':cl, 'marks':mk}
Effective Computer Science 118

print('Dictionary of students is : ')


print(a)

s = 0
for i in a:
s = s+ a[i]['marks']

print('Average Marks of the students = ', s/n)

28) Create a Dictionary by reading detail for n number of students having roll numbers from 1 to n as keys of the
dictionary and another dictionary as value with keys as Name, Class and Marks. After creating the dictionary,
display details of those students who have scored marks more than 90.
Ans:
n = int(input('Enter number of students : '))
a = {}
for i in range(1, n+1):
print('Enter details of the student', i)
nm = input('Enter Name : ')
cl = int(input('Enter Class : '))
mk = int(input('Enter Marks : '))
a[i] = {'name': nm, 'class':cl, 'marks':mk}

print('Dictionary of students is : ')


print(a)

print('Students who have scored marks more than 90 are : ')


for i in a:
if a[i]['marks'] > 90:
print('Roll No : ', i)
print('Details : ', a[i])

29) Create a Dictionary by reading detail for n number of students having roll numbers from 1 to n as keys of the
dictionary and another dictionary as value with keys as Name, Class and Marks. After creating the dictionary,
display how many students have scored below 40 marks.
Ans:
n = int(input('Enter number of students : '))
a = {}
for i in range(1, n+1):
print('Enter details of the student', i)
nm = input('Enter Name : ')
cl = int(input('Enter Class : '))
mk = int(input('Enter Marks : '))
a[i] = {'name': nm, 'class':cl, 'marks':mk}

print('Dictionary of students is : ')


print(a)

x = 0
for i in a:
if a[i]['marks'] < 90:
x+=1

print('Number of Students scored marks below 40 are : ', x)

30) Create a Dictionary by reading detail for n number of students having roll numbers from 1 to n as keys of the
dictionary and another dictionary as value with keys as Name, Class and Marks. After creating the dictionary,
display details of those students who have scored highest and lowest marks.
Effective Computer Science 119
Ans:
n = int(input('Enter number of students : '))
a = {}
for i in range(1, n+1):
print('Enter details of the student', i)
nm = input('Enter Name : ')
cl = int(input('Enter Class : '))
mk = int(input('Enter Marks : '))
a[i] = {'name': nm, 'class':cl, 'marks':mk}

print('Dictionary of students is : ')


print(a)

D = list(a.values())[0]
L = D['marks']
S = D['marks']

for i in a:
if a[i]['marks'] >= L:
L = a[i]['marks']
x = i
elif a[i]['marks'] <= S:
S = a[i]['marks']
y = i

print('Details of Highest Scorer is : ')


print('Roll No. : ', x)
print('Details : ', a[x])

print('Details of Lowest Scorer is : ')


print('Roll No. : ', y)
print('Details : ', a[y])

31) Create a Dictionary by reading detail for n number of students having roll numbers from 1 to n as keys of the
dictionary and another dictionary as value with keys as Name, Class and Marks. After creating the dictionary,
display details of students whose name start with ‘A’ and ends with ‘e’.
Ans:
n = int(input('Enter number of students : '))
a = {}
for i in range(1, n+1):
print('Enter details of the student', i)
nm = input('Enter Name : ')
cl = int(input('Enter Class : '))
mk = int(input('Enter Marks : '))
a[i] = {'name': nm, 'class':cl, 'marks':mk}

print('Dictionary of students is : ')


print(a)

print('Details of students whose name start with ‘A’ and ends with ‘e’ : ')
for i in a:
if a[i]['name'][0] == 'A' and a[i]['name'][-1]:
print('Roll No : ', i)
print('Details : ', a[i])

32) Create a Dictionary by reading detail for n number of students having roll numbers from 1 to n as keys of the
dictionary and another dictionary as value with keys as Name, Class and Marks. After creating the dictionary,
display details of students whose name is “James Bond” and who have scored more than 90 marks.
Effective Computer Science 120
Ans:
n = int(input('Enter number of students : '))
a = {}
for i in range(1, n+1):
print('Enter details of the student', i)
nm = input('Enter Name : ')
cl = int(input('Enter Class : '))
mk = int(input('Enter Marks : '))
a[i] = {'name': nm, 'class':cl, 'marks':mk}

print('Dictionary of students is : ')


print(a)

print('Details of students is : ')


for i in a:
if a[i]['name'] == 'James Bond' and a[i]['marks'] > 90:
print('Roll No : ', i)
print('Details : ', a[i])

33) Create a Dictionary by reading detail for n number of students having roll numbers from 1 to n as keys of the
dictionary and another dictionary as value with keys as Name, Class and Marks. After creating the dictionary,
display the dictionary after increasing marks of the students by 5% who study in class 12 and have sored marks
below 40.
Ans:
n = int(input('Enter number of students : '))
a = {}
for i in range(1, n+1):
print('Enter details of the student', i)
nm = input('Enter Name : ')
cl = int(input('Enter Class : '))
mk = int(input('Enter Marks : '))
a[i] = {'name': nm, 'class':cl, 'marks':mk}

print('Dictionary of students is : ')


print(a)

print('Details of students is : ')


for i in a:
if a[i]['class'] == 12 and a[i]['marks'] < 40:
a[i]['marks'] = a[i]['marks'] + 0.05 * a[i]['marks']

print('Updated Dictionary of students is : ')


print(a)

34) Create a Dictionary by reading detail for n number of students having roll numbers from 1 to n as keys of the
dictionary and another dictionary as value with keys as Name, Class and Marks. After creating the dictionary,
display the dictionary after deleting the students who have scored below 40 marks.
Ans:
n = int(input('Enter number of students : '))
a = {}
for i in range(1, n+1):
print('Enter details of the student', i)
nm = input('Enter Name : ')
cl = int(input('Enter Class : '))
mk = int(input('Enter Marks : '))
a[i] = {'name': nm, 'class':cl, 'marks':mk}

print('Dictionary of students is : ')


print(a)
Effective Computer Science 121

print('Details of students is : ')


for i in a:
if a[i]['marks'] < 40:
a.pop(i)

print('Updated Dictionary of students is : ')


print(a)

35) Create a Dictionary by reading detail for n number of employees having a tuple of employee id and name as
keys of the dictionary and a list as value containing salary and department. After creating the dictionary, display
name of the employees who are working in the department ‘Computer’ and having salary below 50000.
Ans:
n = int(input('Enter number of employees : '))
a = {}
for i in range(1, n+1):
print('Enter details of the employee', i)
ei = int(input('Enter Employee ID : '))
nm = input('Enter Name : ')
sl = int(input('Enter Salary : '))
dp = input('Enter Department : ')
a[(ei, nm)] = [sl, dp]

print('Dictionary of employees is : ')


print(a)

for i in a:
if a[i][1] == 'Computer' and a[i][0] < 50000:
print('Employee ID : ', i[0])
print('Name : ', i[1])
print('Salary : ', a[i][0])
print('Department : ', a[i][1])

36) Create a Dictionary by reading detail for n number of students having a tuple of roll number and name as keys
of the dictionary and another dictionary as value containing marks of three subjects for Physics, Chemistry and
Maths with keys as ‘phy’, ‘chem’ and ‘math’. After creating the dictionary, display name of the student along
with name of the subject and marks, in which he has got highest marks.
Ans:
n = int(input('Enter number of students : '))
a = {}
for i in range(1, n+1):
print('Enter details of the student', i)
rn = int(input('Enter Roll Number : '))
nm = input('Enter Name : ')
ph = int(input('Enter Marks of Physics : '))
ch = int(input('Enter Marks of Chemistry : '))
ma = int(input('Enter Marks of Maths : '))
a[(rn, nm)] = {'phy':ph, 'chem':ch, 'math':ma}

print('Dictionary of students is : ')


print(a)

for i in a:
if a[i]['phy'] > a[i]['chem'] and a[i]['phy'] > a[i]['math']:
print('Name : ', i[1])
print('Subject : Physics, Marks : ', a[i]['phy'])
elif a[i]['chem'] > a[i]['phy'] and a[i]['chem'] > a[i]['math']:
print('Name : ', i[1])
print('Subject : Chemistry, Marks : ', a[i]['chem'])
Effective Computer Science 122
else:
print('Name : ', i[1])
print('Subject : Maths, Marks : ', a[i]['math'])

37) Create two dictionaries with keys ‘length’ and ‘breadth’ to store the details of two rectangles after reading the
values of length and breadth from a user. Create another dictionary to store the length and breadth after
adding the length and breadth of given two rectangles.
Ans:
print('Enter details of first rectangel : ')
l = int(input('Enter length : '))
b = int(input('Enter breadth : '))
r1 = {'length':l, 'breadth':b}

print('Enter details of second rectangel : ')


l = int(input('Enter length : '))
b = int(input('Enter breadth : '))
r2 = {'length':l, 'breadth':b}

r3 = {}
r3['length'] = r1['length'] + r2['length']
r3['breadth'] = r1['breadth'] + r2['breadth']

print('Resultant Dictionary is : ')


print(r3)

38) Create two dictionaries with keys ‘length’ and ‘breadth’ to store the details of two rectangles after reading the
values of length and breadth from a user. Calculate and store area of each rectangle in the dictionary with key
‘area’. Display area of both the rectangles and also display which rectangle is having more area.
Ans:
print('Enter details of first rectangel : ')
l = int(input('Enter length : '))
b = int(input('Enter breadth : '))
r1 = {'length':l, 'breadth':b}
r1['area'] = r1['length'] * r1['breadth']

print('Enter details of second rectangel : ')


l = int(input('Enter length : '))
b = int(input('Enter breadth : '))
r2 = {'length':l, 'breadth':b}
r2['area'] = r2['length'] * r2['breadth']

r3 = {}
r3['length'] = r1['length'] + r2['length']
r3['breadth'] = r1['breadth'] + r2['breadth']
r3['area'] = r1['length'] * r1['breadth']

print('Resultant Dictionary is : ')


print('First Rectangel : ', r1)
print('Second Rectangel : ', r2)
print('Third Rectangel : ', r3)

39) Create two dictionaries with keys ‘hours’, ‘minutes’ and ‘seconds’ to store the details of two time. Create
another dictionary to store the ‘hours’, ‘minutes’ and ‘seconds’ after adding both the given time and display the
dictionary of resultant time in the clock of 12 hours.
Ans:
print('Enter details of first time : ')
h = int(input('Enter Hours : '))
m = int(input('Enter Minutes : '))
s = int(input('Enter Seconds : '))
Effective Computer Science 123
t1 = {'hours':h, 'minutes':m, 'seconds':s}

print('Enter details of second time : ')


h = int(input('Enter Hours : '))
m = int(input('Enter Minutes : '))
s = int(input('Enter Seconds : '))
t2 = {'hours':h, 'minutes':m, 'seconds':s}

t3 = {'hours':0, 'minutes':0, 'seconds':0}

t3['seconds'] = t1['seconds'] + t2['seconds']


if t3['seconds'] >= 60:
t3['minutes'] = t3['seconds']//60
t3['seconds'] = t3['seconds']%60

t3['minutes'] = t3['minutes'] + t1['seconds'] + t2['seconds']


if t3['minutes'] >= 60:
t3['hours'] = t3['minutes']//60
t3['minutes'] = t3['minutes']%60

t3['hours'] = t3['hours'] + t1['hours'] + t2['hours']


if t3['hours'] >= 12:
t3['hours'] = t3['hours']%12

print('First Time is : ', t1)


print('Second Time is : ', t2)
print('Resultant Time is : ', t3)

40) Create two dictionaries with keys ‘km’, ‘mt’ and ‘cm’ to store the details of kilometers, meters and centimeters
of two distances from a user. Create another dictionary to store the kilometers, meters, and centimeters after
adding the dimension of both the given dictionaries and display the resultant distance.
Ans:
print('Enter details of first distance : ')
k = int(input('Enter Kilometers : '))
m = int(input('Enter Meters : '))
c = int(input('Enter Centimeters : '))
d1 = {'km':k, 'mt':m, 'cm':c}

print('Enter details of second distance : ')


k = int(input('Enter Kilometers : '))
m = int(input('Enter Meters : '))
c = int(input('Enter Centimeters : '))
d2 = {'km':k, 'mt':m, 'cm':c}

d3 = {'km':0, 'mt':0, 'cm':0}

d3['cm'] = d1['cm'] + d2['cm']


if d3['cm'] >= 100:
d3['mt'] = d3['cm']//100
d3['cm'] = d3['cm']%100

d3['mt'] = d3['mt'] + d1['mt'] + d2['mt']


if d3['mt'] >= 1000:
d3['km'] = d3['mt']//1000
d3['mt'] = d3['mt']%1000

d3['km'] = d3['km'] + d1['km'] + d2['km']


Effective Computer Science 124
print('First Distance is : ', d1)
print('Second Distance is : ', d2)
print('Resultant Distance is : ', d3)

41) Create a dictionary with the keys ‘dd’, ‘mm’ and ‘yy’ to store the value of date, month and year and number of
days from a user. from a user. Create another dictionary to store the value of date, month and year after
adding the number of days in the given date.
Ans:
print('Enter details of a date : ')
d = int(input('Enter Date : '))
m = int(input('Enter Month : '))
y = int(input('Enter Year : '))
d1 = {'dd':d, 'mm':m, 'yy':y}

n = int(input('Enter number of days to be added in the date: '))

d2 = {'dd':0, 'mm':0, 'yy':0}

d2['dd'] = d1['dd'] + n
if d2['dd'] > 30:
d2['mm'] = d2['dd']//30
d2['dd'] = d2['dd']%30

d2['mm'] = d2['mm'] + d1['mm']


if d2['mm'] >= 12:
d2['yy'] = d2['mm']//12
d2['mm'] = d2['mm']%12

d2['yy'] = d2['yy'] + d1['yy']

print('Original Date is : ', d1)


print('Resultant Date is : ', d2)

42) Write a program in Python to read a string from a user and create a dictionary having words as keys and length
of each word as values.
Ans:
a = input('Enter a string : ')
print('Original string is : ', a)
a = a.split()

d = {}
for i in a:
d[i] = len(i)

print('Resultant Dictionary is : ')


print(d)

43) Write a program in Python to read a string from a user and create a dictionary having words which starts with a
vowel as keys and reverse of these words as values.
Ans:
a = input('Enter a string : ')
print('Original string is : ', a)
a = a.split()

d = {}
for i in a:
if i[0] in 'AEIOUaeiou':
d[i] = i[::-1]
Effective Computer Science 125
print('Resultant Dictionary is : ')
print(d)

44) Write a program in Python that will read a string from a user and display a dictionary containing how many
vowel (both upper and lower case) are present in the string. In the dictionary, all the vowels are keys and their
count are values.
For example, if the string is "Come let us have some fun",
The dictionary will have {‘a’:1 , ‘e’:3 , ‘i':0 , ‘o’:2 , ‘u’:2 }
Ans:
a = input('Enter a string : ')
print('Original string is : ', a)

d = {'a':0, 'e':0, 'i':0, 'o':0, 'u':0}


for i in d:
x = 0
for j in a:
if i == j or i.upper() == j:
x+=1
d[i] = x

print('Resultant Dictionary is : ')


print(d)
Effective Computer Science 126

Chapter : 12
Python Modules

 Theoretical /Conceptual Problems

6) Write Python representation for the following mathematical expressions:


1. (a+b)2 = a2 + b2 + 2ab
Ans: math.pow((a + b),2) = math.pow(a, 2) + math.pow(b, 2) + 2*a*b
2. sin2a + cos2a = 1
Ans: math.pow(math.sin(a), 2) + math.pow(math.cos(a), 2) = 1
3. log sin| x2|
Ans: math.log(math.sin(math.fabs(math.pow(x, 2))))
4. −𝒃+√𝒃𝟐 −𝟒𝒂𝒄
sin 𝒙 =
𝟐𝒂
Ans: math.sin(x)= (-b + math.sqrt(math.pow(b, 2) - 4*a*c)) / 2*a
5. 𝒓 𝒏𝒕
𝑨 = 𝑷 (𝟏 + )
𝒏
Ans: A = P*(math.pow((1+r/n), n*t))
6. 𝒏
𝒔𝒊𝒏 √𝒂𝒏
Ans: math.sin(math.pow(math.pow(a, n), 1//n))
7.
𝒆−𝟏∗𝐥𝐨𝐠 𝐬𝐢𝐧 𝒙
Ans: math.exp(-1* math.log(math.sin(x)))
8. 𝒏(𝒏 − 𝟏)𝒙𝟐
(𝟏 + 𝒙 )𝒏 =𝟏+
𝐥𝐨𝐠 𝒙𝒏
Ans: math.pow((1+x), n) = 1 + n*(n-1)*math.pow(x, 2) / math.log(math.pow(x, n))
9. 𝒔𝒊𝒏 𝒂𝒏 𝒍𝒐𝒈 𝒂𝒎 = 𝒂𝒏+𝒎
Ans: math.sin(math.pow(a, n)) * math.log(math.pow(a, m)) = math.pow(a, (n+m))
10. 𝟏⁄ 𝒏
|𝒂 𝒏 = √𝐬𝐢𝐧 𝒂 + 𝒄𝒐𝒔 𝒂 |
Ans: math.fabs(math.pow(a, 1//n) = math.pow(math.sin(a) + math.cos(a), i//n))
7) A list named studentMarks stores marks of the students of a class. Write the Python command to import the
required module and (using built-in function) to display the most common value of the marks from the given list.
Ans: import statistics
print(statistics.mode(studentmarks))

8) A list named studentMarks stores marks of the students of a class. Write the Python command to import the
required module and (using built-in function) to display a random value from the given list.
Ans: import random
print(random.choice(studentmarks))
Effective Computer Science 127

Answers of Objective Type Questions

Chapter 1 : Computer Systems and Organisation

1 a 2 c 3 d 4 a 5 d 6 c 7 a 8 d 9 a 10 d
11 b 12 c 13 a 14 b 15 a 16 c 17 d 18 b 19 d 20 a

Chapter 2 : Number System

1 c 2 a 3 c 4 d 5 d 6 C 7 D 8 D 9 C 10 A
11 a 12 c 13 b 14 c 15 d

Chapter 3: Boolean Algebra

1 d 2 b 3 b 4 d 5 a 6 D 7 C 8 D 9 A 10 B
11 d 12 a 13 c 14 b 15 a 16

Chapter 4 : Introduction To Problem Solving

1 b 2 d 3 a 4 c 5 c 6 b 7 c 8 d 9 d 10 a

Chapter 5 : Introduction to Python Programming

1 c 2 b 3 b 4 c 5 d 6 a 7 a 8 a 9 c 10 c
11 d 12 d 13 d 14 c 15 d 16 c 17 d 18 d 19 c 20 d
21 a 22 a 23 b 24 d 25 a 26 c 27 a 28 a 29 d 30 d
31 a 32 c 33 b 34 d 35 a 36 b 37 d 38 d 39 d 40 b
41 b 42 d 43 c 44 d 45 a 46 d 47 d 48 a 49 d 50 d
51 b 52 b 53 a 54 b 55 a 56 d 57 a 58 d 59 d 60 a
61 a 62 a 63 d 64 a 65 d 66 c 67 d 68 a 69 b 70 a
71 d 72 c 73 b 74 c 75 a 76 c 77 d 78 a 79 b 80 c
81 b 82 d 83 c 84 a 85 b 86 b

Chapter 6 : Flow of Control

1 c 2 d 3 d 4 a 5 d 6 a 7 d 8 d 9 a 10 a
11 a 12 d 13 f 14 b 15 c 16 d 17 d 18 a 19 c 20 a

Chapter 7 : Strings

1 B 2 A 3 B 4 B 5 D 6 B 7 C 8 A 9 B 10 D
11 B 12 D 13 A 14 D 15 D 16 D 17 A 18 D 19 A 20 C
21 B 22 D 23 C 24 A 25 B 26 D 27 A 28 B 29 C 30 D
31 A 32 A
Effective Computer Science 128
Chapter 8 : Tuples
1 B 2 A 3 C 4 C 5 C 6 B 7 A 8 B 9 B 10 D
11 B 12 A 13 D 14 B 15 A 16 A 17 B 18 B 19 D 20 D
21 C 22 D 23 D 24 D 25 C 26 A 27 A

Chapter 9 : Lists

1 d 2 c 3 b 4 c 5 d 6 a 7 a 8 c 9 b 10 a
11 c 12 d 13 b 14 c 15 c 16 d 17 c 18 d 19 d 20 b
21 c 22 d 23 c 24 a 25 c 26 d 27 b 28 d 29 d 30 b
31 c 32 b 33 c 34 d 35 c 36 d 37 a 38 b 39 d 40 c
41 a 42 a 43 b 44 d 45 a 46 a

Chapter 10 : Dictionaries

1 d 2 c 3 a 4 a 5 1 6 b 7 b 8 c 9 d 10 d
11 c 12 d 13 b 14 d 15 d 16 a 17 a 18 a 19 d 20 a
21 d 22 b 23 b 24 a 25 d 26 a 27 c 28 d

Chapter 11 : Data Structure Algorithms

1 d 2 d 3 b 4 a 5 b

Chapter 12 : Python Modules

1 b 2 c 3 b 4 b 5 b 6 a 7 a 8 a 9 c 10 B
11 d 12 d 13 a 14 d 15 b 16 b 17 a 18 d 19 a 20 D
21 d 22 c 23 d 24 b,d 25 b 26 a 27 d 28 b 29 d

Chapter 13 : Society, Law and Ethics

1 d 2 d 3 a 4 a 5 d 6 c 7 d 8 b 9 a 10 c
11 a 12 b 13 c 14 a 15 b 16 c 17 b 18 b 19 d 20 a
21 c 22 d 23 d 24 d 25 b 26 c 27 b 28 b 29 b 30 c

Answers of Assertion And Reasoning Based Questions


Effective Computer Science 129

Chapter 1 : Computer Systems and Organisation


1 b 2 d 3 d 4 b 5 c 6 c 7 a 8 b 9 a 10 a
11 a 12 a 13 b 14 a 15 b 16 a 17 a

Chapter 2 : Number System


1 b 2 b 3 a 4 a 5 a

Chapter 3 : Boolean Algebra


1 b 2 a 3 c 4 b 5 c 6 b 7 a 8 c 9 b 10 b

Chapter 4 : Introduction To Problem Solving


1 a 2 a 3 a 4 a 5 b

Chapter 5 : Introduction to Python Programming


1 a 2 b 3 a 4 c 5 c 6 b 7 b 8 b 9 a 10 a
11 b 12 d 13 b 14 b 15 c 16 a 17 a 18 d 19 c 20 d
21 b 22 a 23 b 24 c 25 a 26 d 27 b 28 d 29 d 30 A
31 c 32 d

Chapter 6 : Flow of Control


1 b 2 d 3 a 4 a 5 a 6 a 7 b 8 a 9 d 10 d

Chapter 7 : Strings
1 d 2 a 3 b 4 c 5 b 6 a 7 d 8 b 9 d 10 b

Chapter 8 : Tuples
1 b b d 3 b 4 d 5 b

Chapter 9 : Lists
1 c 2 b 3 a 4 b 5 c

Chapter 10 : Dictionaries
1 d 2 a 3 c 4 b 5 d 6 b 7 d 8 c 9 a 10 d

Chapter 11 : Data Structure Algorithms


1 A 2 A 3 C 4 b 5 b

Chapter 12 : Python Modules


1 a 2 b 3 b 4 a 5 a

chapter 13 : Society, Law and Ethics


1 a 2 b 3 b 4 c 5 a 6 a 7 b 8 a 9 c 10 d
11 d 12 b 13 a 14 d 15 a 16 b 17 c 18 b 19 d 20 d

You might also like