Floor Division in Python is performed using the // (double forward slash) operator. Unlike regular division (/), floor division always rounds down the result to the nearest whole number (integer), regardless of whether the numbers are positive or negative.
The modulo operator % in Python is closely related to floor division. The identity a = (a // b) * b + (a % b) always holds true, which is Python's equivalent of Euclid's Division Lemma.
The // operator divides the left number by the right number and then floors the result โ meaning it rounds down to the nearest integer.
# Regular division
print(7 / 2) # Output: 3.5
# Floor division
print(7 // 2) # Output: 3 (rounds DOWN from 3.5)
print(10 // 3) # Output: 3 (10/3 = 3.33... โ rounds to 3)
print(15 // 4) # Output: 3 (15/4 = 3.75 โ rounds to 3)
This is where floor division behaves differently from simple truncation. For negative numbers, 'rounding down' means going further away from zero (toward more negative values).
print(-7 // 2) # Output: -4 (NOT -3!)
# -7/2 = -3.5 โ floor rounds DOWN to -4
print(7 // -2) # Output: -4
# 7/-2 = -3.5 โ floor rounds DOWN to -4
print(-7 // -2) # Output: 3
# -7/-2 = +3.5 โ floor rounds DOWN to 3
`/` is true division โ it always returns a `float` (e.g., 7/2 = 3.5). `//` is floor division โ it returns the largest integer โค the true result (e.g., 7//2 = 3). If both operands are integers, `//` returns an int; if either is a float, it returns a float.
What is the Full Form of CRT?
Discover the full form of CRT in computer hardware. Learn how the heavy, bulky Cathode Ray Tube monitors worked before flat LCD screens existed.
Difference Between Data and Information
Understand the key difference between Data (raw, unprocessed facts) and Information (processed, organized, and meaningful data).
Deadlock Prevention vs Deadlock Avoidance in OS
Learn the difference between deadlock prevention and deadlock avoidance in Operating Systems. Understand Banker's Algorithm and the four Coffman conditions.
What is Verbal Communication? (Types and Examples)
Learn the definition of Verbal Communication. Understand the difference between Oral and Written communication with advantages and daily examples.
Difference Between Compiler and Interpreter
Learn the key differences between a Compiler and an Interpreter. Understand how these language translators process code differently with examples.
Turn this guide into revision flashcards, a practice exam, or an AI-generated podcast โ free, no signup required.