Learning library

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

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

Machine Learning Engineering (16 weeks)

Full-Stack Software Developer

Search from all Lessons

Social & live learning

The most efficient way to learn: Join a cohort with classmates just like you, live streams, impromptu coding sessions, live tutorials with real experts, and stay motivated.

From zero to getting paid as a developer, learn the skills of today and tomorrow. Boost your professional career and be hired by a technology company.

Start Coding

← Back to How to's
Edit on Github

Written by:

## 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.

``````1num1 = 19
2num2 = 5
3result1 = num1 / num2
4result2 = num1 // num2
5
6print("normal division of", num1, "by", num2, "=", result1)
7print("floor division of", num1, "by", num2, "=", result2)
8
9# Output:
10# normal division of 19 by 5 = 3.8
11# floor division of 19 by 5 = 3``````

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. 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:

``````1num1 = 19
2num2 = 5
3result1 = num1 / num2
4result2 = num1 // num2
5
6print("normal division of", num1, "by", num2, "=", result1)
7print("floor division of", num1, "by", num2, "=", result2)
8
9# Output:
10# normal division of 19 by 5 = 3.8
11# floor division of 19 by 5 = 3``````

## 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:

``````1num1 = 19
2num2 = 5
3num3 = -19
4result1 = num1 / num2
5result2 = num1 // num2
6result3 = num3 // num2
7
8print("normal division of", num1, "by", num2, "=", result1)
9print("floor division of", num1, "by", num2, "=", result2)
10print("floor division of", num3, "by", num2, "=", result3)
11
12# Output:
13# normal division of 19 by 5 = 3.8
14# floor division of 19 by 5 = 3
15# floor division of -19 by 5 = -4``````

## 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:

``````1num1 = 17.5
2num2 = 3.3
3num3 = 10
4num4 = 2.5
5num5 = 13.4
6num6 = 3
7result1 = num1 // num2
8result2 = num3 // num4
9result3 = num5 // num6
10
11print("floor division of", num1, "by", num2, "=", result1)
12print("floor division of", num3, "by", num4, "=", result2)
13print("floor division of", num5, "by", num6, "=", result3)
14
15# Output:
16# floor division of 17.5 by 3.3 = 5.0
17# floor division of 10 by 2.5 = 4.0
18# floor division of 13.4 by 3 = 4.0``````

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:

``````1# importing the math library
2import math
3
4# using the math.floor() function and printing the values
5print(math.floor(2.4)) #Output 2
6print(math.floor(9.3)) #Output 9
7print(math.floor(-7.3)) #Output -8
8print(math.floor(25.7)) #Output 25
9print(math.floor(11.0)) #Output 11``````

### `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:

``````1# importing the math library
2import math
3
4# using the math.ceil() function and printing the values
5print(math.ceil(2.4)) #Output 3
6print(math.ceil(9.3)) #Output 10
7print(math.ceil(-7.3)) #Output -7
8print(math.ceil(25.7)) #Output 26
9print(math.ceil(11.0)) #Output 11``````

## Summary

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