Assignments

  • Python is a dynamically typed language
  • We do not have to declare the type of the variable when we initialise it
  • Type of variables are determined at run-time

We can basically do this:

n = 0
n = "abc"

We can also do multiple assignments at once

n, m = 0, "abc"

Incrementing in python is a little different

n = n + 1 # this works
n += 1 # this works
n++ # this does NOT work

Null in python is called None

x = None

Division

Division is decimal by default (compared to most languages where they will round towards zero)

i = 5 / 2 # 2.5
 
# to do rounding down
i = 5 // 2 # 2

Python always round down, so negative division is weird

i = -3 // 2 # this will be -2 instead of -1 for most langauges
 
# to overcome this:
i = int(-3 / 2) # -1

Modulo

Modulo is quite similar, but negative modulo has some weird outputs

10 % 3 # 1
 
-10 % 3 # 2

To be more consistent with other languages, we can import math and use the functions there

import math
math.fmod(-10, 3) # 1