Floor Division
What Is the Floor Division Operator?
- The floor division operator
//in Python divides two numbers and rounds down the result to the nearest whole number.
How to Use It:
The basic way to use the floor division operator is:
pythonresult = number1 // number2number1andnumber2are the numbers you want to divide.resultwill store the rounded down quotient.
Examples and Exercises:
Floor Division of Integers:
pythona = 14 b = 5 result = a // b print('Quotient:', result) # Output: Quotient: 2Floor Division with Negative Numbers:
- Let's see what happens with negative numbers.
pythona = -11 b = 5 result = a // b print('Quotient:', result) # Output: Quotient: -3- Here, -11/5 is -2.2. The floor of -2.2 is -3.
Floor Division of Floating Point Numbers:
- You can also use
//with numbers that have decimals.
pythona = 14.8 b = 5.1 result = a // b print('Quotient:', result) # Output: Quotient: 2.0- You can also use
Summary:
- The floor division operator
//in Python helps you divide two numbers and round down the result to the nearest whole number. It works with both integers and floating point numbers. Try the examples to see how it works!