0% found this document useful (0 votes)
5 views43 pages

Python Programs

The document contains a series of Python programs designed for various tasks, including displaying personal information, calculating areas and perimeters, converting units, and performing financial calculations. Each program includes a solution with code, expected output, and user input prompts. The programs cover a wide range of topics suitable for educational purposes in programming and mathematics.

Uploaded by

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

Python Programs

The document contains a series of Python programs designed for various tasks, including displaying personal information, calculating areas and perimeters, converting units, and performing financial calculations. Each program includes a solution with code, expected output, and user input prompts. The programs cover a wide range of topics suitable for educational purposes in programming and mathematics.

Uploaded by

vibbumak
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Python Programs

NAME = Himnik Sai Makhija


Class= 11thA
Roll no. = 4

[Link] to display name, previous school marks in each subject.


Solution:
print('Himnik Sai Makhija')
print('Siddharth International School')
print('science-95')
print('maths-79')
print('social-97')
print('english-89')
print('Hindi-92')
output:
Himnik Sai Makhija
Siddharth International School
science-95
maths-79
social-97
english-89
Hindi-92

[Link] to accept your age and display the same.


Solution:
age=int(input('enter your age'))
print('entered age is=',age)
output:
enter your age16
entered age is= 16

[Link] to display area of square of side 5 units.


Solution:
a=5
A=5*5
print('side of a square is=',a)
print('area of the square is=',A)
output:
side of a square is= 5
area of the square is= 25
4. WAP to display perimeter of rectangle whose length and breadth is 3
and 4 units respectively.
Solution:
l=4
b=5
print('length of rectangle is=',l)
print('breadth of rectangle is=',b)
P=2*l+2*b
print('perimeter of rectangle=',P)
output:
length of rectangle is= 4
breadth of rectangle is= 5
perimeter of rectangle= 18

[Link] to display the area and circumference of circle with radius 6 units.
Solution:
r=4
pi=3.14
A=pi*r*r
C=2*pi*r
print('radius of a circle=',r)
print('area of a circle=',A)
print('circumference of a circle=',C)
output:
radius of a circle= 4
area of a circle= 50.24
circumference of a circle= 25.12

[Link] to accept the side of a square from the user and display area and
perimeter.
Solution:
s=int(input('enter a value for side of a square'))
P=4*s
A=s*s
print('perimeter of the square=',P)
print('area of a square=',A)
output:
enter a value for side of a square10
perimeter of the square= 40
area of a square= 100

[Link] to accept the base and height from the user and display area of
triangle.
Solution:
b=int(input('enter a value for base of a triangle'))
h=int(input('enter a value for height of a triangle'))
A=b*h/2
print('the area of the triangle is=',A)
output:
enter a value for base of a triangle20
enter a value for height of a triangle25
the area of the triangle is= 250.0

8. WAP to accept the radius of the circle and display area and
circumference.
Solution:
r=int(input('enter a value for radius of a circle'))
pi=3.14
A=pi*r*r
C=2*pi*r
print('area of a circle is=',A)
print('circumference of circle=',C)
output:
enter a value for radius of a circle5
area of a circle is= 78.5
circumference of circle= 31.400000000000002

[Link] to accept name and roll number and also accept the 5 subjects
marks in class 10 and display total marks and percentage.
Solution:
n=input('enter your name')
r=int(input('enter your roll number'))
S=int(input('enter your marks in social'))
Sc=int(input('enter your marks in science'))
Sa=int(input('enter your marks in Hindi'))
E=int(input('enter your marks in english'))
M=int(input('enter your marks in maths'))
T=S+Sc+Sa+M+E
P=T/500*100
print('your total marks is=',T)
print('your percentage is=',P)
output:
enter your nameArchit Bhatnagar
enter your roll number2
enter your marks in social97
enter your marks in science95
enter your marks in Hindi92
enter your marks in english89
enter your marks in maths79
your total marks is= 452
your percentage is= 90.40

[Link] to accept two numbers from the user and display remainder and
quotient.
Solution:
num1=int(input('enter a value for the first number'))
num2=int(input('enter a value for the second number'))
D=num1/num2
R=num1%num2
print('the quotient is=',D)
print('the remainder is=',R)
output:
enter a value for the first number14
enter a value for the second number13
the quotient is= 1.0769230769230769
the remainder is= 1

[Link] to accept temp n celcius and convert it to farenheit


Solution:
t=int(input('enter the temperature in degree celcius'))
F=9/5*t+32
print('the temperature in farenheitis',F)
output:
enter the temperature in degree celcius28
the temperature in farenheit is 82.4

[Link] to read a number and print its square, cube and quadruple.
Solution:
n=int(input('enter a value for n'))
n1=n**2
n2=n**3
n3=n**4
print('the square of the number entered is',n1)
print('the cube of the number entered is',n2)
print('the quartic of the number entered is',n3)
output:
enter a value for n17
the square of the number entered is 289
the cube of the number entered is 4913
the quartic of the number entered is 83521

13. WAP to calculate simple and compound interest .


Solution:
p=int(input('enter a value for principle amount'))
r=int(input('enter a value for rate of interest'))
t=int(input('enter a value for the time period'))
n=int(input('enter a value for the number of times the amount is compounded'))
SI=p*r*t/100
CI=p*(1+r/n)**n*t
print('the simple interest is=',SI)
print('the Compound interest is=',CI)
output:
enter a value for principle amount30000
enter a value for rate of interest5
enter a value for the time period2
enter a value for the number of times the amount is compounded3
the simple interest is= 3000.0
the Compound interest is= 1137777.7777777782

14. WAP to input a number and print its first 5 multiples.


