Python Programs
Python Programs
[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 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
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
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] 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:
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
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
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
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
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
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}
== == == ==