Python ceil() function Explained [Easy Examples]

 

Introduction to python ceil() function

Python is well known for its various different modules. One of those popular and most used modules is the python math module, which allows us to perform various mathematical calculations on real numbers. One of the methods which is very popular in the math module is the python ceil() function. It actually returns the smallest possible integer greater than or equal to the input number. The python ceil() function is part of the standard library, which means it comes with python packages and we don't need to install it manually. As soon as we install python on our system, the math library will be installed automatically.

In this tutorial, we will learn about the python ceil() function by taking different examples. We will also take the python map() function, lambda expression, and lists to see how we can work with them using the python math.ceil() function.

Advertisement

 

Getting started with Python ceil() function

We already discussed in the above section that the python ceil() function is a mathematical function that returns the ceiling value of the given number, lets say x. The output of the python math.ceil() function is  be less than, either it should be greater or equal. In this section, we will cover different examples related to the python ceil() function, but first, let us see the basic syntax of the python math.ceil() function.

 

Examples and syntax of python ceil() function

It performs the calculation very similar to the rounding method, with a difference it cannot return a value less than the given number. The simple syntax of python ceil() function looks like this:

math.ceil(number)

The number or the parameter should be any real number. If we will provide a string or any other data type, the python ceil() function will return an error. Now let us take different examples.

# importing math module
import math

# python math.ceil() function
print(math.ceil(5.5))
print(math.ceil(5.1))
print(math.ceil(5.9))

Output:

6
6
6

Notice that we get 6 for all cases which is the smallest value greater than the argument.

Now let us try by giving an integer value as an argument, rather than a decimal number. In such cases, the python ceil() function will return the same value. See the examples below which uses integer values as an argument.

Advertisement
# importing math module
import math

# python math.ceil() function
print(math.ceil(5))
print(math.ceil(1))
print(math.ceil(9))

Output:

5
1
9

Notice that we get the same number, so we can conclude and confirm from the above examples that the python ceil() function returns an integer value greater than or equal to the argument.

If we provide an argument other than integer or decimal value, the python ceil() function will return an error. See the following example.

# importing math module
import math

# python math.ceil() function
print(math.ceil("A"))

Output:

Python ceil() function Explained [Easy Examples]

You can see that the python ceil() function had returned TypeError because it cannot take strings as an argument.

One more thing we can do is to directly import ceil() function from the math module, instead of importing the whole math module. See the following example, where we only import the ceil() function from the math module. See the example below:

# importing ceil from math
from math import ceil

# python math.ceil() function
print(ceil(3.4))
print(ceil(5))

Output:

4
5

From now onward, we will only import the python ceil() function from the math module, rather than importing the whole math module.

Advertisement

The python ceil() function works in a similar way with negative values as well. For example, it will return -4 for -4.7 because -4 is the upper or greater value than -4.7. See the example below:

# importing ceil from math
from math import ceil

# python math.ceil() function
print(ceil(-3.4))
print(ceil(-4.5))
print(ceil(-5.6))

Output:

-3
-4
-5

 

Python ceil() function with constants

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.

# importing math module
import math

# python constants
print(math.pi)
print(math.e)

Output:

3.141592653589793
2.718281828459045

Now let us apply the python ceil() function on these built-in functions and see what we get. See the following example.

# importing math module
import math

# python math.ceil() method on constants
print(math.ceil(math.pi))
print(math.ceil(math.e))

Output:

4
3

 

Python ceil() function with mathematical operations

In Python language, we have different mathematical operations, for example, addition, multiplication, division, subtraction, and many more. Sometimes, we get floating numbers while performing mathematical operations, in such cases, we can use the python ceil() function to give us integer values rather than floating numbers.

See the example below:

Advertisement
# importing ceil function from math
from math import ceil

# python math.ceil() function on mathematical operations
print(ceil(3*34.453))
print(ceil(45/7))
print(ceil(74.09+34.43))

