Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What are Ceil() and Floor() in Python?
2.1.
Ceil() in Python
2.2.
Syntax
2.3.
Implementation
2.4.
Floor() in Python
2.5.
Syntax
2.6.
Implementation
3.
What is the difference between the two?
4.
Pros and cons of using these functions
5.
Examples
6.
Frequently asked questions
6.1.
What do you mean by dynamically typed language?
6.2.
What is the floor 2.4 ceil 2.9 equal to?
6.3.
How else can we compute the ceil value of division of 2 numbers(a/b)?
6.4.
Calculate the value of ceil(3.4) & floor(-3.4)?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

ceil() and floor() in Python

Author Komal
0 upvote
gp-icon
Basics of Python
Free guided path
7 chapters
99+ problems
gp-badge
Earn badges and level up

Introduction

Welcome Ninjas! This blog will follow a series of topics like ceil() in python and floor() in python, but before that, we will discuss the basics of Python language to make you familiar with it.

ceil and floor in python

Before discussing, ceil() in python and floor() in python, let's see what Python is. Guido van Rossum developed Python during 1985- 1990. Python is a high level open source programming language that is gaining popularity nowadays. Python is a general-purpose language. It is an interpreted but not compiled language. Other features of Python include- being interactive and object-oriented. Python is a dynamically-typed language. Now let's look into the primary concern of our blog i.e. ceil() in python and floor() in python.

Let us get started! 

Check this out, ping command in linux

What are Ceil() and Floor() in Python?

Ceil() in Python

ceil(n) in python is an inbuilt math function in Python that returns the smallest integer number which is greater than or equal to n.

For example, let's say n=3.6, then the smallest integer greater than it is 4; therefore the ceil of 3.6 would be 4. 

Syntax

import math
math.ceil(number)

In the above syntax:

  • math is the standard Python module that contains mathematical operations. You have to import this module before using the ceil() function.
  • ceil() is the function present in the math module for performing the ceiling operation.
  • number is the value for which you want to find the smallest integer that is greater than or equal to this number.

Implementation

#Import the math library first
import math  
#using function .ceil() of math lib
print(math.ceil(3.6))
print(math.ceil(-3.6)) 
output

Floor() in Python

floor(n) in python is an inbuilt mathematical function in Python that returns the largest integer not greater than n.

For example, for any positive real number, say 3.6, the largest integer which is not greater than 3.6 is 3.

Syntax

import math
math.floor(number)

In the above syntax:

  • math is the standard Python module that contains mathematical operations. You have to import this module before using the floor() function.
  • floor() is the function present in the math module for performing the floor operation.
  • number is the value for which you want to find the largest integer that is less than or equal to this number.

Implementation

#Import the math library first
import math  
#using function .floor() of math lib
print(math.floor(3.6))
print(math.floor(-3.6)) 
output

Also see, Python Filter Function

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

What is the difference between the two?

Let's establish the difference between the two functions-ceil() in python and floor in python.

ceil() in python

floor() in python

ceil(n) function in python returns the smallest integer greater than n.

floor(n) returns the largest integer not greater than n.

The ceil(x) in python function rounds the x value upwards.

The floor(x) function rounds the x value downwards.

ceil() is found in math library

floor() is found in math library

Eg. ceil(8.6)=9

Eg. floor(8.6)=8





 
Why do we use these built-in functions?

We come across many problems while solving competitive programming problems or Data structure algorithms where the use of ceil() in python and floor() is very common, so we import math beforehand and then simply use ceil() in python and floor() in python. Let’s see the separate implementations of both:

  • ceil(n) in python is used to round up the value i.e. it returns the smallest integer greater than n.
  • floor(n) in python is used to round-down value i.e it returns the largest integer not greater than n.

Both ceil() in python and floor() in python are two important functions of the math library having specific functionalities.

Pros and cons of using these functions

As the ceil() in python and floor() in python are inbuilt functions, they are easy to use and implement. We just have to import the math library in Python The main feature of ceil() in python is to return the rounded-up value of the parameter. The main feature of floor in python is to return the largest integer not greater than the parameter.

Both ceil() in python and floor() in python takes inputs only in the form of real numeric data, integer(int, boolean & float).

Overall, ceil() in python and floor in python are very useful functions.

Examples

Let us see an example with various calculations of ceil() in python and floor() in python. As we know that the ceil function rounds up the number, therefore, ceil value of 8.4 would be 8 and ceil value of -5.6 would be 5.

#Import the math library first
import math  
#examples of function .ceil() & .floor() of math library
print(math.ceil(8.4))
print(math.ceil(-5.6))
print(math.floor(4.3))
print(math.floor(0))
output

Frequently asked questions

What do you mean by dynamically typed language?

It means that the memory is allocated at runtime.

What is the floor 2.4 ceil 2.9 equal to?

The floor of 2.4 rounds down to 2, and the ceil of 2.9 rounds up to 3.

How else can we compute the ceil value of division of 2 numbers(a/b)?

We can calculate ceil value using the following expression ceil(a/b) = (a/b) +((a%b)=0)).

Calculate the value of ceil(3.4) & floor(-3.4)?

ceil(3.4) is equal to 4 and floor(-3.4) is equal to -4.

Conclusion

We hope this blog was easy to understand. Firstly, We discussed the basic concepts of python language and its features, followed by the use of ceil() in python and floor() in Python, their implementation, and examples.

If you found this blog interesting and insightful, refer to similar blogs:

Python Introduction

Python Operators

Refer to the Basics of C++ with Data StructureDBMS, and Operating System by Coding Ninjas, and keep practicing on our platform Coding Ninjas Studio. You can check out the mock test series on code studio.

You can also refer to our Guided Path on Coding Ninjas Studio to upskill yourself in domains like Data Structures and AlgorithmsCompetitive ProgrammingAptitude, and many more! Refer to the interview bundle if you want to prepare for placement interviews. Check out interview experiences to understand various companies' interview questions.

Give your career an edge over others by considering our premium courses!

Happy Learning!

 

Thankyou image

 

Previous article
Sep() parameter in Python
Next article
What is the Difference Between del, pop and remove in Python?
Guided path
Free
gridgp-icon
Basics of Python
7 chapters
127+ Problems
gp-badge
Earn badges and level up
Live masterclass