Arithmetic & String Operators

code guest
Python Journey
Published in
3 min readAug 15, 2019

Introducing two kinds of operators — arithmetic and string operators. You probably know the arithmetic computations using operators like + — x / from math lessons. Take note of operations that are marked by 🐈 — these are more peculiar to Python.

  • addition: 10 + 10 gives us 20
  • 🐈 string concatenation (or linking together): “Romeo” + “Juliet” gives us “RomeoJuliet”
  • subtraction: 10 — 10 gives us 0
  • multiplication: 10 * 10 gives us 100
  • 🐈 string repetition (whether string * integer or integer * string doesn’t matter): “ha” * 4 gives us “hahahaha”
  • exponential: 10 ** 10 gives us 10000000000
  • division: 10 / 10 gives us 1
  • 🐈 Integer division (Integer division rounds down the results of division to the nearest integer — also note the operator is double /): 10 // 3 gives us 3
  • 🐈 modulo (calculates the remainder from division): 10 % 3 gives us 1

Notes about the application of these operators in our programs:

There are many use cases for string concatenation from combining first name and last name to even generating reports using python.

Integer division is very useful is cases where we just want the division result without accompany fractions — eg. maximum amount of an item we can buy within our budget.

Modulo has its own use cases too — such as differentiating between even and odd numbers — since even numbers % 2 gives us 0 while odd numbers % 2 gives us 1. More applications of Modulo can be found on wikipedia.

The asterisk operator * is used in multiplication, exponential calculation and string repetition!

Order of operations

Let us apply what you have learnt above to calculating the total amount including compound interest on an investment of principal sum $10,000 at 3.0% annual interest rate compounded half-yearly for five years.

Note: compound periods are the number of times in a year that interest is added to total amount. For example, if the compound period is monthly, 1/12 of the annual interest rate times principal is added to the total amount every month.

The compound interest formula looks like this: principal * (1 + annualInterestRate / compoundPeriods) ^ (compoundPeriods * timeInYears)

Converting the formula to python and fitting our scenario values in, we get

totalAmount = 10000 * (1 + 0.03/2)**(2*5)

Similar to what we learnt in school during math lessons, in Python we

  • evaluate brackets first
  • followed by exponential
  • and then multiplication/division/modulo from left to right (like in 2 * 3 % 4, we start from left, calculating 2*3 first then % 4)
  • and finally addition/subtraction from left to right
# After evaluating the brackets we get:
totalAmount = 10000 * 1.015 ** 10

Key point to note:
The concept of left to right is important because multiplication, division and modulo share the same order of operation.

If we do not follow the left to right rule, we will end up making incorrect calculations. For example, if we have multiplication followed by modulo in 2 * 3 % 4, doing 3 % 4 first would lead to a different set of results from doing 2 * 4 first.

Further reading

Summary

  • The seven arithmetic operators are +, -, *, /, **, //, %
  • The two string operators are + for string concatenation and * for string repetition
  • Order of operations — brackets first → exponential → multiplication / division / modulo → addition / subtraction last

Quiz

Which of the following options are incorrect?

a) 10 // 3 * 4 gives us 4

b) (2*1.5)**3*2 gives us 54.0

c) 5 % 2.5 gives us 2

d) “I laughed so much” + “ “ + 2 * “ha” * 3 gives us “I laughed so much hahahahahaha”

Quiz explanation

a) // and * share the same order of operation, so we do 10 // 3 first followed by * 4

b) We first evaluate brackets (2*1.5) → 3.0 and then exponential 3.0 ** 3 → 27.0 and finally 27.0 * 2

c) 5 divides nicely by 2.5 which doesn’t leave any remainder

d) Option d involves both string repetition and string concatenation. The order of multiplication in 2 * “ha” * 3 doesn’t matter. We can do “haha” * 3 or 2 * “hahaha”. Either way, we get “hahahahahaha”. Next, “I laughed so much” is concatenated with whitespace character “ “ and “hahahahahaha”.

quiz answers: a & c

--

--