SARASWATI COMPUTER CLASSES
M3R5 (Python) Practical Questions By: Kshitij Singh
Question 1. Write a program to print all Armstrong numbers in a given range. Note: An Armstrong number is a number
whose sum of cubes of digits is equal to the number itself. E.g. 370=33+73+03.
s=int(input("Input start number : "))
e=int(input("Input end number : "))
for i in range(s,e+1):
num=i
temp = num
sum = 0
while num>0:
rem = num%10
sum = sum+(rem**3)
num = num//10
if temp == sum:
print(temp," ")
Question [Link] a function to obtain sum n terms of the following series for any positive integer value of X: X +X3 /3!
+X5 /5! ! +X7 /7! + …
def power(a,b):
p=1
for i in range(1,b+1):
p=p*a
return p
def factorial (n):
f=1
for i in range(1,n+1):
f=f*1
return f
sum=0
n=int(input(“enter the number of terms”))
x=int(input(“enter the value of x”))
Question 3. Write a program to multiply two numbers by repeated addition e.g. 6*7 = 6+6+6+6+6+6+6
num1=int(input("Enter first number "))
num2=int(input("Enter second number "))
product=0
for i in range (1,num2+1):
product=product+num1
print("The multiply is: ",product)
Question 4. Write a program to compute the wages of a daily laborer as per the following rules :-
Hours Worked Rate Applicable Upto first 8 hrs Rs100/-
a) For next 4 hrs Rs30/- per hr extra
b) For next 4 hrs Rs40/- per hr extra
c) For next 4 hrs Rs50/- per hr extra
d) For rest Rs60/- per hr extra
def calculate_wages(hours_worked):
wages = 0
if hours_worked <= 8:
wages = hours_worked * 100
else:
# First 8 hours
wages = 8 * 100
remaining_hours = hours_worked - 8
# Next 4 hours at Rs 30/hr
if remaining_hours > 0:
extra_hours = min(4, remaining_hours)
wages += extra_hours * 30
remaining_hours -= extra_hours
# Next 4 hours at Rs 40/hr
if remaining_hours > 0:
extra_hours = min(4, remaining_hours)
wages += extra_hours * 40
remaining_hours -= extra_hours
# Next 4 hours at Rs 50/hr
if remaining_hours > 0:
extra_hours = min(4, remaining_hours)
wages += extra_hours * 50
remaining_hours -= extra_hours
# Remaining hours at Rs 60/hr
if remaining_hours > 0:
wages += remaining_hours * 60
return wages
hours = int(input("Enter the number of hours worked: "))
total_wage = calculate_wages(hours)
print(f"Total wages for {hours} hours worked is Rs {total_wage}")
Questions 5. Write a function that takes a string as parameter and returns a string with every successive repetitive
character replaced by ?e.g. school may become scho?l.
def str_encode(s):
ans=""
for a in s:
if a not in ans:
ans=ans+a
else:
ans=ans+'&'
return ans
st1=input("Enter any String :")
output=str_encode(st1)
print("Output is : ",output)
Question 6. Write a program that takes in a sentence as input and displays the number of words, number of capital
letters, no. of small letters and number of special symbols.
str = "Hell0 W0rld ! 123 * #"
print("Original strings : ",str)
upr, lwr, num, spl = 0, 0, 0, 0
for i in range(len(str)):
if str[i] >= 'A' and str[i] <= 'Z':
upr += 1
elif str[i] >= 'a' and str[i] <= 'z':
lwr += 1
elif str[i] >= '0' and str[i] <= '9':
num += 1
else:
spl += 1
print("UpperCase : ",upr)
print("LowerCase : ",lwr)
print("NumberCase : ",num)
print("SpecialCase : ",spl)
Question [Link] a program which takes list of numbers as input and finds:
a) The largest number in the list
b) The smallest number in the list
c) Product of all the items in the list
mylist = []
tn = int(input("How many numbers do you want to enter? "))
for i in range(tn):
item = int(input("Enter a number: "))
[Link](item)
print("Greatest number =", max(mylist))
print("Smallest number =", min(mylist))
product = 1
for i in mylist:
product *= i
print("Product =", product)
Question 8. Write a Python function that takes two lists and returns True if they have at least one common item.
a = [1, 2, 3, 4]
b = [5, 6, 3, 8]
# Find common elements using filter and set
common = set(filter(lambda x: x in b, a))
if common:
print("Common", common)
else:
print("No common elements.")
Question 9. Write a Python program to combine two dictionary adding values for common keys.
d1 = {‘a’: 100, ‘b’: 200, ‘c’:300}
d2 = {‘a’: 300, ‘b’: 200, ‘d’:400}
Sample output: Counter({‘a’: 400, ‘b’: 400, ‘d’: 400, ‘c’: 300})
from collections import Counter
dict1 = {'a': 100, 'b': 200, 'c':300}
dict2 = {'a': 300, 'b': 200, 'd':400}
new_dict = Counter(dict1) + Counter(dict2)
print("The new dict is:", new_dict)
Question 10. Write a python program to make calculator.
n1 = float(input("Enter the First Number: "))
n2 = float(input("Enter the Second Number: "))
print("{} + {} = ".format(n1, n2))
print(n1 + n2)
print("{} - {} = ".format(n1, n2))
print(n1 - n2)
print("{} * {} = ".format(n1, n2))
print(n1 * n2)
print("{} / {} = ".format(n1, n2))
print(n1 / n2)
Question 11. prime number up to N number
lower_value = int(input ("Please, Enter the Lowest Range Value: "))
upper_value = int(input ("Please, Enter the Upper Range Value: "))
print ("The Prime Numbers in the range are: ")
for number in range (lower_value, upper_value + 1):
if number > 1:
for i in range (2, number):
if (number % i) == 0:
break
else:
print (number)
Question 12. Write a Python function that takes two lists and returns True if they have at least one common item.
numbers=[4,2,7,1,8,3,6]
print("Original lists item: ",numbers)
chk=int(input("Enter that item which you want to check"))
if chk in numbers:
print("Number is present in list")
else:
print("Number Does not exist")
[Link](chk)
print("Item added Succussfully: ", numbers)
Question 13. Write a NumPy program to find the most frequent value in an array.
import numpy as np
x = [Link]([1,2,3,4,5,1,2,1,1,1])
print("Original array:")
print(x)
print("Most frequent value in the above array:")
print([Link](x).argmax())
Question 14. Write a program to find the number is even or odd.
no=int(input("enter any number"))
if(no%2==0):
print("even")
else:
print("odd")
Question 15. Write a program to print your name 100 times.
i=1
while(i<=100):
i=i=1
print("name")
Question 16. Write a program to check whether the given number is Armstrong or not.
n=int(input("enter any number"))
t=n
r=0
while(n>0):
a=n%10
r=r+(a*a*a)
n=n//10
if(r==t):
print("is armstrong number")
else:
print("is not a armstrong number")
Question 17. Write a python program to check whether the given number is neon or not.
num=int(input("enter any number"))
sq=num*num
sum=0
while(sq>0):
sum=sum+sq%10
sq=sq//10
if(sum==num):
print("neon number")
else:
print("not a neon number")
Question18. Write a program to swap two numbers.
x=int(input("enter value of x")) x=int(input("enter value of x"))
y=int(input("enter value of y")) y=int(input("enter value of y"))
print("before swapping") print("before swapping")
print("x=",x) print("x=",x)
print("y=",y) print("y=",y)
temp=x
or x=x+y
x=y y=x-y
y=temp x=x-y
print("after swapping") print("after swapping")
print("x=",x) print("x=",x)
print("y=",y) print("y=",y)
Question 19. Fibonacci series
a=0
b=1
n=int(input("enter n how many times you want tp generate the series"))
for i in range(n):
c=a+b
a=b
b=c
print(c)
Question 20. Write a program to calculate and print the following series
1,4,9,16…………………225
for i in range(1,16):
b=i*i
print(b)
Question 21. write a program to check whether the given number is palindrome or not. (mirror number-151,555,121)
n=int(input("enter any number"))
temp=n
rev=0
while(n>0):
d=n%10
rev=rev*10+d
n=n//10
if(temp==rev):
print("number is palindrome")
else:
print("not a palindrome number")
Question 22. Write a program to find the given number is perfect number or not. (6 is a perfect number cz it
divide by 1,2,3)
i=1
sum=0
n=int(input("enter any number"))
while(i<n):
if(n%i==0):
sum=sum+i
i=i+1
if(sum==n):
print("perfect number")
else:
print("not a perfect number")
Question 23. Write a program to check whether a given year is leap year or not.
y=2028
if (y % 4 == 0 and y % 100 != 0) or (y % 400 == 0):
print("Leap year")
else:
print("Not a leap year")
Question 24. Write a program to enter a number and print its binary, octal and hexadecimal.
num=int(input("enter any number"))
print("octal value",oct(num))
print("hexadecimal value",hex(num))
print("binary value",bin(num))
Question24. Write a program to create a list of 10 numbers and print all uts items and count how many them are
positive, negative or zeros.
p=n=z=0
num=[34,-89,90,0,8,9,67,97,0,-14]
print("list items",num)
for a in num:
if(a>0):
p=p+1
if(a<0):
n=n+1
if (a==0):
z=z+1
print("total positive numbers",p)
print("total negetive numbers",n)
print("total zeros",z)
Questions25. Write a program to convert Celsius to Fahrenheit.
cel=int(input("enter temprature in C"))
fah=cel*1.8+32
print("fahrenhiet=",fah)
Question26. Write a program to find the disarium number.(ex: 135=1^1+13^2+5^3)
num = int(input("Enter a number: "))
temp = num
sum_val = 0
digits = list(map(int, str(num)))
for i in range(len(digits)):
sum_val += digits[i] ** (i + 1)
if sum_val == num:
print(num, "is a Disarium Number")
else:
print(num, "is not a Disarium Number")
Question27. Write a program to find all disarium number between 1 to 200.
print("Disarium numbers between 1 and 200 are:")
for num in range(1, 201):
sum=0
digits = list(map(int, str(num)))
for i in range(len(digits)):
sum+= digits[i] ** (i + 1)
if sum==num:
print(num)
[Link] a python program to reverse of digits of an integer number using while loop.
num=int(input("enter a integer"))
rev=0
while num!=0:
digit=num%10
rev=rev*10+digit
num=num//10
print("reversed number=",rev)
[Link] a program to count the number of character in a string “[Link]”
string="[Link]"
count={}
for char in string:
if char in count:
count[char]+=1
else:
count[char]=1
print(count)
Question28. Python program to find number of days between to given dates
from datetime import date
def number_of_days(date_1,date_2):
return abs((date_1-date_2).days)
date_1=date(2024,6,12)
date_2=date(2023,1,30)
print("number of days between the given days are",number_of_days(date_1,date_2),"days")
Question29. Write a program to test whether a passed is vowel or not.
char=input("enter a character")
vowels=['a','e','i','o','u','A','E','I','O','U']
if char in vowels:
print("the character",[char],"is a vowel")
else:
print("the character",[char],"is not a vowel")
[Link] a program to find intersection of two arrays?
For example; sample input
arr1[]=[1,2,3,4,7];
arr2[]=[2,3,5,6];
the intersection is [3,5]
arr1=[1,3,4,5,7]
arr2=[2,3,5,6]
intersection=list(set(arr1)&set(arr2))
print(intersection)