Python arithmetic operators

Python Arithmetic Operators

In Python, numbers are the most commonly used data type. Python uses the same notation for basic arithmetic operators: + for addition, - for subtraction, * for multiplication (most programming languages use * instead of x in math/algebra), and / for division (÷ in math).

Python also defines several other arithmetic operators: “%” (modulo operator), ** (exponential operator), and // (floor division operator).

Arithmetic operators are binary operators; they operate on two operands. Python fully supports mixed arithmetic operations. That is, the two operands can be of different numeric types. In this case, Python widens the narrower operand to the wider type. Integer objects are narrower than floating-point objects, which are narrower than complex objects. Therefore, arithmetic operations between int and float result in a float. A float and complex operation result in a complex number, and similarly, operations between integer and complex objects result in a complex number.

Let’s learn these operators through examples.

Python Addition Operator (+)

This operator, pronounced “plus,” is a basic arithmetic operator. It adds two numeric operands and returns the result.

In the following example, two integer variables are used as operands for the “+” operator.

a=10
b=20
print ("Addition of two integers")
print ("a =",a,"b =",b,"addition =",a+b)

This will produce the following output −

Addition of two integers
a = 10 b = 20 addition = 30

Adding an integer and a floating-point number produces a floating-point number.

a=10
b=20.5
print ("Addition of integer and float")
print ("a =",a,"b =",b,"addition =",a+b)

This will produce the following output −

Addition of integer and float
a = 10 b = 20.5 addition = 30.5

The result of adding a floating-point number to a complex number is a complex number.

a=10+5j
b=20.5
print ("Addition of complex and float")
print ("a=",a,"b=",b,"addition=",a+b)

This will produce the following output . −

Addition of complex and float
a= (10+5j) b= 20.5 addition= (30.5+5j)

Python Subtraction Operator (-)

This operator, called the minus operator, subtracts the second operand from the first. If the second operand is larger, the result is a negative number.

The first example demonstrates subtracting two integers.

a=10
b=20
print ("Subtraction of two integers:")
print ("a =",a,"b =",b,"a-b =",a-b)
print ("a =",a,"b =",b,"b-a =",b-a)

Result –

Subtraction of two integers
a = 10 b = 20 a-b = -10
a = 10 b = 20 b-a = 10

Subtraction of integers and floating-point numbers follows the same principles.

a=10
b=20.5
print ("subtraction of integer and float")
print ("a=",a,"b=",b,"a-b=",a-b)
print ("a=",a,"b=",b,"b-a=",b-a)

This will produce the following output −

subtraction of integer and float
a= 10 b= 20.5 a-b= -10.5
a= 10 b= 20.5 b-a= 10.5

In subtraction involving complex and floating-point numbers, the real part is included in the operation.

a=10+5j
b=20.5
print ("subtraction of complex and float")
print ("a=",a,"b=",b,"a-b=",a-b)
print ("a=",a,"b=",b,"b-a=",b-a)

This will produce the following output

subtraction of complex and float
a= (10+5j) b= 20.5 a-b= (-10.5+5j)
a= (10+5j) b= 20.5 b-a= (10.5-5j)

Python Multiplication Operator (*)

In

a=10
b=20
print ("Multiplication of two integers")
print ("a =",a,"b =",b,"a*b =",a*b)

This will produce the following output: Output

Multiplication of two integers
a = 10 b = 20 a*b = 200

In multiplication, floating-point operands can use standard decimal notation or scientific notation.

a=10
b=20.5
print ("Multiplication of integer and float")
print ("a=",a,"b=",b,"a*b=",a*b)
a=-5.55
b=6.75E-3
print ("Multiplication of float and float")
print ("a =",a,"b =",b,"a*b =",a*b)

It will produce the following output

Multiplication of integer and float
a = 10 b = 20.5 a-b = -10.5
Multiplication of float and float
a = -5.55 b = 0.00675 a*b = -0.0374624999999999996

For a multiplication operation involving a complex operand, the other operand is multiplied by both its real and imaginary parts.

a=10+5j
b=20.5
print ("Multiplication of complex and float")
print ("a =",a,"b =",b,"a*b =",a*b)

This will produce the following output −

Multiplication of complex and float
a = (10+5j) b = 20.5 a*b = (205+102.5j)

Python Division Operator (/)

