Introduction to Python floor() function
The Python math module consists of several functions that allow us to perform various mathematical calculations on real numbers. One of those methods is the python math.floor()
method, which returns the largest integer not greater than its argument. The Python math.floor()
function is part of the standard library, meaning it comes prepackaged with every installation of Python and is ready to be used immediately.
In this tutorial, we will learn about math.floor()
method in details. We will cover different examples explaining various scenarios using math.floor()
method. Moreover, we will take map()
function, lambda expression, and lists and see how we can work with them using math.floor()
method.
Getting started with floor() function
The Python math.floor()
method is a mathematical function that is used to return the floor of a given number value x
, a number value that is not greater than x
. The floor()
method is inside Python built-in module math. In this section, we will cover the basics of math.floor()
method, starting from the syntax.
Python flooring numbers
We already know the concept of rounding numbers in mathematics. It is a process by which we take a number with digits after the decimal place and figure out which whole numbers we can use to represent it instead of decimal points. Rounding in Python also returns the next closest integer to a given number. For example the number 5.4 will round down to 4, as it is only four steps away from number 4 but six steps away from 5, so it round down to 4.
However, the problem is when we get 4.5, which is equally far apart from 4 and 5. In such cases, python 4 will then choose the closest even integer to the given number and in the given case it will return 4. Now let us take some examples and see what we get in python. See the examples below.
# rounding in python
print(round(3.3))
print(round(4.4))
print(round(5.7))
Output:
3
4
6
Notice that we get the closest integer to the given decimal in the above cases.
Flooring numbers, on the other hand, is a bit simpler. When we are flooring numbers, we don't need to be worried about choosing the closest integer or looking for an even number. Instead, we only need to consider the number that comes before the decimal point. This is the floor of that number: Simply the largest integer less than or equal to a given number is called floor. In the following section, we will see the working and syntax of the python floor()
method.
Syntax of Python floor() function with examples
The Pythonmath.floor()
method performs a calculation similar to that of rounding down.If we given a number, floor()
will return the largest integer value less than or equal to that same number. The simple syntax looks like this:
math.floor(number)
The parameter should be a number, otherwise the floor()
function will return an error. Now let us take different examples. Before accessing the floor()
function, we have to first import the math module. See the example below:
# import math module
import math
# using floor function
print(math.floor(3.4))
print(math.floor(3.5))
print(math.floor(3.9))
Output:
3
3
3
Notice that we get the largest possible integer value but less than its argument. If we provide an integer number, rather than a decimal number, it will return the same number. See the example below, which uses integer value as an argument.
# importing math module
import math
# using python math.floor() function
print(math.floor(3))
print(math.floor(5))
print(math.floor(9))
Output:
3
5
9
You can see that we get the same number, so we can conclude that Python math.floor()
method returns the integer value less than or equal to the argument.
If we provide an argument, other than integer or decimal value, the python math.floor()
method returns an error. See the following example below.
# import math module
import math
# using python math.floor() function
print(math.floor("B"))
Output:
Notice that the python math.floor()
method, returns an error because it cannot take a string as an argument.
There is another way to use floor()
function, by directly importing it from math module, instead of importing the whole math module. See the following example, where we only imported the floor()
function from the math module.
# import python floor function from math module
from math import floor
# using python math.floor() function
print(floor(2.3))
print(floor(7))
Output:
2
7
Notice that we had used from keyword to import floor()
function from the math module.
The python math.floor() method works in a similar way when we provide a negative value. For example, it will return -3 for a -2.3 because -3 is less than the provided decimal number. See the example below:
# import math module
import math
# using python math.floor() function
print(math.floor(-1))
print(math.floor(-2.3))
print(math.floor(-0.4))
Output:
-1
-3
-1
Flooring Constants in Python
The Python standard library also contains several numeric constants that can be accessed through the math module. Some of which are pi, the value of e, and many more. See the following example, which prints out these constants.
# import math module
import math
# printing constants
print(math.pi)
print(math.e)
Output:
3.141592653589793
2.718281828459045
Now let us apply the python math.floor()
method on these constants and see what we get. See the following example.
# import math module
import math
# flooring constants
print(math.floor(math.pi))
print(math.floor(math.e))
Output:
3
2
Notice that we get 3 for pi value and 2 for e value. We can also apply the python math.floor()
method to the number after using different mathematical operations. See the following example.
# import math module
import math
# flooring constants
print(math.floor(3*3.3432))
print(math.floor(math.pi*34))
Output:
10
106
Difference between int() and floor() function in Python
One of the common mistakes most people do is they think floor and int functions are the same because they return the same result for all decimal numbers. However, this is not true. Although int and floor function return the same value for positive numbers, but things get change for negative values. See the examples below, which print shows the similarity of floor and int function.
# import math module
import math
# flooring
print(math.floor(3*3.3432))
# using int() function
print(int(3*3.3432))
# using python math.floor() function
print(math.floor(3.4))
# int method
print(int(3.4))
Output:
10
10
3
3
Notice that we get the same result. Now, you might also see int
and floor as same functions, but let see the examples below:
# import math module
import math
# using python floor function
print(math.floor(-3*3.3432))
# using int method
print(int(-3*3.3432))
# flooring
print(math.floor(-3.4))
# int method
print(int(-3.4))
Output:
-11
-10
-4
-3
For negative values, we get different results, because the floor function always returns values equal to or less than its parameter.
Python math.floor() method and lists
We can use python math.floor() method to floor given numbers in a list using a for loop to iterate over the list. See the following examples which print out the floor numbers from the list.
# import math module
import math
# list
num = [1,2.222, 5.32, -34.53]
# for loop
for i in num:
   print(math.floor(i))
Output:
1
2
5
-35
In a similar way, we can also apply the same method using the list comprehension method. See the example below which uses the list comprehension method.
# import math module
import math
# list
num = [1,2.222, 5.32, -34.53]
# list comprehension
print([math.floor(i) for i in num])
Output:
[1, 2, 5, -35]
Notice that this time we get a list as an output because we had used the list comprehension method.
Python math.floor() method and tuples
In a similar way, we can provide a tuple containing different numbers and can iterate over it to apply the python math.floor()
method to its elements. See the following example which uses for loop to iterate over python tuple.
# import math module
import math
# list
num =(1,2.222, 5.32, -34.430)
# for loop
for i in num:
   print(math.floor(i))
Output:
1
2
5
-35
Now let us apply the list comprehension method over tuple to apply python math.floor()
method to its elements.
# import math module
import math
# list
num =(1,2.222, 5.32, -34.430)
# list comprehension method
print([math.floor(i) for i in num])
Output:
[1, 2, 5, -35]
Python math.floor() function with lambda and map function
Now let us take an example to see how we can use the python map inside lambda function with python math.floor()
function to find out the floor of a number. See the example below:
# import math module
import math
# list
num =(1,2.222, 5.32, -34.430)
# list comprehension method
print(map(lambda i:math.floor(i),num))
Output:
<map object at 0x7f2a4bba44c0>
We get an object type because the python map function returns an iterable object. We can iterate over it to print out the elements or simply apply the list method. See the example below:
# import math module
import math
# list
num =(1,2.222, 5.32, -34.430)
# list comprehension method
print(list(map(lambda i:math.floor(i),num)))
Output:
[1, 2, 5, -35]
Summary
The Python floor()
function rounds a number down to the nearest integer. This method takes only one argument and returns the possible greatest integer value less than or equal to the argument. It can only take integer and float values as an argument, otherwise, it will return an error. In this tutorial, we learned about the python math.floor()
method. We learned how we can import a math module to get access to its floor()
function.
At the same time, we also discussed how we can apply python floor()
function on integers, decimal, and negative values. We also covered how floor()
method is different from int()
method in python. Moreover, we applied the python math.floor()
method of lists, tuples, and map() function. All in all, this tutorial covers everything that you need to know about the python math.floor()
method.
Further Reading
Python math.floor() function
Floor() function in Python
math module documentation