Sir Padampat Singhania Education Centre
Kamla Nagar, Kanpur
Sheet No. 9
Subject: Computer Science Lesson: Python Class: VIII
E. Perform the following programs using python:
Q1. Write a program to display the name of a color based on the corresponding
VIBGYOR color code.
a=input("Enter a color code of rainbow color:")
if a=='v' or a=='V':
print("Voilet")
elif a=='i' or a=='I':
print("Indigo")
elif a=='b' or a=='B':
print("Blue")
elif a=='g' or a=='G':
print("Green")
elif a=='y' or a=='Y':
print("Yellow")
elif a=='o' or a=='O':
print("Orange")
elif a=='r' or a=='R':
print("Red")
else:
print("Invalid color code!")
Q2. Write a program to print the squares of numbers from 10 to 20.
Using for loop Using while loop
for x in range(10,21,1): x=10
print(“The square of”, x , “is:” , x * x) while x<=20:
print(“The square of”, x , “is:” , x * x)
x=x+1
Q3. Write a program to print the table of a number that has been entered by a user using
both for and while loop.
Using for loop Using while loop
num = int(input("Enter the number: ")) num = int(input("Enter the number: "))
print("Multiplication Table of", num) print("Multiplication Table of", num)
for i in range(1, 11,1): i=1
print(num , "x" , i , "=" , num * i) while i<=10:
print(num , "x" , i , "=" , num * i)
i=i+1
Q4. Write a program to enter the sides of a triangle and display the kind of triangle it is.
s1=int(input("enter first side"))
s2=int(input("enter second side"))
s3=int(input("enter third side"))
if s1==s2==s3:
print("It is an Equilateral Triangle")
elif s1==s2 or s2==s3 or s1==s3:
print("It is an Isosceles Triangle")
else:
print("It is a Scalene Triangle")
Q5. Write a program to enter a number and to check if it a multiple of 7 or not.
i=int(input("Enter a number:"))
if i%7==0:
print(i, "is a multiple of 7")
else:
print(i, "is not a multiple of 7")
Q6. Write a program to enter three numbers and print the largest out of the three.
n1=int(input("Enter first number:"))
n2=int(input("Enter second number:"))
n3=int(input("Enter third number:"))
if n1>n2 and n1>n3:
print(n1, "is the greatest number.")
elif n2>n1 and n2>n3:
print(n2, "is the greatest number.")
elif n3>n1 and n3>n2:
print(n3, "is the greatest number.")
else:
print("Your entries have equal numbers.")
Q7. Write a program to accept the age from the user and tell whether the person is a
teenager or not.
n = input("Enter your name:")
age = int(input("Enter your age:"))
if age >= 13 and age <= 19:
print(n, "is a teenager.")
else:
print(n, "is not a teenager.")
Q8. Write a program to accept the name, class and marks in three subjects of a child.
Display the total and average of the marks and name and class of the child.
n = input("Enter your name:")
cl = input("Enter your class:")
m1 = int(input("Enter marks of first subject:"))
m2 = int(input("Enter marks of second subject:"))
m3 = int(input("Enter marks of third subject:"))
total = m1 + m2 + m3
average = total/3
print(n , “, you belong to class” , cl)
print(total, "is the total marks of three subjects.")
print(average, "is the average of three subjects.")
Q9. Write a program to enter a string from the user and print it 10 times using both for
and while.
Using For Loop Using While Loop
h=input("Enter string:") h = input("Enter string:")
for i in range(1,11,1):
i=0
print(h)
while i<10:
i=i+1
print(h)
Q10. Write a program to generate a calculator for two numbers.
Answer:
select = int(input("Select operations form 1, 2, 3, 4 :"))
number_1 = int(input("Enter first number: "))
number_2 = int(input("Enter second number: "))
if select == 1:
print(number_1 , “+” , number_2, "=", number_1 + number_2)
elif select == 2:
print(number_1, "-", number_2, "=", number_1 - number_2)
elif select == 3:
print(number_1, "*", number_2, "=",number_1 * number_2)
elif select == 4:
print(number_1, "/", number_2, "=",number_1 / number_2)
else:
print("Invalid input")
Q11. Write a program to enter length and breadth of a rectangle and print its perimeter
and area.
l = int(input("Enter Length:"))
b = int(input("Enter Breadth:"))
area = l*b
print("Area is", area)
perimeter = 2*(l+b)
print("Perimeter is",perimeter)
Q12. Write a program to accept user’s name and surname in two separate variables, and
then concatenate and print them.
n = input("Enter your first name:")
s = input("Enter your surname:")
full_name=n+s
print(full_name)
Q13. Write a program to print cubes of numbers from 10 to 20 using both for and while.
Using for loop Using while loop
for x in range(10,21,1): i = 10
print(“The cube of” ,x, “is:” , x * x* x) while i<=20:
print(“The cube of” , i , “is:” , i*i*i)
i=i+1
Q14. Write a program to print the numbers in reverse order from 15 to 3 using both for
and while.
Using For Loop Using While Loop
for i in range(15,2,-1): i=15
print(i) while i>=3:
print(i)
i=i-1
Q15. Write a program to print 10 to 200 skipping 5 during the same using both for and
while.
Using For Loop Using While Loop
for i in range(10,201,5): i=10
print(i) while i<=200:
print(i)
i=i+5