/ symbol is commonly known as slash. The result of the division operator is the dividend (left operand) divided by the divisor (right operand). If either operand is negative, the result is negative. Because infinity cannot be stored in memory, Python raises a ZeroDivisionError if the divisor is 0.

In Python, the result of the division operator is always a floating-point number, even if both operands are integers.

a=10
b=20
print ("Division of two integers")
print ("a=",a,"b=",b,"a/b=",a/b)
print ("a=",a,"b=",b,"b/a=",b/a)

The following is the output produced: Output

Division of two integers
a= 10 b= 20 a/b= 0.5
a= 10 b= 20 b/a= 2.0

In division, floating-point operands can use standard decimal notation or scientific notation.

a=10
b=-20.5
print ("Division of integer and float")
print ("a=",a,"b=",b,"a/b=",a/b)
a=-2.50
b=1.25E2
print ("Division of float and float")
print ("a=",a,"b=",b,"a/b=",a/b)

It will produce the following output

Division of integer and float
a= 10 b= -20.5 a/b= -0.4878048780487805
Division of float and float
a= -2.5 b= 125.0 a/b= -0.02

When one of the operands is complex, a division operation is performed between the other operand and the two halves of a complex (real and imaginary) object.

a=7.5+7.5j
b=2.5
print ("Division of complex and float")
print ("a =",a,"b =",b,"a/b =",a/b)
print ("a =",a,"b =",b,"b/a =",b/a)

This will produce the following output –

Division of complex and float
a = (7.5+7.5j) b = 2.5 a/b = (3+3j)
a = (7.5+7.5j) b = 2.5 b/a = (0.166666666666666666 - 0.166666666666666666j)

If the numerator is 0, the result of the division is always 0, unless the denominator is 0, in which case Python throws a ZeroDivisionError with the error message “Division by Zero.”

a=0
b=2.5
print ("a=",a,"b=",b,"a/b=",a/b)
print ("a=",a,"b=",b,"b/a=",b/a)

It will produce the following output

a= 0 b= 2.5 a/b= 0.0
Traceback (most recent call last):
  File "C:Usersmlathexamplesexample.py", line 20, in <module>
     print ("a=",a,"b=",b,"b/a=",b/a)
                                 ~^~
ZeroDivisionError: float division by zero

Python modulo operator (%)

If both operands are integers, the result of the modulo operation is also an integer. If the numerator is exactly divisible by the denominator, the remainder is 0. If the numerator is less than the denominator, the modulus is equal to the numerator. If the denominator is 0, Python throws a ZeroDivisionError.

a=10
b=2
print ("a=",a, "b=",b, "a%b=", a%b)
a=10
b=4
print ("a=",a, "b=",b, "a%b=", a%b)
print ("a=",a, "b=",b, "b%a=", b%a)
a=0
b=10
print ("a=",a, "b=",b, "a%b=", a%b)
print ("a=", a, "b=", b, "b%a=",b%a)

It will produce the following output

a= 10 b= 2 a%b= 0
a= 10 b= 4 a%b= 2
a= 10 b= 4 b%a= 4
a= 0 b= 10 a%b= 0
Traceback (most recent call last):
File "C:Usersmlathexamplesexample.py", line 13, in <module>
print ("a=", a, "b=", b, "b%a=", b%a)
~^~
ZeroDivisionError: integer modulo by zero

If either operand is a floating-point type, the modulo value will always be a floating-point type.

a=10
b=2.5
print ("a=",a, "b=",b, "a%b=", a%b)
a=10
b=1.5
print ("a=",a, "b=",b, "a%b=", a%b)
a=7.7
b=2.5
print ("a=",a, "b=",b, "a%b=", a%b)
a=12.4
b=3
print ("a=",a, "b=",b, "a%b=", a%b)

It produces the following output

a= 10 b= 2.5 a%b= 0.0
a= 10 b= 1.5 a%b= 1.0
a= 7.7 b= 2.5 a%b= 0.20000000000000018
a= 12.4 b= 3 a%b= 0.40000000000000036

Python does not accept complex numbers as operands in modulo operations. It throws a TypeError: unsupported operand type(s) for %.

Python Exponentiation Operator (**)

Python uses the ** (double asterisk) as the exponentiation operator (sometimes called the power operator). So, for a**b, you can say a raised to the bth power, or even a raised to the bth power.

