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 Print Preview Shortcut Key?
Learn the keyboard shortcut key for Print Preview in Windows, Microsoft Word, Excel, and Web browsers like Chrome.
Find and Replace Shortcut Key (Windows & Mac)
Learn the keyboard shortcut for Find and Replace in MS Word, Excel, and Notepad. Use Ctrl+H for Windows and Cmd+Shift+H for Mac.
What is the 'static' Keyword in Java?
Learn about the static keyword in Java. Understand how static variables, methods, and blocks are used for memory management at the class level.
Basic Structure of a C Program
Learn the basic structure of a C program. Understand the use of preprocessor directives (#include), the main() function, and curly braces { }.
Difference Between System Software and Application Software
Learn the exact difference between System Software and Application Software. Understand how Operating Systems run the hardware, while Apps help the user.
Turn this guide into revision flashcards, a practice exam, or an AI-generated podcast โ free, no signup required.