Python > Modules and Packages > Standard Library > Mathematical Operations (`math`, `cmath`, `random` modules)
Basic Mathematical Operations with `math` and `random`
This snippet demonstrates fundamental mathematical operations using Python's `math` and `random` modules. It covers calculating square roots, exponents, trigonometric functions, and generating random numbers.
Importing Modules
First, we import the necessary modules. The `math` module provides access to mathematical functions, while the `random` module is used for generating random numbers.
import math
import random
Calculating Square Root and Exponent
The `math.sqrt()` function calculates the square root of a number. The `math.pow()` function raises a number to a given power. In this case, we're calculating the square root of 16 and 16 squared.
x = 16
sqrt_x = math.sqrt(x)
pow_x = math.pow(x, 2) # x squared
print(f'The square root of {x} is: {sqrt_x}')
print(f'{x} raised to the power of 2 is: {pow_x}')
Trigonometric Functions
The `math` module also provides trigonometric functions like `sin()` and `cos()`. Note that these functions expect input in radians, so we use `math.radians()` to convert degrees to radians before passing them to `math.sin()` and `math.cos()`.
angle_degrees = 30
angle_radians = math.radians(angle_degrees)
sin_angle = math.sin(angle_radians)
cos_angle = math.cos(angle_radians)
print(f'The sine of {angle_degrees} degrees is: {sin_angle}')
print(f'The cosine of {angle_degrees} degrees is: {cos_angle}')
Generating Random Numbers
The `random.random()` function generates a random floating-point number between 0.0 (inclusive) and 1.0 (exclusive). The `random.randint()` function generates a random integer between the specified start and end values (inclusive).
random_float = random.random() # Generates a random float between 0.0 and 1.0
random_int = random.randint(1, 10) # Generates a random integer between 1 and 10 (inclusive)
print(f'Random float between 0 and 1: {random_float}')
print(f'Random integer between 1 and 10: {random_int}')
Real-Life Use Case: Simulating a Dice Roll
This demonstrates how `random.randint()` can be used to simulate real-world events, such as rolling a dice.
def roll_dice():
return random.randint(1, 6)
result = roll_dice()
print(f'You rolled a: {result}')
Best Practices
Interview Tip
Be prepared to explain the difference between `math.random()` and `random.randint()`, as well as their use cases. Understanding how to generate random numbers within specific ranges is a common interview question.
When to Use Them
Use the `math` module when you need accurate mathematical calculations, especially those involving square roots, exponents, trigonometry, or logarithms. Use the `random` module when you need to generate random numbers for simulations, games, or any other application where randomness is required.
Alternatives
For more advanced numerical computations, consider using the `numpy` library, which provides optimized functions for array operations and linear algebra.
Pros
Cons
FAQ
-
What is the difference between `random.random()` and `random.randint()`?
`random.random()` generates a random floating-point number between 0.0 and 1.0 (exclusive), while `random.randint(a, b)` generates a random integer between `a` and `b` (inclusive). -
How can I generate a random float within a specific range (e.g., between 5.0 and 10.0)?
You can use the following formula: `random.uniform(5.0, 10.0)`. This generates a random floating-point number within the specified range (inclusive).