Day 13: Math built-in functions

On Day 13 of our coding journey, we learned about the built-in math functions that are available in most programming languages.

These functions provide a wide range of functionality and help in performing common tasks without having to write code from scratch. In Python, there are many built-in functions available, including math built-in functions. Math built-in functions perform mathematical operations and are often used in scientific and engineering applications.

In this post, we will focus on math built-in functions in Python. We will discuss the most commonly used math built-in functions and the math module in Python, which provides additional math functions. We will also explore how math functions can be used in practical applications to solve real-world problems. So, let's dive in and explore the world of math built-in functions in Python!

You can see math module documentation here:

https://docs.python.org/3/library/math.html

1. Overview

The math module in Python provides additional math functions beyond the built-in ones we've discussed so far. Here's how you can import the math module and use some of its functions:

import math

# Using math.pi to get the value of pi
print(math.pi)  # Output: 3.141592653589793

# Using math.ceil() to round up a number to the nearest integer
print(math.ceil(4.2))  # Output: 5

# Using math.floor() to round down a number to the nearest integer
print(math.floor(4.8))  # Output: 4

# Using math.sin() to calculate the sine of an angle in radians
print(math.sin(math.pi/2))  # Output: 1.0

# Using math.cos() to calculate the cosine of an angle in radians
print(math.cos(math.pi))  # Output: -1.0

# Using math.radians() to convert degrees to radians
print(math.radians(180))  # Output: 3.141592653589793

# Using math.degrees() to convert radians to degrees
print(math.degrees(math.pi/2))  # Output: 90.0

2. Math module

Here are some examples of how to use math built-in functions in Python:

  1. Using abs():
  2. Using pow():
  3. Using sqrt():
  4. Using round():
  5. Using max():
  6. Using min():
  7. Using sum():

3. Math in Action