Python : Math - Exercises and Solution


1. Write a Python program to convert degree to radian

Sample Output

Degree = 90

Degree to Radian = 1.5707963267948966

View Solution

2. Write a Python program to convert radian to degree.

Sample Output

Radian = 2

Radian to Degree = 114.59155902616465

View Solution

3. Write a Python program to calculate the area of a trapezoid.

Sample Output

Area = ( ( base1 + base2 ) / 2 ) * height

Height of Trapezoid = 2

Base 1 = 3

Base 2 = 2

Area = 5.0

View Solution

4. Write a Python program to calculate the area of a parallelogram

Sample Output

Area = base * height

Base = 5

Height = 3.4

Area of the parallelogram = 17.0

View Solution

5. Write a Python program to calculate surface volume and area of a cylinder

Sample Output

Radius of the Cylinder = 2.4

Height of the Cylinder = 3

Volume of the Cylinder = 54.28672105403163 cubic units

Lateral surface area of the Cylinder = 45.23893421169302 square units

Total surface area of the Cylinder = 81.43008158104743 square units

View Solution

6. Write a Python program to calculate surface volume and area of a sphere

Sample Output

Radius of Sphere = 2.3

Surface Area of the Sphere = 66.47610054996001

Volume of the Sphere = 50.965010421636

View Solution

7. Write a Python program to calculate arc length of an angle.

Sample Output

Angle in Degrees = 35

Radius = 25

Arc Length = 15.271630954950384

View Solution

8. Write a Python program to calculate the discriminant value.

Sample Output

Discriminant = b2−4ac
If D > 0, it indicates that there are two real solutions.
If D = 0, there is one real solution (a repeated root).
If D < 0, there are no real solutions (complex roots).

Enter the A Value = 1

Enter the B Value = -3

Enter the C Value = 2

Two Solutions. Discriminant value is = 1.0

View Solution

9. Write a Python program to find the smallest multiple of the first n numbers. Also, display the factors.

Sample Output

Enter a positive integer (n) = 6

The smallest multiple of the first 6 numbers is = 60

Factors of the smallest multiple = [2, 3, 5]

View Solution

10. Write a Python program to calculate the difference between the squared sum of first n natural numbers and the sum of squared first n natural numbers.(default value of number=2).

Sample Output

N = 23

The for loop iterates through each natural number from 1 to 23
Calculate the sum of the squares of each number(num * num)
Calculate the sum of the numbers and square it (square_of_sum += num)
Square the final sum obtained in square of sum(square_of_sum ** 2)
Calculate the difference between the square of sum and the sum of squares (square_of_sum - sum_of_squares)

Calculated difference = 71852

View Solution

11. Write a Python program to calculate the sum of all digits of the base to the specified power

Sample Output

Base Number = 5

Power Number = 12

Sum of digits = 28

View Solution

12. Write a Python program to print sum of all divisors of a number

Sample Output

Enter the Number = 12

Sum of All Divisors = 16

View Solution

13. Write a Python program to print all permutations of a given string (including duplicates)

Sample Output

Enter the Values = 234

[ '234' , '324' , '342' , '243' , '423' , '432' ]

View Solution

14. Write a Python program to multiply two integers without using the * operator in python

Sample Output

Number 1 = 12

Number 2 = 34

Multiplication = 408

View Solution

15. Write a python program to find the next smallest palindrome of a specified number

Sample Output

Enter the Number = 23454

The Next smallest palindrome after 23454 is = 23532

View Solution

16. Write a python program to find the next previous palindrome of a specified number

Sample Output

Enter the Number = 2374

Previous Palindrome = 2332

View Solution

17. Write a Python program to convert a float to ratio

Sample Output

Enter a float number = 9.2

The ratio of 9.2 is = 46/5

View Solution

18. Write a Python program to print number with commas as thousands separators (from right side)

Sample Output

Number = 2550000

Formatted Number = 2,550,000

View Solution

19. Write a Python program to calculate distance between two points using latitude and longitude

Sample Output

Starting latitude = 37.7749

Starting longitude = -122.4194

Ending latitude = 34.0522

Ending longitude = -118.2437

Distance = 559.12 km

View Solution

20. Write a python program to calculate the area of a regular polygon

Sample Output

Number of Sides = 3

Length of a Side = 45

Area of the Polygon = 876.8507

View Solution

21. Write a Python program to calculate wind chill index

Sample Output

wind Speed in kilometers/hour = 21.3

Temperature in Degrees Celsius = 34.5

The wind chill index is 38.0

View Solution

22. Write a Python program to find the roots of a quadratic function

Sample Output

Quadratic function = (a * x^2) + b*x + c

A Number = 2

B Number = 5

C Number = 3

There are Two Roots = -1.0 and -1.5

View Solution

23. Write a Python program to convert a binary number to decimal number

Sample Output

Binary Number = 100100

Decimal Number = 36

View Solution

24. Write a Python program to print a complex number and its real and imaginary parts

Sample Output

X Number = 12

Y Number = 16

Complex Number = (12+16j)

Real Part = 12.0

Imaginary Part = 16.0

View Solution

25. Write a Python program to add, subtract, multiply and division of two complex numbers

Sample Output

complex1 = (2+5j)

complex2 = (5-3j)

Addition of Two Complex Numbers = (7+2j)

View Solution

26. Write a Python program to get the length and the angle of a complex number

Sample Output

Real = 3

Imaginary = 4

Length of a Complex Number = 5.0

Complex Number Angle = 1.5707963267948966

View Solution

27. Write a Python program to convert Polar coordinates to rectangular coordinates

Sample Output

Polar Coordinates = (5.0, 0.9272952180016122)

Convert Polar to rectangular Coordinates = (-2+2.4492935982947064e-16j)

View Solution

28. Write a Python program to find the maximum and minimum numbers from the specified decimal numbers

Sample Output

Multiple Decimal Points with Comma Separate = 23.45 , 67.23 , 1.45 , 89.67 , 0.34

Maximum = 89.67

Minimum = 0.34

View Solution

29. Write a Python program to get the square root and exponential of a given decimal number

Sample Output

Decimal number = 64.0

Square root of 64.0 is = 8.0

Eresponential of 64.0 is = 6235149080811616882909238709

View Solution

30. Write a Python program to display the fraction instances of the string representation of a number

Sample Output

["2.3", "7.5", "6.32", "5.1"]

Convert each Decimal to a Fraction

2.3 = 23/10

7.5 = 15/2

6.32 = 158/25

5.1 = 51/10

View Solution

31. Write a Python program to generate random float numbers in a specific numerical range

Sample Output

Generate and print 10 random float numbers. You'll get different sets of random
float numbers each time, with each number being within its specified range.

View Solution

32. Write a Python program to generate random integers in a specific numerical range.

Sample Output

Generate and print 10 random integer numbers. You'll get different sets of random
integer numbers each time, with each number being within its specified range.

View Solution

33. Write a Python program to generate random even integers in a specific numerical range.

Sample Output

Generate and print 10 random even integer numbers. You'll get different sets of random
even integer numbers each time, with each number being within its specified range.

View Solution

34. Write a Python program to get a single random element from a specified string

Sample Output

String = Python Exercise

Random Element = o

In this example, the random element selected from the string is "o". However,
since it's a random process, the output may vary each time you run the program,
and you might get a different character as the random element.

View Solution

35. Write a Python program to shuffle the following elements randomly

Sample Output

Numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

Shuffle Numbers = [30, 50, 70, 60, 10, 100, 40, 90, 80, 20]

Each time the program runs, the elements of the list numbers are shuffled
randomly and printed to the console.

View Solution

36 .Write a Python program to flip a coin 1000 times and count heads and tails

Sample Output

Flipping a coin 100 times and counts the number of heads and tail.
Randomly select either "heads" or "tails" for each flip.

Heads = 62

Tails = 38

"Heads" appeared 62 times, and "Tails" appeared 38 times. Since it's based on
random selection, the actual results may vary each time you run the code

View Solution

37. Write a Python program to randomly select an item from a list

Sample Output

book = [ "C" , "C++" , "JAVA" , "PYTHON" , "MYSQL" , "PHP" ]

Randomly select one of the items from the book list, and the
output will display the selected book

View Solution

38.Write a Python program to calculate the absolute value of a floating point number

Sample Output

Values = -45.56

Absolute Values = 45.56

Values = 0.1

Absolute Values = 0.1

View Solution

39. Write a Python function to round up a number to specified digits

Sample Output

Number = 88.24573
Round up to the nearest whole number = 89
Round up to one decimal place = 88.3
Round up to two decimal places = 88.25
Round up to three decimal places = 88.246
Round up to four decimal places = 88.2458

View Solution

40. Write a Python program to find perfect squares between two given numbers

Sample Output

Start Number = 1

End Number = 49

Perfect Squares = [1, 4, 9, 16, 25, 36, 49]

View Solution

41. Write a Python program to that takes an integer and rearrange the digits to create two maximum and minimum numbers

Sample Output

Numbers = 2014

Minimum Numbers = 0124

Maximum Numbers = 4210

View Solution

42. Write a Python program to check the absolute difference between two consecutive digits is two or not. Return true otherwise false

Sample Output

Enter a number: 2468

The absolute difference between consecutive digits is two

View Solution

43. Write a Python program to check whether a given number is a Disarium number or unhappy number

Sample Output

Enter the Number = 56

56 is Not Disarium Number

View Solution

44. Write a Python program to cap a number within the inclusive range specified by the given boundary values x and y

  1. If num falls within the range (a, b), return num
  2. Otherwise, return the nearest number in the range
View Solution

45. Write a Python program to calculate the Hamming distance between two given values

  1. Use the XOR operator (^) to find the bit difference between the two numbers
  2. Use bin() to convert the result to a binary string
  3. Convert the string to a list and use count() of str class to count and return the number of 1s in it
View Solution

46. Write a Python program to get the sum of the powers of all the numbers from start to end (both inclusive)

  1. Use range() in combination with a list comprehension to create a list of elements in the desired range raised to the given power
  2. Use sum() to add the values together
  3. Omit the second argument, power, to use a default power of 2
  4. Omit the third argument, start, to use a default starting value of 1
View Solution

47. Write a Python program to get the nth tetrahedral number from a given integer(n) value

Sample Output

T(n) = (n * (n + 1) * (n + 2)) / 6

Enter the Number = 23

Tetrahedral Number = 2300.0

View Solution

48. Write a Python program to calculate the aliquot sum of an given integer

Sample Output

Integer = 12

Aliquot sum = 16

View Solution

49. Write a Python program to convert a given float value to ratio

Sample Output

Float value: 23.56

Fraction Representation = 589/25

View Solution

50. Write a Python program to generate a series of unique random numbers

Sample Output

Create a list of numbers from 0 to 49 and shuffle it.

Random Number = 46

Do You Want Another Random Number? (Y/N) = Y

Random Number = 4

Do You Want Another Random Number? (Y/N) = N

Generates the random number 46. Then, it asks if you want another random number.
You choose 'Y' (Yes), so it generates and displays 4. Finally, it asks if you want
another random number again. This time, you choose 'N' (No), so the program stops

View Solution

List of Programs


Sample Programs


Python Database Connection


Python Flask


Python Tkinder Tutorial