Self-paced

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Search from all Lessons


LoginGet Started
← Back to How to's
Edit on Github

What Does // Mean in Python?

Written by:

What Does // Mean in Python?
Similar Methods of a Floor Division

What Does // Mean in Python?

One of the many operators in Python is the double slash // operator, which refers to an integer division (or a division with no remainder/decimals) which is called Floor Division. When dividing two numbers using this // operator, the result will always be an integer, ignoring the decimal part of the result.

Loading...

The double slash // has the same function as the Python method math.floor (which we will discuss later), as its name implies, rounds the result down (rounds to its floor) to the closest integer.

Floor Division Example

In this article, we will review more thoroughly how the Floor Division differs from a normal division, how the // operator works with different types of numbers (negative numbers and decimal numbers) and some extra methods similar to a Floor Division.

Difference Between Floor Division // and Regular Division /

A normal division / in Python will always return a number with its decimal part, that is, a floating number, while a floor division will always return an integer number.

The result of regular division (using the / operator) is 45/6=7.5, but using // has floored 7.5 down to 7.

We can see another example as follows:

Loading...

Working with Negative Numbers in Floor Divisions

To understand how to work with negative numbers using floor division, we need to understand that the floor of a positive number is different from the floor of a negative number. For example, the floor of 3.4 is 3, but the floor of -3.4 is -4, not 3. For greater understanding, we could say that the floor of a positive number would be the integer below and that the floor of a negative number would be the integer that is above (maintaining the negative sign).

We can see another example as follows using the same numbers as before:

Loading...

Working with Floats in Floor Divisions

When we work with float numbers using floor division, the result will always be a float number, and as we already know, the result will be the nearest integer rounded to its floor, but in this case, it will be represented by a float number.

Let's have a look at the following float-based floor division example:

Loading...

As a result, the floor division operation is carried out using floats, and a float with an integer returns the value rounded down to the nearest integer represented by the floats.

Similar Methods of a Floor Division

In Python, there are several methods similar to operations with // (floor division). But, how would these other methods be useful to us? It all depends on our use case, in some situations we will find ourselves needing to handle only integers in our projects, and since floor division operations can get both integers and floats, we could use a hand in these cases.

This is where these two additional methods come in, which are math.floor() and math.ceil, they will allow us to manipulate the result so that it is always an integer.

math.floor() Function in Python

Python comes with a built-in math module that includes a variety of practical calculating tools. The math.floor() function is one of the math module's built-in features. This function takes a numeric input and rounds it down to the closest integer to return the floor value.

Therefore, math.floor() is an alternative to the // operator since they accomplish the same goal in the background, but always returning an integer.

Let's have a look at the following example:

Loading...

math.ceil() Function in Python

math.ceil() is an alternative to math.floor() but it will always round up to the nearest whole number rather than down.

For instance:

Loading...

Summary

The next video will be of great help to better understand what was explained in this What Does // Mean in Python? article

Python Floor Division Tutorial (Double Forward Slash)