Solution:
n=int(input('enter the value for a number'))
n1=n*1
n2=n*2
n3=n*3
n4=n*4
n5=n*5
print('the first multiple of the number entered is=',n1)
print('the second multiple of the number entered is=',n2)
print('the third multiple of the number entered is=',n3)
print('the fourth multiple of the number entered is=',n4)
print('the fifth multiple of the number entered is=',n5)
output:
enter the value for a number5
the first multiple of the number entered is= 5
the second multiple of the number entered is= 10
the third multiple of the number entered is= 15
the fourth multiple of the number entered is= 20
the fifth multiple of the number entered is= 25

15. WAP that asks height in cm and converts it to feet and inches.
Solution:
h=int(input('enter your height in cms'))
I=0.394*h
F=0.328*h
print('your height in foot and inches is',F,I)
output:
enter your height in cms154
your height in foot and inches is 50.512 60.676

16. WAP to read details like name class and age then print details firstly
in the same line then in separate lines. Make sure to have two blank
lines between these 2 types of print.
Solution:
n=input('enter your name')
a=int(input('enter your age'))
c=int(input('enter your class'))
print('your name',n,'yourage',a,'yourclass',c,)
print()
print('your name',n,'\n''yourage',a,'\n','yourclass',c)
output:
enter your nameArchit Bhatnagar
enter your age15
enter your class11
your name Archit Bhatnagar your age 15 your class 11

your name Archit Bhatnagar


your age 15
your class 11

17. WAP to read 3 numbers in 3 variabls and swap first two variables
with the sums of first and second, second and third numbers
respectively.
Solution:
a=int(input('enter a value for the first number'))
b=int(input('enter a value for the second number'))
c=int(input('enter a value for the third number'))
a=a+b
b=b+c
print('the swapped number is=',a,b)
output:
enter a value for the first number5
enter a value for the second number6
enter a value for the third number7
the swapped number is= 11 13

18. WAP to input a single digit and print a 3 digit number created as n
(n+1) (n+2).
Solution:
n=int(input('enter a numbr in the range 1 to 7:'))
d=n*10+n+1
e=d*10+n+2
print('the 3 digit number is=',e)
output:
enter a numbr in the range 1 to 7:4
the 3 digit number is= 456

[Link] to accept time in seconds and print in hours and minutes.


Solution:
s=int(input('enter a number of seconds:'))
m=s/60
h=s/3600
print('the seconds converted to hours and minutes:',h,s)
output:
enter a number of seconds:8100
the seconds converted to hours and minutes: 2.25 135.0
[Link] to input x,y,z values and calculate 4x^4+3y^3+9z+6pi.
Solution:
x=int(input('enter a value:'))#enter value for x
y=int(input('enter a value:'))#enter value for y
z=int(input('enter a value:'))#enter value for z
pi=3.14
w=4*x**4+3*y**3+9*z+6*pi
print('the calculated output using given variables is=',w)
output:
enter a value:4
enter a value:5
enter a value:6
the calculated output using given variables is= 1471.84

[Link] To input temp for 7 days and display average temperature.


Solution:
a=int(input('enter the temp of monday:'))
b=int(input('enter the temp of tuesday:'))
c=int(input('enter the temp of wednesday:'))
d=int(input('enter the temp of thursday:'))
e=int(input('enter the temp of friday:'))
f=int(input('enter the temp of saturday:'))
g=int(input('enter the temp of sunday:'))
A=(a+b+c+d+e+f+g)/7
print('the average temperature of the week is=',A)
output:
enter the temp of monday:27
enter the temp of tuesday:28
enter the temp of wednesday:29
enter the temp of thursday:30
enter the temp of friday:31
enter the temp of saturday:32
enter the temp of sunday:32
the average temperature of the week is= 29.857142857142858

[Link] that reads the radius andprints the volume of the sphere.
Solution:
r=int(input('enter the value of the radius:'))
pi=3.14
V=(4*pi*r**3)/3
print('the volume of the sphere is=',V)
output
enter the value of the radius:7
the volume of the sphere is= 1436.0266666666666:

23 WAP to accept a 2 digit number and print its reverse.


