Python / vs //

In Python, the / and // operators are both used for division, but they behave differently. The / operator performs normal division and always returns a float, while the // operator performs floor division and returns an integer result, discarding any fractional part source.

Here’s a detailed tutorial explaining the differences between / and // operators in Python, with examples.

Python’s Division Operators

When it comes to division in Python, you have two choices: the / operator and the // operator. While they may look similar, they serve different purposes and return different results. In this tutorial, we’ll explore the distinctions between these operators and when to use each one.

Read How to Write a Variable to a File in Python?

The / Operator: Normal Division

The / operator in Python performs normal division, just like you learned in school. It divides the left operand by the right operand and returns a floating-point result, even if the division yields a whole number.

Here’s an example:

result = 10 / 3
print(result)  

Output:

3.3333333333333335

I have executed the above example and added the screenshot below.

Python slash vs doubleslash

In this case, 10 is divided by 3, and the result is a float: 3.3333333333333335.

Even if the division results in a whole number, the / operator still returns a float:

result = 6 / 2
print(result)

Output:

3.0

Here, 6 divided by 2 equals 3, but the result is still a float: 3.0.

Check out Understand for i in range Loop in Python

The // Operator: Floor Division

The // operator, on the other hand, performs floor division. It divides the left operand by the right operand and rounds the result down to the nearest integer, discarding any fractional part.

Here’s an example:

result = 10 // 3
print(result)  

Output:

3

I have executed the above example and added the screenshot below.

Python slash vs doubleslash doubleslash

In this case, 10 is divided by 3, and the result is rounded down to 3. The fractional part is discarded.

If the division results in a whole number, the // operator returns that whole number as an integer:

result = 6 // 2
print(result)  

Output:

3

Here, 6 divided by 2 equals 3, and the result is an integer.

Read How to Use Constructors in Python?

Handle Negative Numbers

When dealing with negative numbers, the behavior of the // operator might be surprising at first. It still rounds down, but it rounds towards negative infinity.

Here’s an example:

result = -10 // 3
print(result)  # Output: -4

In this case, -10 divided by 3 equals -3.3333..., but the // operator rounds it down to -4, not -3.

Check out Convert String to Uppercase in Python

Performance Considerations

In terms of performance, the // operator is slightly faster than the / operator because it doesn’t need to perform the extra step of creating a float object. However, the difference is usually negligible unless you’re performing millions of divisions in a performance-critical section of your code.

Read Data Science vs Machine Learning

Usage

So, when should you use the / operator, and when should you use the // operator? It depends on your specific needs:

  • If you need the exact result of the division, including any fractional part, use the / operator.
  • If you only need the whole number part of the division result, and you don’t care about the fractional part, use the // operator.

Summary

OperatorDescriptionExampleResult
/Performs normal division and returns a float10 / 33.3333333333333335
6 / 23.0
//Performs floor division and returns an integer10 // 33
6 // 23
Rounds down towards negative infinity for negatives-10 // 3-4

Check out Convert String to JSON in Python

Conclusion

In this tutorial, I have explained the difference between Python / and // operators. The / operator performs normal division and returns a float, while the // operator performs floor division. I showed differences with examples, and we saw how to handle negative numbers, performance considerations, where to use each operator, anda summary table.

You may also like to read:


51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.