VOOZH about

URL: https://www.geeksforgeeks.org/python/division-operators-in-python/

⇱ Division Operators in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Division Operators in Python

Last Updated : 3 Apr, 2026

In Python, division operators allow you to divide two numbers and return the quotient. But unlike some other languages (like C++ or Java), Python provides two different division operators, each behaving slightly differently.

Types of Division Operators

1. Float Division (/)

This operator always returns a floating-point result. The output is equivalent to standard mathematical division, regardless of whether the operands are integers or floating-point numbers.

Example: Below code shows how '/' always returns a float result.


Output
1.0
4.5
-5.0
10.0

Even though 5 5 equals 1 mathematically, Python returns 1.0 because the / operator always produces a floating-point result.

2. Floor Division (//)

The // operator performs floor division. It divides the numbers and returns the largest integer less than or equal to the result. If any operand is a float, the result will also be a float.

Example 1: This code demonstrates floor division with positive integers.


Output
1
1
3

Example 2: This code shows how // behaves with negative numbers.


Output
2
-3

Why -3 instead of -2?

Because Python always floors the result.

  • -5 / 2 = -2.5
  • Floor of -2.5 = -3

Division with Floats

When one of the numbers is a float, the result can also be a float, even with floor division.

Example: This code shows how floor division works with float numbers.


Output
3.0
-4.0

Real-World Examples

Example 1: This code splits students into groups and shows leftover students.


Output
Full groups: 3
Remaining students: 2

Example 2: This code converts total seconds into minutes and the remaining seconds using floor division (//) and modulus (%) operators.


Output
2 minutes and 10 seconds

Division and Boolean Values

In Python, booleans (True, False) behave like integers (1, 0) in arithmetic. But division is not directly defined for booleans.

Example: This code shows basic arithmetic operations with booleans.


Output
2
5
0

However, True / False would raise a ZeroDivisionError, and True / True works but is not meaningful (1.0).

Operator Overloading with Division

The division operator / can be overloaded by defining the special method __truediv__() inside a class. This allows you to control how division behaves for objects of that class.


Output
False

Explanation:

  • __truediv__() overloads the / operator.
  • When a / b is executed, it performs self.value and other.value.
  • True and False evaluates to False.
  • A new MyClass object is returned with value False.
  • print(c.value) outputs False.
Comment
Article Tags:
Article Tags: