This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

Operators

We now try to combine the ‘atoms’ (variables, literals, etc.) that we have seen into expressions using different kinds of operators.

Arithmetic operators

The arithmetic operators in Python are quite straightforward:

  • + (addition)
  • - (subtraction)
  • * (multiplication)
  • / (division)
  • // (floor division, or integer part of division)
  • % (modulo, or remainder after division)
  • ** (exponential - raised to the power)

You can use any object as operands (literals, variables, etc.).

Make sure you understand what each operator does.

>>> 7 + 2
>>> 7 - 2 
>>> 7 * 2
>>> 7 / 2      # 3.5
>>> 7 // 2     # 3
>>> 7 % 2      # 0.5
>>> 7 ** 2     # 7 squared = 49

You can chain operations, e.g. 7 + 2 * 3. It largely obeys a standard operator precedence and evaluation order as in Mathematics (exponentiation, multiplication/division, addition/subtraction; and left to right if equal precedence). You can use parenthesis (7 + 2) * 3 if you wish to be more explicit.

Type Casting

Implicit type casting or type conversion will occur if the operands are of different (but compatible) types.

>>> x = 2
>>> type(x)
>>> y = 3.0
>>> type(y)
>>> z = x + y
>>> type(z)

Like mentioned in the pre-sessional video, you cannot combine incompatible types.

>>> x = 2
>>> y = "3"
>>> x + y           # No! This does not work!
>>> x + True        # This works though. True is implicitly converted to 1

You will need to perform explicit type casting to manually convert a value to a compatible type.

>>> x = 2
>>> y = "3"
>>> x + int(y) 

Comparison operators

Again, a straightforward list:

  • > (greater than)
  • < (less than)
  • == (equal to. Remember: two equal signs!)
  • != (not equal to)
  • >= (greater than or equal to)
  • <= (less than or equal to)

Again, operands can be any object.

All comparisons return either True or False (boolean expressions).

>>> x = 7 
>>> x >= 5
>>> 5 != x
>>> x == x
>>> 5 + 6 > 3 * x

IMPORTANT!!!

What happens when you do the following?

>>> print(1.1 + 2.2 == 3.3)

Why? Try investigating it!

>>> print(1.1 + 2.2) 
>>> print(3.3) 

Do you now see why they are not equal? It is hard for computers to store decimal fractions on the computer (think \(\frac{1}{3} = 0.3333333333333333333....\)). So floating points are often just an approximation.

So how do we check for ‘equality’ between two floating point numbers? Easy! As long as the two numbers are “close enough”, then we can say they are equal. So if the difference between the two numbers is “small enough”, then we consider them equal.

>>> epsilon = 0.000000001
>>> print(abs((1.1 + 2.2) - 3.3) < epsilon)

abs(x) gives you the absolute value of x.

Logical operators

not, and, or

>>> not True
>>> not False
>>> True and False
>>> False and False
>>> True or False
>>> True or True

You can combining the logical operators with comparison operators to form complex boolean expressions.

>>> x = 7
>>> y = 2
>>> x == 7
>>> x > 3 and y < 1
>>> not x >= 5 and x < 3      # make sure you understand the 
>>> not (x >= 5 and x < 3)    # difference between these two
>>> not 5 + y > 3 * x or 4 == x + 3

Identity operators (Advanced topic: optional)

The identity operators (is, is not) check whether two operands refer to the same object. Again, this results in a boolean expression (True or False).

>>> x = complex(2, 4)
>>> y = complex(2, 4)
>>> id(x)
>>> id(y)
>>> x is y
>>> x is not y

So, in a nutshell, x is y checks whether id(x) == id(y).

If x is y is True, then it implies that x == y is also True.

However, the reverse is not true. Just because two objects are equal does not necessarily mean they refer to the same object.

Try this out.

>>> x = 9000
>>> y = 9000
>>> print(x == y)     # True 
>>> print(x is y)     # False

By default, every time you create a new object, Python will allocate a different space in memory for each new object. However, for optimisation, Python treats small numbers and short strings as being the same objects. Try the above with x = 30 and y = 30 !