Solution:
n=int(input('enter the value of a 2 digit number:'))
n1=(n%10)*10
n2=(n//10)
an=n1+n2
print('the swapped number is=',an)
output:
enter the value of a 2 digit number:56
the swapped number is= 65

24 WAP to accept a3 digit number and print its reverse.


Solution:
n=int(input('enter the value of a 3 digit number:'))
n1=(n%10)*100
n2=((n//10)%10)*10
n3=n//100
an=n1+n2+n3
print('the swapped number is=',an)
output
enter the value of a 3 digit number:234
the swapped number is= 432

25 WAP to get third side of right angled triangle from the given two
sides.
Solution:
a=int(input('enter the value for the base of a triangle'))
b=int(input('enter the value for the height of a triangle'))
c=(a**2+b**2)**(1/2)
print('the length of the hypotenuse is=',c)
output
enter the value for the base of a triangle3
enter the value for the height of a triangle4
the length of the hypotenuse is= 5.0
26 WAP to find distance between two points on the Cartesian plane .
Solution:
x1=int(input('enter the first value of abscissa:'))
x2=int(input('enter the second value of abscissa:'))
y1=int(input('enter the first value of ordinate:'))
y2=int(input('enter the second value of ordinate:'))
DF=((x2-x1)**2+(y2-y1)**2)**0.5
print('the distance between the given coordinates is:',DF)
output
enter the first value of abscissa:4
enter the second value of abscissa:5
enter the first value of ordinate:9
enter the second value of ordinate:1
the distance between the given coordinates is: 8.06225774829855

27 WAP to accept number of days and print as years, months and days.
Solution:
# to accept number of days and print as years, months and days.
d=int(input('enter the number of days:'))
Y=d//365
x=d%365
M=x//12
Z=x%12
print(Y,'years',M,'months',Z,'days')
output:
enter the number of days:730
2 years 0 months 0 days

28. WAP to accept total capital and total liabilities then calculate and
display total assets.
Solution:
tc=int(input('enter the total capital'))
tl=int(input('enter the total liabilities'))
ta=tc+tl
print('the total asset is=',ta)
output:
enter the total capital10000
enter the total liabilities2000
the total assets is= 12000
29. WAP to accept short term debt, long term debt, total assets of a
company, calculate and display the value of total assets.
Solution:
sd=int(input('enter the short term debt value;'))
ld=int(input('enter the long term debt value;'))
a=int(input('enter the assests'))
tv=(sd+ld)/a
print('the total assests of the company is=',tv)
output:
enter the short term debt value;12000
enter the long term debt value;100000
enter the assests1400000
the total assests of the company is= 0.08

30. WAP to accept the number of sales of 4 quarters calculate the total
sales and average.
Solution:
a=int(input('enter the sales of the first quarter'))
b=int(input('enter the sales of the second quarter'))
c=int(input('enter the sales of the third quarter'))
d=int(input('enter the sales of the fourth quarter'))
T=a+b+c+d
A=T/4
print('the total sales of the 4 quarters is=',T)
print('the Average sales of the 4 quarters is=',A)
output:
enter the sales of the first quarter30000
enter the sales of the second quarter40000
enter the sales of the third quarter50000
enter the sales of the fourth quarter20000
the total sales of the 4 quarters is= 140000
the Average sales of the 4 quarters is= 35000.0

31. WAP to calculate EMI where the formula is E= PR(1+R)^n/(1+R)^n-1.


Solution:
P=int(input('enter the value for principal amount:'))
R=int(input('enter the value for rate of interest per year:'))
n=int(input('enter the value for tenure of loan repayment'))
EMI=(P*R*(1+R)**n)/((1+R)**n-1)
print('the calculated EMI is =',EMI)
output:
enter the value for principal amount:10000
enter the value for rate of interest per year:5
enter the value for tenure of loan repayment2
thecalulated EMI is = 51428.57142857143

32. WAP a program to input a three digit number and print its sum.
Solution:
a=int(input('enter a three digit number:'))
n=a//100
n2=a%100
n3=n2//10
n4=n2%10
an=n+n3+n4
print('the sum of the number entered is=',an)
output:
enter a three digit number:456
the sum of the number entered is= 15

33. WAP to take two inputs for day and month and calculate which day of
the year thegiven date is, for simplicity take 30 days for each month.
Solution:
#calculate which day of month the given date is.
d=int(input('enter the number of days:'))
m=int(input('enter the number of months:'))
v=(m-1)*30+d
print('the computed day of year is=',v)
output:
enter the number of days:5
enter the number of months:7
the computed day of year is= 185

34. WAP that asks user for a number of years as input and prints number
of days, hours, mins and sec in that years.
Solution:
y=int(input('enter the number of years:'))
d=y*365
h=d*24
m=h*60
s=m*60
print('the years contains ', d,'days',h,'hours',m,'minutes',s,'seconds')
output:
enter the number of years:3
the years contains 1095 days 26280 hours 1576800 minutes 94608000 seconds

35. WAP to calculate volume of cylinder whose radius is 8cm and height
is 15cm.
Solution:
#the volume of cylinder if radius is 8cm and height is 15cm
r=8
h=15
pi=3.14
v=pi*r*r*h
print('the radius of cylinder is=',r,'cm')
print('the height of cylinder is=',h,'cm')
print('the volume of cylinder is=',v,'cm3')
output:
the radius of cylinder is= 8 cm
the height of cylinder is= 15 cm
the volume of cylinder is= 3014.4 cm3

36. WAP to compute (a+b)^3 using the respective formula.


Solution:
a=int(input('enter the value of the first number'))
b=int(input('enter the value of the second number'))
v=(a**3)+(b**3)+(3*(a**2)*b)+(3*a*(b**2))
print('the cube of a+b is=',v)
output:
enter the value of the first number11
enter the value of the second number15
the cube of a+b is= 17576

[Link] to accept a 4 digit number and print its sum.


Solution:
n=int(input('enter a 4 digit number:'))
n1=n//1000
n2=(n%1000)//100
n3=(n//100)%10
n4=n%10
A=n1+n2+n3+n4
print('the sum of the entered digit is=',A)
output:
enter a 4 digit number:5432
the sum of the entered digit is= 14

38. WAP to input monthly sales of an employee and give bonus of 10% if
sale is more than 50000 otherwise bonus is 0.
Solution:
sales=int(input("enter your monthly sales:"))
bonus=0
if sales>50000:
bonus=(sales*10)/100
print('bonus='+str(bonus))
output:
enter your monthly sales:50000
bonus=0
enter your monthly sales:76000
bonus=7600.0

[Link] to enter 2 numbers and print the larger number.


x=int(input("enter a number:"))
y=int(input("enter another number:"))
if x>y:
print('the greater number is=',x)
else:
print('the greater number is=',y)
output:
enter a number:8
enter another number:10
the greater number is= 10

[Link] to enter 3 numbers and print the larger number.


Solution:
n1=int(input("enter first number:"))
n2=int(input("enter second number:"))
n3=int(input("enter third number:"))
large=n1
if (large<n1):
large=n2
if (large<n3):
large=n3
print('the largest number is =',large)
output:
enter first number:100
enter second number:64
enter third number:48
the largest number is = 100
41. WAP to check if entered year is leap year or not.
Solution:
year=int(input('enter any year'))
if year%400==0:
if year%400==0:
print('year is leap year')
else:
print('year is not leap year')
elif year%4==0:
print('year is leap year')
else:
print('year is not leap year')
output:
enter any year2024
year is leap year

42. WAP to read three numbers and print in ascending order.


Solution:
x=int(input('enter first number:'))
y=int(input('enter second number:'))
z=int(input('enter third number:'))
if y>=x<=z:
if y<=z:
Min,Mid,Max=x,y,z
else:
Min,Mid,Max=x,z,y
elif x>=y<=z:
if x<=z:
Min,Mid,Max=y,x,z
else:
Min,Mid,Max=y,z,x
elif x>=z<=y:
if x<=y:
Min,Mid,Max=z,x,y
else:
Min,Mid,Max=z,y,x
print('the numbers in ascending orde is:',Min,Mid,Max)
output:
enter first number:4
enter second number:5
enter third number:2
the numbers in ascending order is: 2 4 5
[Link] to check if the entered number is even or odd.
Solution:
n=int(input('enter any positive number:'))
if n%2==0:
print('the entered number is even')
else:
print('the entered number is odd')
output:
enter any positive number:4
the entered number is even

44. WAP to check if the number is positive or negative.


Solution:
n=int(input('enter a number:'))
if n>0:
print('the entered number is positive')
else:
print('the entered number is negative')
output:
enter a number:-4
the entered number is negative

[Link] a menu driven program to find area of square, rectangle, circle


and triangle.
Solution:
#the area of 2d shapes based on users will
print(' [Link] find area of square \n [Link] find area of rectangle \n [Link] find area of
circle \n 4. To find area of triangle')
n=int(input('enter the number for which shape the area must be found:'))
if n==1:
s=int(input('the side of the square is:'))
A=s*s
print('the area of square is=',A)
if n==2:
l=int(input('the length of the rectangle is:'))
b=int(input('the breadth of the rectangle is:'))
B=l*b
print('the area of rectangle is=',B)
if n==3:
r=int(input('enter a value for radius of a circle:'))
pi=3.14
C=pi*r*r
print('area of a circle is=',C)
if n==4:
p=int(input('enter a value for base of a triangle'))
h=int(input('enter a value for height of a triangle'))
D=p*h/2
print('the area of the triangle is=',D)
output:
[Link] find area of square
[Link] find area of rectangle
[Link] find area of circle
4. To find area of triangle
enter the number for which shape the area must be found:1
the side of the square is:4
the area of square is= 16

46. WAP to print roots ax^2+bx+c=0.


Solution:
#Program to find roots of a quadratic equation
print('A quadratic is of the form ax^2+bx+c')
a=int(input('enter the value of leading coefficient:'))
b=int(input('enter the value of coefficient of x:'))
c=int(input('enter the value of constant:'))
D=(b**2)-4*a*c
if D==0:
print('the roots are real and equal')
x=((-b)+D)/(2*a)
print('the root of the equation is',x)
elif D>0:
print('the roots are real')
x1=((-b)+D)/(2*a)
x2=((-b)-D)/(2*a)
print('the roots of the equation is',x1,x2)
else:
print('the roots are imaginary')
output:
A quadratic is of the form ax^2+bx+c
enter the value of leading coefficient:4
enter the value of coefficient of x:5
enter the value of constant:6
the roots are imaginary

47. WAP to print whether a character is in uppercase, lowercase or any


other character.
Solution:
#to check if a given character is in uppercase or lowercase
x=input('enter any character:')
if x>='A' and x<='Z':
print('the character is in uppercase')
elif x>='a' and x<='z':
print('the character is in lowercase')
elif x>='0' and x<='9':
print('the character is digit')
output:
enter any character:B
the character is in uppercase
48. WAP to find ASCII value of character entered.
Solution:
n=input('enter any character:')
A=ord(n)
print('the ASCII value of',n,'is:',A)
output:
enter any character:s
the ASCII value of s is: 115
49. WAP to determine the type of triangle.
Solution
n1=int(input('enter the value of one side'))
n2=int(input('enter the value of second side'))
n3=int(input('enter the value of third side'))
if n1==n2 and n2==n3 and n3==n1:
print('equialteral triangle')
elif n1!=n2 and n2!=n3 and n3!=n1:
print('scalene triangle')
else:
print('isoceles triangle')
Output:-
enter the value of one side4
enter the value of second side6
enter the value of third side7
scalene triangle
50. WAP to determine the total and average sales of 4 quarters.
Solution:-
a=int(input('enter the sales of the first quarter'))
b=int(input('enter the sales of the second quarter'))
c=int(input('enter the sales of the third quarter'))
d=int(input('enter the sales of the fourth quarter'))
T=a+b+c+d
A=T/4
print('the total sales of the 4 quarters is=',T)
print('the Average sales of the 4 quarters is=',A)
Output:-
enter the sales of the first quarter500
enter the sales of the second quarter450
enter the sales of the third quarter680
enter the sales of the fourth quarter355
the total sales of the 4 quarters is= 1985
the Average sales of the 4 quarters is= 496.25
[Link] to enter string and no of occurance of any word.
Solution:-
#program to enter string and no of occurance of any word
line=input('enter line:')
sub=input('enter substring:')
length=len(line)
lensub=len(sub)
start=count=0
end=length
pos=[Link](sub,start,end)
print('\n Number of occurance of',sub,':',pos)
Output:-
enter line:ronaldo is a good footballer
enter substring:o
Number of occurance of o : 6
52. WAP to count uppercase, lowercase and digits.
Solution:-
#program to count uppercase, lowercase and digits
str1=input('enter any sentence:')
d=0
u=0
l=0
w=0
for ch in str1:
if [Link]():
l+=1
if [Link]():
u+=1
if [Link]():
d+=1
if ch==' ':
w+=1
print("\n Total upper case alphabets:",u)
print("\n Total lower case alphabets:",l)
print("\n Total digits are:",d)
print("\n Total words are:",w+1)
Output:-
enter any sentence:hindi is our official language
Total upper case alphabets: 0
Total lower case alphabets: 26
Total digits are: 0
Total words are: 5
53. WAP to find even numbers.
Solution:-
#program to find even numbers
n=int(input('enter any number until which even numbers must be printed'))
i=0
while i<(n-1):
i=i+2
print(i)
Output:-
enter any number until which even numbers must be printed5
2
4
54. WAP for for loop trial.
Solution:-
x=0
l=['archit','narain',"fawaz"]
for i in l:
x=x+1
print(i)
print(x)
Output:-
archit
1
narain
2
fawaz
3
55. WAP which is manudriven and includes all the operators.
Solution:-
a=int(input('Enter 1st no.'))
b=int(input('Enter 2nd no.'))
c=int(input('Enter 3rd no.'))
print(a+b+c)
print(a-b-c)
print(a*b*c)
print(a/b/c)
print(a//b//c)
print(a%b%c)
print(a==b)
print(b==c)
Output:-
Enter 1st no.34
Enter 2nd no.11
Enter 3rd no.10
55
13
3740
0.3090909090909091
0
1
False
l=int(input('enter an hours ahead:'))

False
56. WAP to generate the 3 digit number by accepting digit from usewr
between range of 1 to 7.
Solution:-
n=int(input('enter a numbr in the range 1 to 7:'))
d=n*10+n+1
e=d*10+n+2
print('the 3 digit number is=',e)
Output:-
enter a numbr in the range 1 to 7:7
the 3 digit number is= 789
[Link] to find the area of sphere by accepting the radius from user.
Solution:-
r=int(input('enter the value of the radius'))
pi=3.14
A=4*pi*r**2
print('the area of the sphere is=',A)
Output:-
enter the value of the radius23
the area of the sphere is= 6644.240000000001
[Link] to determine how many hours ahead.
Solution:-
n=int(input('enter an hour between 1 to 12:'))
m=n+l
if m<=12:
print('the time is:',m,'o clock')
elif m>12 and m<=24:
if m==13:
print('the time is 1 o clock')
elif m==14:
print('the time is 2 o clock')
elif m==15:
print('the time is 3 o clock')
elif m==16:
print('the time is 4 o clock')
elif m==17:
print('the time is 5 o clock')
elif m==18:
print('the time is 6 o clock')
elif m==19:
print('the time is 7 o clock')
elif m==20:
print('the time is 8 o clock')
elif m==21:
print('the time is 9 o clock')
elif m==22:
print('the time is 10 o clock')
elif m==23:
print('the time is 11 o clock')
elif m==24:
print('the time is 12 o clock')
Output:
enter an hour between 1 to 12:10
enter an hours ahead:5
the time is 3 o clock
[Link] to calculate BMI.
Solution:-
W=float(input('enter your weight='))
H=float(input('enter your height='))
n=H*H
BMI=W/n
print('your Body Mass Index is=',BMI)
Output:
enter your weight=55
enter your height=180
your Body Mass Index is= 0.0016975308641975309
[Link] to calculate the BMI using if .
Solution:-
w=float(input('enter your weight in kilograms:'))
h=float(input('enter your height in metres:'))
BMI=w/(h*h)
if BMI<18.9:
print('the person is underweight')
elif BMI>19 and BMI<24.9:
print('the person is normal')
elif BMI>25 and BMI<29.9:
print('the person is overweight')
else:
print('the person is obese')
Output:-
enter your weight in kilograms:55
enter your height in metres:180
the person is underweight
[Link] to input three values and print sum of three.
Solution:-
a=int(input('enter a value='))
b=int(input('enter a value='))
c=int(input('enter a value='))
S=a+b+c
print('sum of the numbers is=',S)
Output:-
enter a value=3
enter a value=4
enter a value=2
sum of the numbers is= 9
[Link] to calculate the cost of each item.
Solution:-
n=int(input('enter the number of items bought:'))
if n<=10:
print('the cost of each item is rupees 120 each')
elif n>10 and n<=99:
print('the cost of each item is rupees 100 each')
else:
print('the cost of each item is rupees 70 each')
Output:-
enter the number of items bought:5
the cost of each item is rupees 120 each
63. WAP to find if the two numbers taken from user are close or not.
Solution:-
a=float(input('enter any number'))
b=float(input('enter another number'))
if a>b:
if abs(a-b)<=0.0010000000000012221:
print('the given two numbers are close')
else:
print('the given two numbers are not close')
elif b>a:
if abs(b-a)<=0.0010000000000012221:
print('the given two numbers are close')
else:
print('the given two numbers are not close')
Output:-
enter any number234235
enter another number234454
the given two numbers are not close
64. WAP to calculate the sum of natural numbers till the user wants.
Solution:-
n=int(input('enter a number:'))
sum1=0
for x in range(1,n+1):
sum1 =sum1+x
print('total is:',sum1)
Output:-
enter a number:234
total is: 27495
65. WAP to accept any number and print its factorial.
Solution:-
#to accept any number and print its factorial
sub1=1
n=int(input('enter any number:'))
for i in range(n,1,-1):
sub1=sub1*i
print(sub1)
Output:-
enter any number:5
120
[Link] to make of the numbers 1,12,123,1234.
Solution:-
for i in range(1,5):
for j in range(1,i+1):
print(j, end= " ")
print()
Output:-
1
12
123
1234
[Link] to make pattern of 54321,4321,321,21,1.
Solution:-
for i in range(5,0,-1):
for j in range(i,0,-1):
print(j, end= " ")
print()
Output:-
54321
4321
321
21
1
[Link] to make pattern of 1,11,111.
Solution:-
for i in range(1,5):
print('1'*i)
Output:-
1
11
111
1111
69. WAP to make a pattern of 5,54,543,5432,54321.
Solution:-
for i in range(5,0,-1):
for j in range(5,i-1,-1):
print(j,end=" ")
print()
Output:-
5
54
543
5432
54321
[Link] to make a pattern of 54321,4321,321,21,1.
Solution:-
for i in range(5,0,-1):
for j in range(i,0,-1):
print(j,end=" ")
print()
Output:-
54321
4321
321
21
1
[Link] find the largest of three numbers.
Solution:-
#Write a program to find the largest of three numbers
a=int(input("Enter the first number: "))
b=int(input("Enter the second number: "))
c=int(input("Enter the third number: "))
if a>b>c:
print(a,"is the greatest number")
elif b>c>a:
print(b," is the greatest number")
elif c>b>a:
print(c,"is the greatest number")
Output:-
Enter the first number: 53
Enter the second number: 654
Enter the third number: 865
865 is the greatest number
[Link] to display the sum of all odd numbers within the given range.
Solution:-
#display the sum of all odd numbers within the given range if the given range is
10
#set up a variable to store the value initial value is 0
n=int(input("enter a number"))
sum1=0
for i in range(n):
#if i is odd, add it
#to the sum variable
if i%2!=0:
sum1+=i
print(sum1)
#print the total sum at the end
Output:-
enter a number4556
5189284
[Link] to display the first 10 odd natural numbers.
Solution:- #display the first 10 odd natural numbers
n=int(input("enter a number:"))
i=0
for i in range(1,(n*2),2):
print(i)
Output:-
enter a number:10
1
3
5
7
9
11
13
15
17
19
[Link] to display all even natural numbers within the range.
Solution:-
# display all even natural numbers within the range if the range is 10
n=int(input("enter a number: "))
for i in range(n+1):
if i%2==0:
print(i)
Output:-
enter a number: 10
0
2
4
6
8
10
75. WAP to display first ten natural numbers using for loop.
Solution:-
#display first ten natural numbers
n=int(input("Enter a number: "))
for i in range(1,n):
print(i)
Output:-
Enter a number: 10
1
2
3
4
5
6
7
8
9
76. WAP to make a pattern using star by using for loop.
Solution:-
n=int(input("enter number of rows:"))
for i in range(n):
print('* '*n)
Output:-
enter number of rows:5
*****
*****
*****
*****
*****
77. WAP to make pattern of alphabets using for loop.
Solution:-
n=int(input("enter number of rows: "))
for i in range(n):
print((chr(65+i)+' ')*(i+1))
Output:-
enter number of rows: 5
A
BB
CCC
DDDD
EEEEE
78. WAP to make a pattern of numbers using for loop.
Solution:-
n=int(input("enter number of rows: "))
for i in range(n):
print((str(i+1)+' ')*n)
Output:-
enter number of rows: 4
1111
2222
3333
4444
79. WAP to make Pattern of A using for loop.
Solution:-
n=int(input("enter number of rows: "))
for i in range(n):
print('A '*n)
Output:-
enter number of rows: 6
AAAAAA
AAAAAA
AAAAAA
AAAAAA
AAAAAA
AAAAAA
80. WAP to make a triangle pattern by stars using for loop.
Solution:-
n=int(input("enter number of rows: "))
for i in range(n):
print('* '*(n-i))
Output:-
enter number of rows: 3
***
**
*
81. WAP to to calculate the average of three numbers.
Solution:-
#Write a program to calculate the average of three numbers
n=int(input("enter first number:"))
m=int(input("enter second number:"))
o=int(input("enter third number:"))
average=n+m+o/3
print("the average of the three number is:",average)
Output:-
enter first number:45
enter second number:56
enter third number:67
the average of the three number is: 123.33333333333333
[Link] to to take input name from the user and display it.
Solution:-
#Write a program to take input name from the user and display it
n=input("enter name")
print("the name is",n)
Output:-
enter nameArchit BHatnagar
the name is Archit BHatnagar
83. WAP to display date and remaining das in a month.
Solution:-
dd=int(input('enter dd'))
mm=int(input('enter mm'))
yy=int(input('enter yy'))
print('todays date is',dd,'-',mm,'-',yy)
b=30-dd
print('days left in month=',b)
c=dd*4/30
print('the current week running is',c)
Output:-
enter dd14
enter mm9
enter yy2024
todays date is 14 - 9 - 2024
days left in month= 16
the current week running is 1.8666666666666667
[Link] to make a triangle of hashtags using nested for.
Solution:-
for i in range(6):
for j in range(6-i):
print(' ',end=' ')
for j in range(i):
print('#', end=' ')
for j in range(i):
print('#',end=' ')
print('#')
for i in range(6,-1,-1):
for j in range(6-i,0,-1):
print(' ',end=' ')
for j in range(i):
print('#', end=' ')
for j in range(i):
print('#',end=' ')
print('#')
Output:-
#
###
#####
#######
#########
###########
#############
###########
#########
#######
#####
###
#
85. WAP to reverse 3 digit number .
Solution:-
a=int(input('enter original number'))
reversed_a=0
while a!=0:

x=a%10
reversed_a = reversed_a*10+x
a//=10

print('reversed number=',int(reversed_a))
Output:-
enter original number4567
reversed number= 7654
86. WAP to calculate the square of numbers till user wants using nested
loop.
Solution:-
n=int(input('show square till'))
for i in range(2,n+1):
for j in range(i,i+1):
print(i*j)
Output:-
show square till24
4
9
16
25
36
49
64
81
100
121
144
169
196
225
256
289
324
361
400
441
484
529
576
[Link] to make a multiplication table unitil user wants.
Solution:-
n=int(input(‘ enter the number till you want table’))
for i in range(2, n+1):
for j in range(1, 11):
print(i, "*", j, "=", i*j)
print()
Output:-
enter the number till you want table2
2*1=2
2*2=4
2*3=6
2*4=8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18
2 * 10 = 20
88. WAP to check if any word is a part of sentence.
Solution:-
Line = input ()
Word = input ()
If word in line :
Print (‘Yes’,word,is in line’)
Else :
Print(‘No’, word , ‘not in line’)
Output:-
You are ronaldo
ronaldo
Yes ronaldo is in line
89. WAP to replicate list twice and then print in ascending and
descending order:.
Solution:-
l1=eval(input('enter list'))
print(l1)
print(l1)
b=sorted(l1)
print('list in ascending order=',b)
l=len(b)
print('list in descending order',b[ : :-1])
Output:-
enter list[23,45,6,7,34,67,12]
[23, 45, 6, 7, 34, 67, 12]
[23, 45, 6, 7, 34, 67, 12]
list in ascending order= [6, 7, 12, 23, 34, 45, 67]
list in descending order [67, 45, 34, 23, 12, 7, 6]
90. WAP to input a list and swap elements at even and odd location.
Solution:-
l=eval(input('enter list'))
a=l[0: :2]
b=l[1: :2]
l1=[]
ind=1
for i in b:
[Link](i)
for j in a:
[Link](ind,j)
ind=ind+2
print(l1)
Output:-
enter list[1,2,3,4,5,6]
[2, 1, 4, 3, 6, 5]
91. WAP to find freq. of every element and create new list of unique list
and duplicate list.
Solution:-
l=eval(input('enter list'))
d=[]
b=[ ]
c=[ ]
for i in l:
if i not in d:
[Link](i)
for j in d:
e=[Link](j)
print('[Link]',j,'is',e)
if e>=2:
if i not in b:
[Link](i)
else:
[Link](i)
print('list of Unique elements is',c)
print('list of duplicate elements is',b)
Output:-
enter list[1,2,3,3,4,5,5,6,7,7,8,9]
[Link] 1 is 1
[Link] 2 is 1
[Link] 3 is 2
[Link] 4 is 1
[Link] 5 is 2
[Link] 6 is 1
[Link] 7 is 2
[Link] 8 is 1
[Link] 9 is 1
list of Unique elements is [1, 2, 4, 6, 8, 9]
list of duplicate elements is [3, 5, 7]
92. WAP to calculate the mean of given list of no.
Solution:-
l1=eval(input('enter list:'))
l=len(l1)
s=0
b=sum(l1)
m=b/l
print('mean of the list is',m)
Output:-
enter list:[1,2,4,656,77,89]
mean of the list is 138.16666666666666
93. WAP to print separated and overlapped.
Solution:-
l=eval(input('enter list one'))
l2=eval(input('enter list two'))
a=0
b=0
for i in l:
if i in l2:
b=a+1
else:
a=0
if b>0:
print('Overlapped')
else:
print('Separated')
Output:-
enter list one[1,2,3,4,5,6]
enter list two[1,3,8,9,7,0]
Overlapped
94. WAPto check if max no. is in 1st half or 2nd half.
Solution:-
l1=eval(input('enter list'))
l=len(l1)
b=max(l1)
if b in range (0,(l//2)+1):
print('max no. is',b,'max no. in 1st half')
else:
print('max no. is',b,'max no. in 2nd half')
Output:-
enter list[1,2,3,4,5,6,7,8]
max no. in 2nd half
95. WAP to check if given element the list.
Solution:-
l1=eval(input('enter list'))
b= int(input('enter no.'))
if b in l1:
print('yes given number is present in the list')
else:
print('no given number is not present in the list')
Output:-
enter list[1,2,3,4,5,6]
enter no.2
yes given number is present in the list
96. WAP to add new roll no with marks as values to dict m.
Solution:-
m={1:45,2:56,3:78,4:55}
x=eval(input('enter roll no.'))
y=eval(input('enter marks'))
d=[Link](x,y)
print(m)
Output:-
enter roll no.6
enter marks45
{1: 45, 2: 56, 3: 78, 4: 55, 6: 45}
[Link] to make a dictionary from the list of keys and values .
Solution:-
keys=['one','two','three']
values=[1,2,3]
d=dict(zip(keys,values))
print(d)
Output:-
{'one': 1, 'two': 2, 'three': 3}
[Link] to input number of rows and make pattern of number.
Solution:-
n=int(input("enter no of rows"))
for i in range(n):
print(str(65+i)*(i+1))
Output:-
enter no of rows8
65
6666
676767
68686868
6969696969
707070707070
71717171717171
7272727272727272
[Link] to print 2 slice of the list.
Solution:-
l1=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
l=len(l1)
a=l1[0:l//2]
b=l1[l//2: ]
print('first slice of list',a)
print('second slice of list',b)
Output:-
first slice of list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
second slice of list [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[Link] to check if the number is Armstrong number or not.
Solution:-
num=int(input('num:'))
b=str(num)
l=len(b)
a=0
for i in range(0,l):
a+=int(b[i])**l
if a==num:
print('yes')
else:
print('no')
Output:-
num:1634
yes,the given number is an Armstrong number
[Link] to check if the given number is palindrome or not.
Solution:-
num=int(input('num'))
o=num
rv=0
while num!=0:
x=num%10
rv=rv*10+x
num=num//10
print(rv)
if rv == o:
print('yes')
else:
print('no')
Output:-
num121
121
Yes,the given number is a palindrome number.

102. WAP that reads two numbers and an arithmetic the operator and
displays Computed result.
Solution:-
Num1=float(input(‘enter 1st number’))
num 2= float (input("Enter 2nd number."))
op = input ("Enter operator [+-*/0/%]+")
result = 0
if op== ' + ':
result = num1 + num 2
if OP == '_ '
result=num 1 - num 2
if OP==`* '
result = numi*num 2
if OP== "/":
result = num1/num 2
if op=='%':
result = num1 % num 2
else:
Print ("Invalid operator!!")
Print (num1, OP,num2,'=',result)

Output:-
Enter 1st number: 2
Enter 2nd number:0
Enter Operator [+-*/0/0): *
2*0=0

[Link] to find the minimum elements from a list of elements along


with its index in the list.

Solution:-
L=eval(input(‘enter list:’))
length= len(l)
Min_ele=lst[0]
Min_index=0
For i in range(1,length):
If l[i]<Min_ele:
Min_ele = l[i]
Min_index=i
print(‘Given list is:’,l)
print(‘the minimum element is:’)
print(Min_ele,’at index’,Min_index)

Output:-
Enter list: [2,3,4,-2,-6,7,8,11,-9,11]
Given list is: [2,3,4,-2,6,7,8,11,-9,11]
The minimum element of a gi given list is:
-9 at index 8.

[Link] to input a list of numbers and test if the number equal to the
sum of the cubes of its digit. Find the smallest and largest no. of the
given list.
Solution:-
Val=eval(input(‘enter list’))
Lst=[ ]
S=len(val)
For i in range(s):
num=val[i]
Csum=0
while num:
d=num%10
Csum+=(d*d*d)
num+=num//10
if csum==val[i]:
[Link](val[i])
print(‘largest no.(=sum of its digit cubes):’, max(lst))
print(‘smallest no.(=sum of its digit cubes):’,min(lst))

Output:-
Enter list[67,153,311,96,370,4050,371,407]
Largest no.(=sum of its digit cubes): 407
Smallest no.(=sum of its digit cubes):153

[Link] to input list and 2 number M and N. then create a list from
those 1st elements which are both divisible by M and N.
Solution:-
lst=eval(input(‘enter lst’))
lst2=[ ]
M,N=eval(input(‘enter 2 no as M and N:’))
for num in lst:
if (num%M==0 and num%N==0):
[Link](num)
print(‘new created list is:’,lst2)

Output:
Enter list:[6,8,11,6,12,7,16]
Enter 2 no. as M and N:6,2
New created list is:[6,6,12]

[Link] to read a list of elements. Modify this list so that it does not
contain any duplicate elements.
Solution:-
Val=eval(input(‘enter list’))
tmp=[ ]
print(‘original list:’,val)
for a in val:
if a not in tmp:
[Link](a)
val=lst(tmp)
print(‘modified list:’,val)

Output:-
Enter list[6,3,4,2,6,7,0,7,12,5]
Original list:[6,3,4,2,6,7,0,7,12,5]
Modified list:[6,3,4,2,7,0,12,5]

107. WAP to check if the elements in the 1st half of the tuple are sorted
in ascending order or not.
Solution:-
tup=eval(input(‘enter tuple’))
ln=len(tup)
mid=ln//2
if ln%2==1:
mid=mid+1
half=tup[:mid]
if sorted(half)==list(tup[:mid]):
print(‘1st half is sorted’)
else:
print(‘1st half is not sorted’)

Output:-
Enter tuple(11,12,13,14,10)
1st half is sorted
Enter tuple(11,22,13,14,10)
1st half is not sorted

108.A tuple stores marks of a student in 5 subjects. WAP to calculate


the grade of the student as per the following:
Average grade
>=75 ‘A’
60-74.999 ‘B’
50-59.999 ‘C’
<50 ‘D’
Solution:-
mks=eval(input(‘Enter tuple’))
total=sum(mks)
avg=total/5
if avg >= 75:
grade=’A’
elif avg >=60:
grade=’B’
elif avg >=50:
grade=’C’
else:
grade=’D’
print(‘total marks:’,total,’grade:’,grade)
Output:-
Enter tuple 78.5,67.8,89.9,70.5,50
total marks: 357.70 grade: B

109. WAP to read a sentence and then create a dictionary contains the
frequency of letters and digits in the sentence.
Solution:-
sen=input(‘Enter sentence:’)
sen=[Link]()
alphabet_digit=’abcdefghijklmnopqrstuvwxyz0123456789’
char_count={ }
print(‘total characters are:’, len(sen))
for char in sen:
if char in char_count:
char_count[char]=char_count[char]+1
else:
char_count[char]=1
print(char_count)

Output:
Enter Sentence: hello there! Class 10 is done, now you are in class 11
Total characters are: 58
{‘h’:2,’e’:5,’l’:4,’o’:4,’t’:1,’r’:2,’c’:2,’a’:3,’s’:5,’1’:3,’0’:1,’i’:2,’d’:1,’n’:3,’w’:1,’y’:1,’
u’:1}
[Link] to create a third dictionary from two dictionary having some
common keys, in way so that the values of common keys are added in
the 3rd dictionary.
Solution:-
dict1={‘1’:100,’2’:200,’3’:300}
dict2={‘1’:300,’2’:200,’5’:400}
dict3=dict(dict1)
[Link](dict2)
for i,j in [Link]():
for x,y in [Link]():
if i==x:
dict3[i]=(j+y)
print(‘2 given dictionary are:’)
print(dict1)
print(dict2)
print(‘the resultant dictionary’)
print(dict3)

Output:-
Two given dictionary are:
{‘1’:100,’2’:200,’3’:300}
{‘1’:300,’2’:300,’5’:400}
The resultant dictionary
{‘1’:400,’2’:400,’3’:300,’5’:400}

== == == ==

You might also like