Logo
Unit 14 – Python Standard Library: Math and Random

Python Standard Library: Math and Random

Duration: 5 minutes

Prepare to be blown away by the power of Python’s Standard Library! Today, we’ll look at two fascinating modules:‘math’ and ‘random’. The **‘math’ **module functions similarly to a digital calculator. It offers trigonometric functions, logarithmic functions, the value of pi, and much more! Do you need to find the square root of a number? ‘math. sqrt()’ has your back! The **‘random’ **module, on the other hand, is all about producing randomness. **‘random’ **adds an element of unpredictability to your programs, from selecting a random number within a range to selecting a random element from a list. Here’s how you can put them to use:

import math
print(math. sqrt(16))    # Outputs: 4. 0
import random
print(random. randint(110))    # Outputs a random integer between 1 and 10

Exercise

Now it’s your turn to play with randomness and mathematics! Here’s your challenge:- Generate two random integers, **a** and **b**, between **1 **and 15.

  • Calculate and print the square root of both numbers.
  • Calculate and print the sum of the square roots.
  • Round the sum to the nearest whole number using **math. round()**

Conclusion

Bravo! You’ve just found how Python’s’math’ and ‘random’ modules can improve the performance of your projects. Python has you covered whether it comes to performing sophisticated math calculations or creating random results. Continue to practice, explore, and code!

Next Tutorial: Python Basics: Strings and String Methods

5 minutes Minutes

Continue

Code on the Go with our Mobile App!

Unleash your coding potential anytime, anywhere!

Download Now!