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:

    python
    result = number1 // number2
    
    • number1 and number2 are the numbers you want to divide.
    • result will store the rounded down quotient.

Examples and Exercises:

  1. Floor Division of Integers:

    python
    a = 14 b = 5 result = a // b
    print('Quotient:', result)  # Output: Quotient: 2 
  2. Floor Division with Negative Numbers:

    • Let's see what happens with negative numbers.
    python
    a = -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.
  3. Floor Division of Floating Point Numbers:

    • You can also use // with numbers that have decimals.
    python
    a = 14.8 b = 5.1 result = a // b
    print('Quotient:', result)  # Output: Quotient: 2.0 

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!