Python’s built-in math functions

in

Yep, you heard that right no need for external libraries or complicated calculations here! Just good old fashioned Python magic.

So Let’s get right into it with some examples and see what kind of tricks we can pull off with these bad boys. First up is the classic square root function: `math.sqrt()`. This ones a real lifesaver when you need to find the square root of a number, whether it be for calculating distances or figuring out how many chocolate bars you’ll have left after sharing them with your friends (assuming they don’t eat all of them first).

Here’s an example:

# Importing the math module to use its functions
import math

# Assigning a value to x
x = 25

# Calculating and assigning the square root of x to result
result = math.sqrt(x)

# Printing out the result with some friendly text
print("The square root of", x, "is:", result)

# The above script uses the math.sqrt() function to calculate the square root of a given number and prints out the result with some friendly text. The math module is imported to access the sqrt() function, and a value of 25 is assigned to the variable x. The result variable is then assigned the square root of x, which is calculated using the sqrt() function. Finally, the result is printed out using the print() function, along with some text to make it more user-friendly.

You’ve just calculated the square root of 25 using Python. Pretty cool, huh? But wait theres more! How about we try something a little bit more advanced, like calculating the sine or cosine of an angle? No problem with our trusty math module:

import math # Importing the math module to use its functions
angle = 45 # Assigning a value to angle (in degrees)
result1 = math.sin(math.radians(angle)) # Calculating and assigning the sine of angle in radians to result1
result2 = math.cos(math.radians(angle)) # Calculating and assigning the cosine of angle in radians to result2
print("The sine of", angle, "degrees is:", round(result1 * 100) / 100) # Printing out the rounded value of the sine with some friendly text
print("The cosine of", angle, "degrees is:", round(result2 * 100) / 100) # Printing out the rounded value of the cosine with some friendly text

And there you have it your very own sine and cosine calculator! But wait what if we want to calculate something even more exotic, like a logarithm or an exponential? No problem again: Python’s got us covered. Heres how to use the `math.log()` function for finding the natural logarithm of a number (also known as the “ln” function):

# Importing the math module to use its functions
import math

# Assigning a value of 10 to the variable x
x = 10

# Calculating the natural logarithm of x using the math.log() function and assigning it to the variable result
result = math.log(x)

# Printing out the result with some friendly text
print("The natural logarithm of", x, "is:", round(result * 100) / 100)

# The math.log() function calculates the natural logarithm of a given number
# The round() function rounds the result to two decimal places
# The print() function displays the result with some friendly text to make it more user-friendly

And for finding an exponential (also known as a power function), we can use Python’s built-in `**` operator:

# Importing the math module to use its functions
import math

# Assigning a value to x
x = 2.5

# Calculating and assigning the exponential of x (e raised to the power of x) to result
result = math.exp(x)

# Printing out the rounded value of the exponential with some friendly text
print("The exponential of", x, "is:", round(result * 1000000) / 1000000)

# The above script calculates the exponential of a given number using the built-in math module in Python. 
# The math module provides various mathematical functions, including the exp() function used in this script. 
# The value of x is assigned to 2.5, and the result variable stores the calculated exponential value. 
# The print statement displays the result with some friendly text and rounds it to six decimal places.

And that’s it! You now have a basic understanding of Python’s built-in math functions. Of course, there are many more advanced functions and concepts to explore (such as complex numbers or numerical integration), but we’ll leave those for another day. For now, let’s just enjoy the simple pleasures of life: chocolate bars, square roots, sines, cosines, logarithms, exponentials… you get the idea!

So go ahead and try out some of these functions in your own code who knows what kind of crazy calculations you’ll be able to perform with Python’s built-in math magic? The possibilities are endless, my friends.

SICORPS