Hit enter after type your search item

What is Python floor function?

The floor function is the part of math module that returns the floor of a given number (x) which is the largest integer number less than or equal to the x. For example:

floor(5.5) = 5

floor(-3.9) = 4

This is how floor() function is used in Python:

math.floor(x)

The floor function takes an argument that is a number for what you want to get the floor value.

An example of using floor with integer numbers

In this example, I used the floor() function four times. Different floating point numbers are given including a negative number.


The result of above code is:

Python floor

The first two numbers are 1.6 and 1.3 and both resulted in 1.

Did you know? The floor function does not affect integer numbers. For example, floor(3) will return 3, floor(100) will return 100, floor(-100)  will return -100  and so on. Its floating-point numbers that we get integers as using the floor function.

What is ceil function?

This function returns the ceiling of the given number (x) which is the smallest integer greater than or equal to x.

Just like the floor() function, you have to import the math module in order to use this function.

To demonstrate the usage of ceil() function, I used the same numbers as for floor() function to get the ceil. See the output and difference by comparing with the above result:


The result:

Ceil of 1.6 =  2

Ceil of 1.3 =  2

Ceil of -5.6 =  -5

Ceil of 105.4 =  106

You can see, unlike the floor() function, ceil for 1.6 and 1.3 resulted in 2.

Getting the floor of list items

If you have a list of items that are floating point numbers and you want to get the floor then one of the ways is using the lambda function with floor() and getting the floor.

The code below uses a list of a few items that contain float and int items:


The result:

The Floor of list items:  [10, 25, 35, -26, -51, 100, 105]

Similarly, you may get the ceil of list items for the same list as in above example:


The output:

The ceil of list items:  [11, 26, 36, -25, -50, 100, 106]

You may learn more about using the lambda function in its tutorial: Python lambda

This div height required for enabling the sticky sidebar