Skip to content

Informatics Practices · Ch 3 — Brief Overview of Python

Arithmetic Operators

3.6.1

Arithmetic Operators

Arithmetic operators are the workhorses of everyday computation. Python supports operators for the four basic arithmetic operations — addition, subtraction, multiplication and division — and adds three more that programmers use constantly: modular division (remainder), floor division and exponentiation. That gives seven arithmetic operators in all.

The seven arithmetic operators

OperatorOperationWhat it does
+AdditionAdds the two numeric values on either side of the operator
-SubtractionSubtracts the operand on the right from the operand on the left
*MultiplicationMultiplies the two values on both sides of the operator
/DivisionDivides the operand on the left by the operand on the right and returns the quotient
%ModulusDivides the operand on the left by the operand on the right and returns the remainder
//Floor divisionDivides the operand on the left by the operand on the right and returns the quotient after removing the decimal part; also called integer division
**ExponentRaises the base to the power of the exponent — the base is multiplied by itself as many times as the exponent says

A few of these deserve a closer look, because students often confuse them:

  • / (division) gives the full quotient, decimal part included. Dividing 5 by 2 with / gives 2.5.
  • // (floor division) performs the same division but throws away the decimal part, keeping only the whole-number quotient — 5 divided by 2 with // gives 2. This is why it is sometimes called integer division. The order of operands matters: 2 floor-divided by 5 gives 0.
  • % (modulus) is the companion of floor division: instead of the quotient, it hands back the remainder. 13 divided by 5 leaves a remainder of 3, so 13 % 5 is 3.
  • ** (exponent) computes powers: 3 ** 4 means multiply 3 by itself 4 times, which is 81.

The + and * operators work on strings too

Two arithmetic operators have a second life with strings.

The + operator, when placed between two strings, does not add — it concatenates, joining the string on the right onto the end of the string on the left:

>>> str1 = "Hello"
>>> str2 = "India"
>>> str1 + str2
'HelloIndia'

The * operator repeats the item on its left when the first operand is a string and the second operand is an integer. The string is written out that many times, end to end:

>>> str1 = 'India'
>>> str1 * 2
'IndiaIndia'
Note

This dual behaviour is not limited to strings. The + (concatenation) and * (repetition) operators work in the same manner for the other sequence data types as well, such as lists and tuples. …

Table 3.3Arithmetic operators in Python
OperatorOperationDescriptionExample (Try in Lab)
+AdditionAdds two numeric values on either side of the operator>>> num1 = 5
>>> num2 = 6
>>> num1 + num2
11
-SubtractionSubtracts the operand on the right from the operand on the left>>> num1 = 5
>>> num2 = 6
>>> num1 - num2
-1
*MultiplicationMultiplies the two values on both sides of the operator>>> num1 = 5
>>> num2 = 6
>>> num1 * num2
30
/DivisionDivides the operand on the left by the operand on the right of the operator and returns the quotient>>> num1 = 5
>>> num2 = 2
>>> num1 / num2
2.5
%ModulusDivides the operand on the left by the operand on the right and returns the remainder>>> num1 = 13
>>> num2 = 5
>>> num1 % num2
3