Python has a variety of operators to perform various arithmetic operations like addition, subtraction, multiplication, division, modulus and exponentiation. Here we will discuss the floor division operator in Python with some code snippets

The floor division operator (//) in Python performs division of 2 numbers. The floor division performs division and returns a value by rounding down to nearest integer. The numbers can be of the type integer or floating point. The first operand is called the dividend and the second operand is called the divisor. Let’s see some examples and understand the division operator.

Floor Division of 2 Integer numbers

In below code the floor division operator is used to perform division of 2 integers, the result is an integer value.

a=15
b=4
c=a//b
print(c)        #3
print(type(c))  #int

Floor Division of a Floating Point number and a Integer

In below code the floor division operator is used to perform division of a floating point number and an integer, the result is the a floating point value.

a=15.5
b=4
c=a//b
print(c)        #3.0
print(type(c))  #float

Floor Division of a Integer and a Floating point number

In below code the floor division operator is used to perform division of an integer number and a floating point number, the result is a floating point value.

a=15
b=5.5
c=a//b   
print(c)        #2.0
print(type(c))  #float

Floor Division of 2 Floating Point numbers

In below code the floor division operator is used to perform division of 2 floating point numbers, the result is a floating point number.

a=15.5
b=4.5
c=a//b
print(c)        #3.0
print(type(c))  #float

Floor Division using complex numbers

If floor division is used along with complex numbers, the operation throws a TypeError (unsupported operand type). The floor division operator cannot be used with complex values as operands. The following errors may be observed if any one operand is of type complex.

  • TypeError: unsupported operand type(s) for /: ‘complex’ and ‘int’
  • TypeError: unsupported operand type(s) for /: ‘complex’ and ‘float’
  • TypeError: unsupported operand type(s) for /: ‘int’ and ‘complex’
  • TypeError: unsupported operand type(s) for /: ‘float’ and ‘complex’

Similar Operators in Python

Last modified: July 19, 2023

Author