Output:

104
7
109

Notice that the output is no more a floating number. The python ceil() function had returned the smallest integer value that is greater or equal to the actual number.

 

Python ceil() function vs Python int() function

You may think that the python ceil() function works in a similar way as the python int() function does, but that is not true.

They both are different functions having different properties. Although they both return integer values still they are not the same. Python int() function always returns a value less than or equal to the given positive decimal number, while python ceil() function returns the large or equal integer value to the given decimal number.  See the example below:

# importing ceil function from math
from math import ceil

# python math.ceil() function
print(ceil(3.3))
print(ceil(3.9))

# python int() function
print(int(3.3))
print(int(3.9))

Output:

4
4
3
3

Notice that we get different integer values for the given decimal points. However, both functions return the same integer values for negative decimal numbers. See the example below:

# importing ceil function from math
from math import ceil

# python math.ceil() function
print(ceil(-3.3))
print(ceil(-3.9))

# python int() function
print(int(-3.3))
print(int(-3.9))

Output:

-3
-3
-3
-3

Notice for negative values, both functions return the same values.

 

Example of python ceil() function with lists

In this section, we will learn how we can use python for loop to iterate and apply the python ceil() function on a list of numbers. See the example below:

# importing ceil function from math
from math import ceil

# set of numbers
num = [1, 2.232, 3.43, 53.54]

# for loop
for i in num:

   # python math.ceil() function
   print(ceil(i))

Output:

1
3
4
54

In a similar way, we can use the python list comprehension method to iterate over list given list and apply the python ceil() function. See the example below:

# importing ceil function from math
from math import ceil

# set of numbers
num = [1, 2.232, 3.43, 53.54]

# list comprehension method
print([ceil(i) for i in num])

Output:

[1, 3, 4, 54]

Notice that the output is a list of integers because the list comprehension method returns a list.

 

Examples of Python ceil() function with Python tuple

We know that a python tuple is also an iterable object so we can use the same method to iterate python tuple and apply the python ceil() function on its elements. See the example below:

# importing ceil function from math
from math import ceil

# set of numbers
num = (1, 2.232, 3.43, 53.54)

# for loop
for i in num:

   # python math.ceil() function
   print(ceil(i))

Output:

1
3
4
54

we can also apply the list comprehension method to iterate over tuple and return a set of integer values. See the example below:

# importing ceil function from math
from math import ceil

# set of numbers
num = (1, 2.232, 3.43, 53.54)

# list comprehension method
print([ceil(i) for i in num])

Output:

[1, 3, 4, 54]

 

Examples of python ceil() function with python map() and lambda expression

Now, in this section, we will see how we can use the python map() function and lambda expressions with the python ceil() function. See the following example which uses the map and lambda expression while applying the python ceil() function.

# importing ceil function from math
from math import ceil

# set of numbers
num = [1, 2.232, 3.43, 53.54]

# python map and lambda expression
# python math.ceil() function
print(map(lambda i:ceil(i),num))

Output:

<map object at 0x7f71911b54c0>

Notice that we had got object type, it is because the map function returns an iterable object. We can use for loop or list comprehension to iterate over the object to get access to the elements. See the example below:

# importing ceil function from math
from math import ceil

# set of numbers
num = [1, 2.232, 3.43, 53.54]

# python map and lambda expression
# python math.ceil() function
print( [i for i in (map(lambda i:ceil(i),num))])

Output:

[1, 3, 4, 54]

 

Summary

Python ceil() function is a function found inside the python math module, which returns the smallest integer value greater than or equal to the argument. It can take only one int or float type argument. In this tutorial, we learned everything that you need to know about the python ceil() function. We covered different cases with examples and applied ceiling function. We also covered how we can iterate over an iterable object and apply the ceiling method. All in all, this tutorial provides a full understanding of the python ceil() function.

 

Further Reading

Python ceil() function
Python numpy ceil() function
Python math module

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment