Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

Python math.floor() Function – Examples and Explanation

Python math.floor() function is a mathematical function which is used to return the floor of given number value x, a number value that is not greater than x.

math.floor() Function in Python

math.floor() function exists in Standard math Library of Python Programming Language. The purpose of this function is to return a value which is less than or equal to a specific expression or value.

 

Syntax

The syntax of math.floor() function in python is:
math.floor ( x )

 

Parameters

x is any valid Python number (positive or negative).
Note: if the x parameter is not a number, math.floor() function will return an error.

 

Return Value

math.floor() function will return the largest integer not greater than given value x. In Python 2.x math.floor() function returns a float but in Python 3.x and above it returns an integer.

 

Python math.floor() Function Example and Explanation – 1


Output of Python math.floor() Function
15
-3
3

Note that in output all the numbers (whether they are negative or positive) are less than or equal to the input value by using math.floor() function.

Python math.floor() Function Example and Explanation – 2

Following code snippet takes an input from the user and checks if entered number can be converted into a float number. Then it calculates the floor value of the given number using the Python math.floor() function. It prints a message if the user has entered an invalid number such as string or spaces using Python built-in exception handling.


Enter a valid number (integer or float): 55
Output value is 55

 

Python math.floor() Function Example and Explanation – 3

In the following code snippet of this example, convert_list_new() function takes an argument of type list. It checks and converts all individual items of the list using math.floor() function. It appends all successfully converted items in a new list object using math.floor() function. If an item can not be converted it simply appends None as a placeholder of an item to maintain the exact index of the original list. convert_list_new() doesn’t modify the original list object instead it creates a new object as you can verify it from the ID of both objects.

In the function convert_list_modify() of this example, we check, convert and replace it with an item in the original list object. In this way, original object is modified. This is a natural behavior of mutable objects in Python. You can read more about mutable and immutable function parameter in this article.

You can check the IDs of the list object before and after calling convert_list_modify() function. The IDs are same but after calling convert_list_modify() function, IDs of the object are not same.


Using convert_list_new function

List before conversion - lst: [1.5, 2, 'a', {'x'}, {5}, (5+6j), ['a', 9.5], 8.7]
List after conversion - lst_copy: [1, 2, None, None, None, None, None, 8]

ID of lst and lst_copy objects: 30673816 30674176
check IDs of lst and lst_copy objects are different

Using convert_list_modify function
Check original lst object is modified: [1, 2, None, None, None, None, None, 8]
List after conversion - lst_modify: [1, 2, None, None, None, None, None, 8]

ID of lst and lst_modify objects: 30673816 30673816
check IDs of lst and lst_modify objects are same

 

Python math.floor() Function Example and Explanation – 4

Many of you may think, is there a short way to do the above task? The answer is yes.
List comprehension is a very concise, powerful and fast feature in Python. It will not solve your problem but will reduce the overhead of programming code quite a lot. We will use it to convert all numbers using the math.floor() function.
Let’s find the floor() of the numbers using this method. In following first list comprehension expression, I have tried to convert the numbers using the math.floor() function and list comprehension same as I did in the previous example using the math.floor() and  for loop. In the second expression, I have removed those elements which are not numbers because they can’t be converted using math.floor() function. This is a cleaner and better approach.


Using list comprehension
List before conversion - lst: [1.5, 2, 'a', {'x'}, {5}, (5+6j), ['a', 9.5], 8.7]
List after conversion - lst_1: [1, 2, None, None, None, None, None, 8]
List after conversion - lst_2: [1, 2, 8]

Explanation

I hope you are quite amazed. You never thought about that whole function can be converted in a single line. This is the power of list comprehension and of course, Python too. You should read the full article about this topic here. I hope the above code snippet is well explained and doesn’t require any explanation. I have well explained it in the previous example using for loop. The syntax of list comprehension expressions 1 and 2 are different. Read them carefully. They are self-explanatory.

I leave an exercise for you to re-write the previous example and remove all elements those are not numbers in the convert_list_new() function using the math.floor() function. Hope you do it easily. Best of luck.

 

LearnAndLearn.com © 2018 - All rights reserved