Skip to content

Computer Science · Ch 5 — Getting Started with Python

Arithmetic Operators

5.8.1

Arithmetic Operators

Arithmetic operators let a Python program do calculation. Python supports the four basic arithmetic operations — addition, subtraction, multiplication and division — and adds three more: modulus (remainder), floor division and exponentiation. Two of these operators (+ and *) also have a second, string-related job.

The seven arithmetic operators

Addition: +

Adds the numeric values on either side of the operator.

>>> num1 = 5
>>> num2 = 6
>>> num1 + num2
11

The same operator also concatenates (joins) two strings when both operands are strings:

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

Subtraction: -

Subtracts the right operand from the left operand.

>>> num1 = 5
>>> num2 = 6
>>> num1 - num2
-1

Multiplication: *

Multiplies the values on both sides of the operator.

>>> num1 = 5
>>> num2 = 6
>>> num1 * num2
30

When the first operand is a string and the second an integer, * becomes the repetition operator — it repeats the string that many times:

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

Division: /

Divides the left operand by the right operand and returns the quotient. Note that the result is a float:

>>> num1 = 8
>>> num2 = 4
>>> num2 / num1
0.5

Modulus: %

Divides the left operand by the right operand and returns the remainder of the division:

>>> num1 = 13
>>> num2 = 5
>>> num1 % num2
3

Here 13 divided by 5 gives quotient 2 and remainder 3, so the result is 3.

Floor division: //

Divides the left operand by the right operand and returns the quotient with the decimal part removed. It is sometimes called integer division:

>>> num1 = 13
>>> num2 = 4
>>> num1 // num2
3
>>> num2 // num1
0

Ordinary division would give 13 / 4 = 3.25; floor division discards the fractional part and returns 3. In the second case, 4 // 13 is 0 because 4 divided by 13 gives a quotient less than 1.

Exponent: **

Performs exponential (power) calculation — it raises the left operand to the power of the right operand:

>>> num1 = 3
>>> num2 = 4
>>> num1 ** num2
81

That is, 3 raised to the power 4, which is 3 × 3 × 3 × 3 = 81.

Quick reference

OperatorOperationWhat it does
+AdditionAdds two numbers; joins (concatenates) two strings
-SubtractionSubtracts right operand from left
*MultiplicationMultiplies two numbers; repeats a string an integer number of times
Table 5.3Arithmetic Operators in Python
OperatorOperationDescriptionExample (Try in Lab)
+AdditionAdds the two numeric values on either side of the operator
This operator can also be used to concatenate two strings on either side of the operator
>>> num1 = 5
>>> num2 = 6
>>> num1 + num2
11
>>> str1 = "Hello"
>>> str2 = "India"
>>> str1 + str2
'HelloIndia'
-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 side of the operator
Repeats the item on left of the operator if first operand is a string and second operand is an integer value
>>> num1 = 5
>>> num2 = 6
>>> num1 * num2
30
>>> str1 = 'India'
>>> str1 * 2
'IndiaIndia'
/DivisionDivides the operand on the left by the operand on the right and returns the quotient>>> num1 = 8
>>> num2 = 4
>>> num2 / num1
0.5
%ModulusDivides the operand on the left by the operand on the right and returns the remainder>>> num1 = 13
>>> num2 = 5
>>> num1 % num2
3