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

The division operator (/) in Python performs division of 2 numbers. The numbers can be of the type integer, floating point or complex. 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.

Division of 2 Integer numbers

In below code the division operator is used to perform division of 2 integers, the result is a floating point value.

a=10
b=2
c=a/b
print(c)        #5.0
print(type(c))  #float

Division of 2 Floating Point numbers

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

a=10.5
b=2.5
c=a/b
print(c)        #4.2
print(type(c))  #float

Division of an integer number and a floating point number

In below code the division operator is used to perform division of an integer number (dividend is int) and a floating point number (divisor is float), the result is a floating point value.

a=18
b=2.5
c=a/b
print(c)        #7.2
print(type(c))  #float

In below code the division operator is used to perform division of a floating point number (dividend is float) and an integer number (divisor is int), the result is a floating point value.

a=22.5
b=2
c=a/b
print(c)        #11.25
print(type(c))  #float

Division of 2 complex numbers

In below code the division operator is used to perform division of 2 complex numbers, the result too is a complex number.

a = 7 + 12j
b = 5 + 10j
c = a / b
print(c)         #1.24-0.08j
print(type(c))   #complex

Division of an integer number and a complex number

In below code the division operator is used to perform division of an integer number (dividend is int) and a complex number (divisor is complex), the result is a complex number.

a = 10
b = 5 + 10j
c = a / b
print(c)         #0.4-0.8j
print(type(c))   #complex

In below code the division operator is used to perform division of a complex number (dividend is complex) and an integer number (divisor is int), the result is a complex number.

a = 5+10j
b = 10
c = a / b
print(c)         #0.5+1j
print(type(c))   #complex

Division of a floating point number and a complex number

In below code the division operator is used to perform division of a floating point number (dividend is float) and a complex number (divisor is complex), the result is a complex number.

a = 1.5
b = 5 + 10j
c = a / b
print(c)         #0.06-0.12j
print(type(c))   #complex

In below code the division operator is used to perform division of a complex number (dividend is complex) and a floating point number (divisor is float), the result is a complex number.

a = 5+10j
b = 2.5
c = a / b
print(c)         #2+4j
print(type(c))   #complex

Division of 2 string values

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

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

Division of 2 boolean values

With boolean variables, the division operator works as in integer values, treating True as integer value 1 and False as integer value 0. The result is a float. However, note that division will result in a ZeroDivisionError in case the divisor value is False(0).

t=True      #treated as integer value 1
f=False     #treated as integer value 0
#c=t/f      #ZeroDivisionError
#d=f/f      #ZeroDivisionError
c=f/t
print(c)    #0.0
d=t/t
print(d)    #1.0

Similar Operators in Python

Last modified: April 10, 2023

Author