Flat_Eric learning about math functions

Math Functions

Writing your first program.

The Math is Mathing

In the last lesson, we saw how Python works like a high-powered calculator for basic math. But it goes way beyond just and . We also leveled up our skills by using floor division "Floor Division (//) 'The Whole Number Finder.' Divides two numbers and chops off the decimal to give you the nearest whole number." , the modulo operator "Modulo Operator (%) 'The Leftover Finder.' Divides two numbers and gives you only the remainder left over." , and exponents "Exponents (**) 'The Power-of.' Multiplies a number by itself a set amount of times (e.g., 2 ** 3 is 2 x 2 x 2)." to handle more complex logic.

 
print(type(6)) # class 'int'
print(type(2 - 4)) # class 'int'
print(type(2 * 4)) # class 'int'
print(type(2 / 4)) # class 'float'

print(2 ** 3) # 8
print(5 // 4) # 1
print(6 % 4) # 2
    

As seen in the previous snippet, Python can handle mathematical equations with ease. You can use standard operators to perform basic calculations:

In addition to calculating values, you can use built-in functions to interact with these equations:

Math Functions

The round() function: Another helpful tool in Python is the round() function. This allows you to take a decimal number (a float) and round it to the nearest whole number.

When you combine functions—such as using round() inside of a print() statement—you can see the result immediately. Furthermore, if you check the result using the type() function, you will see that a rounded number is classified as an int (integer) rather than a float.

 
print(round(5.3))
print(round(5.6))
print(type(round(1.4)))

    
 
5
6
class 'int'

    

The abs() function: The next mathematical function we will explore is abs(), which calculates the absolute value of a number.

In mathematics, the absolute value represents the distance a number ('x') is from zero on a number line. Because distance can never be negative, the abs() function always returns a non-negative result.

 
print(abs(5))
print(abs(-5))
print(type(abs(-5.5)))

    
 
5
5
class 'float'

    

Expanding Your Toolkit: The Math Module

So far, we have explored several common math functions—and perhaps one or two that were new to you. Python is a powerful programming language capable of performing a vast array of mathematical operations.

It is important to remember that no programmer memorizes every single function. In fact, one of the most valuable skills you can develop is knowing how to find the right tool for the job. If you run into a mathematical problem, there is almost certainly a built-in function to solve it; you just need to know where to look.

By searching online for "Python math functions," you can easily find the official Python Documentation. For example, the Python 3 Math Module page provides a complete list of every available math function and a clear explanation of what each one does.

As you grow as a programmer, the documentation will become one of your most-used resources.

Time to experiment!

Coding Exercises (VS Code) Instructions:

Exercise 1:

The Absolute Difference Create a program that subtracts a larger number from a smaller number (e.g., 10 - 50). Use a Python function to ensure the final result is displayed as a positive number, regardless of the order of subtraction.

Exercise 2:

Nested Functions Perform a calculation that results in a decimal (for example, 22 ÷ 7). Write a single line of code that rounds that result and then prints the final value to the console.

Exercise 3:

Powers and Exponents (Research Required) In Python, there is a built-in function that allows you to calculate "to the power of" (like 2³, which is 2 x 2 x 2 = 8). Search online for the built-in Python function for exponents.

Hint: It is a three-letter word, not **.

Exercise 4:

Finding the Minimum (Research Required) Imagine you have a list of numbers: 5, 2, 9, 11. Search for a built-in Python function that can look at a group of numbers and automatically pick out the smallest one.

Hint: It is a three-letter word that is the opposite of "max."

Exercise 5:

The "Clean Distance" Calculator Calculate the difference between -15.67 and 12.34. However, your final output must meet these three conditions:

  1. The result must be a positive number (even if the subtraction results in a negative).
  2. The result must be rounded to the nearest whole number.
  3. The final output must be an integer (check this using the type() function).

The Task: Write a single line of code (or a small script) that performs the subtraction and then uses three different functions to meet the requirements above. Both the numerical answer and the datatype should be printed to the screen.

Hint: Think about the order. Do you want to round the number before or after you make it positive? Does it matter?

Click on the GitHub cat, to open github desktop.

Don't Forget to commit and Push!

This course was built by DevSTEM - we turn teaching materials into interactive web courses like this one.

Build your own course