Ceil in Python
Learn via video course

Abstract
Ceil is a function defined in Python's math module which takes a numeric input and returns the integer greater than or equal to the input number. It is equivalent to the Least integer function in mathematics.
Scope of article
- This article discusses the ceil function in Python, goes over the input parameters and return values, and finally shows some usage examples.
- This article does not discuss the implementation of the ceil function in Python.
Introduction to Ceil Function in Python
Suppose you're writing a program that calculates the interest on some loan, and the bank requires that all such monetary amounts should not have fractional amounts of rupees (like paisa) for easier transactions. Keeping in mind that the bank should not lose money, you decide to round off the resultant amount to the next nearest integer.
So how would you do it? Using ceil() function in Python!
Ceil function is provided in Python's math module, which is comes included in the Python Standard Library. It is the same as the Least Integer Function or the Ceil Function used in mathematics.
In mathematical notation, if you're given a real number , ceil(x) is depicted using , where the upper direction of the brackets refers to ceiling operation (as the ceiling lies above your head). Inversely, the floor(x) (which returns the greatest integer ) is depicted using , where the downward symbol depicts the flooring operation.
Defining in a piecewise manner, ceil(x) = :
What this piecewise formula says is that if the input for the ceil function is an integer, then the greatest integer greater or equal to that number is that number itself. Otherwise, just add one to that number and then floor.
Ceil function in Python also performs the same way. It returns the smallest integer greater than or equal to the input real number. For example,
Syntax of Ceil in Python
or
- where x is a real numeric input
The ceil() function in Python can be called in two ways depending upon what you have imported into your Python program.
-
If you have imported the entire math module and not just the function itself, then we have to access the ceil() using the dot (.) operator (since it is defined in math module). If is our output variable and is our numeric input variable, then the syntax is as follows:
-
If you have imported the ceil() function along with the math module, then the syntax for calling the function becomes simpler, and is as follows:
-
If you have some other module also imported which has its own ceil() defined (which may or may not be for the same purpose), then you have to use the first method to avoid ambiguity.
Parameter Values of Ceil in Python
As we saw in the section above, ceil() in Pytho takes a single numeric input. Numeric types in Python include int, float, and complex, but only the real types, that is, int and float are acceptable input parameters for ceil().
Important: boolean is a subtype of int, so True and False values will also be accepted as input. In fact, they will be treated as 1 and 0 respectively by Python ceil().
Using non real-numeric parameters will result in an error. For example:
For the code using string as input:
the output is
For the code using list as input:
the output is
And finally, an example of what happens when we use complex data type in ceil():
For the code using complex number as input:
We get:
Returns of Ceil in Python
As would be obvious, the ceil() function in Python returns a single numeric int type value, irrespective of whether the input was int or float data type. The value of the output is the smallest integer greater than or equal to the input number.
Examples of using Ceil in Python
It is a good practice to also print the types of our identifiers when learning something new, so in the upcoming examples, we will verify what we claim by not only printing the output but also its type.
Example 1
Output:
The smallest integer which greater than or equal to the input = 1.234 is 2, which is the same as the output!
As can be seen here, we used the ceil() function without importing it. Our input was a float and our output was an int value.
Example 2
Output:
Since the input parameter 14 is an integer itself, therefore by the smallest integer greater than or equal to 14 is 14 itself!
Notice that here, our output is of type int, same as that of input value.
Example 3
Output:
We know that , therefore the smallest integer greater than or equal to the input value is -23.
Notice that here we have not imported the entire math module, but just the ceil() method which we required.
This is an example of a negative float input. The behaviour is the same in the case of a negative integer too.
Conclusion
To summarize the article
- ceil() is a function defined in math module of Python's Standard Library which returns the smallest integer greater than equal to the input
- We can use the ceil() function in Python by calling either ceil(x) or math.ceil() depending on what all we've imported in our program
- Only real numeric data types, which include int, float, and boolean (the last one being a sub-type of int) are allowed as input parameters for ceil in Python.
- ceil in Python returns an output of type int for all valid inputs