Python’s `math`

library is well stocked with many helpful functions. One of those is the **Python floor function** that will be discussed in this article.

The math module also includes similarly applicable functions, such as the `ceil()`

function I wrote about in this article and the `round()`

function.

## Python floor function explained

**The floor() function calculates the largest integer not greater or equal to the number x.** In other words, it rounds the number

`x`

down to the nearest integer value as shown in the figure below.The syntax is as follows:

math.floor(x)

where `x`

is the value you want to apply flooring to.

Below you can find the example of the `floor()`

function usage.

>>> import math >>> math.floor(3.14) 3

As you can see, you first have to import Python’s `math`

module. Then simply call the `floor()`

function passing the number as the first argument. **The result will be the nearest integer value smaller or equal to the specified number.**

NOTE- Python 2 returns afloor()function result as a floating point value (float). Python 3 returns it as an integer value (int).

In the table below you can see outcomes of using the `floor()`

function with different numbers.

x | math.floor(x) |
---|---|

3.14 | 3 |

4 | 4 |

2.99999999 | 2 |

-3.14 | -4 |

-4 | -4 |

-2.99999999 | -3 |

math.inf | OverflowError |

## How to perform a Python floor division?

Another usage of flooring in Python is more commonly known as Python floor division.** The goal of such usage is to make a division return the largest whole part of division. **

For example let’s say you have a number **7** as a divisor and number **2** as a dividend. By applying a regular division to those two numbers, you’d get **3.5** as a result.

>>> 7/2 3.5

But if you wanted to calculate how many times the number **2** can fit into number **7**, with any extra value as the remainder, you have to use the Python floor divison. This can be achieved by using the special `//`

floor division operator.

Let’s see how the floor division operator works with the above values.

>>> 7//2 3

The result represents how many times the whole dividend value can fit into the divisor value. In our example, we got number **3** as a result, which means that number **2** can fit **3** times into number **7** as a whole part.

Using the Python floor division operator is equivalent to the following `floor()`

function usage:

a // b == math.floor(a / b)

## Summary

Python’s `math`

module is a useful toolset that contains many functions, including the floor function. The result of using floor function in Python is the largest integer value not greater or equal to the passed argument. To use it, import the `math`

module and pass the number to the `floor()`

function. On the other hand, if you need to divide two mathematical expressions and get an integer value as a result, use the floor division operator. To learn more visit Python’s official documentation.

The Python `ceil`

function is similar to the `floor`

function, but the `ceil`

function rounds the number to the smallest integer that is equal or greater than a number passed as an argument. You can read more about it in this ZeroToByte article.