If both operands in an exponential expression are integers, the result is also an integer. If either operand is a floating-point number, the result is a floating-point number. Likewise, if either operand is complex, the exponentiation operator returns a complex number.

If the base is 0, the result is 0; if the exponent is 0, the result is always 1.

a=10
b=2
print ("a=",a, "b=",b, "a**b=", a**b)
a=10
b=1.5
print ("a=",a, "b=",b, "a**b=", a**b)
a=7.7
b=2
print ("a=",a, "b=",b, "a**b=", a**b)
a=1+2j
b=4
print ("a=",a, "b=",b, "a**b=", a**b)
a=12.4
b=0
print ("a=",a, "b=",b, "a**b=", a**b)
print ("a=",a, "b=",b, "b**a=", b**a)

It will produce the following output

a= 10 b= 2 a**b= 100
a= 10 b= 1.5 a**b= 31.622776601683793
a= 7.7 b= 2 a**b= 59.290000000000006
a= (1+2j) b= 4 a**b= (-7-24j)
a= 12.4 b= 0 a**b= 1.0
a= 12.4 b= 0 b**a= 0.0

Python Floor Division (//)

Floor division is also known as integer division. Python uses // (double slash) notation to represent it. Unlike the modulo operator, which returns the remainder, floor division returns the quotient.

If both operands are positive, floor division returns a number with the decimal portion removed. For example, floor division of 9.8 divided by 2 returns 4 (the pure division is 4.9, and after removing the decimal portion, the result is 4).

However, if one operand is negative, the result is rounded towards zero (towards negative infinity). Floor division of -9.8 divided by 2 returns 5 (the pure division is -4.9, and after rounding towards 0, the result is 5).

a=9
b=2
print ("a=",a, "b=",b, "a//b=", a//b)
a=9
b=-2
print ("a=",a, "b=",b, "a//b=", a//b)
a=10
b=1.5
print ("a=",a, "b=",b, "a//b=", a//b)
a=-10
b=1.5
print ("a=",a, "b=",b, "a//b=", a//b)

It will produce the following output

a= 9 b= 2 a//b= 4
a= 9 b= -2 a//b= -5
a=10 b= 1.5 a//b= 6.0
a= -10 b= 1.5 a//b= -7.0

Python Complex Number Arithmetic

Arithmetic operators behave slightly differently when both operands are complex objects.

Addition and subtraction of complex numbers are simple addition/subtraction of the real and imaginary parts, respectively.

a=2.5+3.4j
b=-3+1.0j
print ("Addition of complex numbers - a=",a, "b=",b, "a+b=", a+b)
print ("Subtraction of complex numbers - a=",a, "b=",b, "a-b=", a-b)

The following will be output: output

Addition of complex numbers - a= (2.5+3.4j) b= (-3+1j) a+b= (-0.5+4.4j)
Subtraction of complex numbers - a= (2.5+3.4j) b= (-3+1j) a-b =
(5.5+2.4j)

Multiplying complex numbers is similar to multiplying two binomials in algebra. If “a+bj” and “x+yj” are two complex numbers, their multiplication is given by the following formula −

(a+bj)*(x+yj) = ax+ayj+xbj+byj² = (ax-by)+(ay+xb)j

For example,

a=6+4j
b=3+2j
c=a*b
c=(18-8)+(12+12)j
c=10+24j

The following program confirms the result −

a=6+4j
b=3+2j
print ("Multplication of complex numbers - a=",a, "b=",b, "a*b=", a*b)

To understand how division works between two complex numbers, we should use the conjugate of the complex number. Python complex number objects have a conjugate() method that returns a complex number with the opposite sign of the imaginary part.

>>> a=5+6j
>>> a.conjugate()
(5-6j)

When dividing two complex numbers, multiply both the numerator and denominator by the conjugate of the denominator.

a=6+4j
b=3+2j
c=a/b
c=(6+4j)/(3+2j)
c=(6+4j)*(3-2j)/3+2j)*(3-2j)
c=(18-12j+12j+8)/(9-6j+6j+4)
c=26/13
c=2+0j

To verify, run the following code −

a=6+4j
b=3+2j
print ("Division of complex numbers - a=",a, "b=",b, "a/b=", a/b)

The Python complex number class does not support the modulo operator (%) or the floor division operator (//).

Leave a Reply

Your email address will not be published. Required fields are marked *