Basic Math Operations

We will take a brief look at some basic math operators and their usage:

  • + addition
  • - subtraction
  • * multiplication
  • ** exponent
  • / division
  • // floor division (integer division) - divides and rounds the answer down to the nearest whole number
  • % modulo - returns the remainder of the division
  • abs(x) returns the absolute (positive) value of x
  • += 'Increment by' operator

Examples:

print(7.0 / 2)              # 3.5
print(3 * 3.75 / 1.5)       # 7.5
print((3 + 5) * 2 * 3)      # 48  
print((3 + 4) * (5**2 - 5)) # 140
print(3 ** 2)                 # 9
print(18.5 // 3)            # 6.0
print(13 % 3)               # 1

Learn with CodeCraft visuals

Addition, subtraction, multiplication:

# basic math operations 
t, s = 5, 2

# columns use the math results as the heights:
column_m(2, -20, t, 2)           # blue
column_m(4, -20, s, 7)           # light blue

column_m(6, -20, (t + s), 8)     # lime green
column_m(8, -20, (t - s), 12)    # purple
column_m(10, -20, (t * s), 11)   # pink

Division: orange

t/s might return a floating point result. To use it as coordinate in CodeCraft, we can use type conversion to change the float number into integer.

int(t/s) returns the whole number part:

# orange column with height int(t/s)
column_m(12, -20, int(t / s), 10)      # orange

Modulo: red

%, returns the remainder of the division, red block at at x = 5%2 = 1, y=3, z=-20

# red column shows the modulo result 5%2=1
column_m(14, -20, t % s, 13)

Floor division: green

# green column shows the floor division, 5//2=2
column_m(16, -20, t // s, 6)

Exponent

The blue columns in the next picture represent the base number, and the diamond blocks above the column represent the power it is raised to. The red column behind the column indicates the result of the power calculation.

# 5**2
column_m(2, -20, t, 39)     # base number t, blue

for j in range(s):
    block_m(2, (t + 2 + j * 2), -20, 3)    # diamond blocks show exponent s

column_m(2, -22, t**s, 50)   # 5**2, the red column has height 5**2=25

# try 2**5  
column_m(8, -20, s, 39)    # base number s = 2, blue

for j in range(t):
    block_m(8, (2 * j + s + 2), -20, 3)     # diamond blocks show exponent t

column_m(8, -22, s**t, 'wool_red')   # 2**5 the red column has height 2**5 = 32

left: 5**2; right: 2**5

See abs() at work

x1, x2, x3, x4 = -2,-4,-5, -1

# blue blocks at x1 ... x4, negative locations
block_m(x1, 2, -20, 62)
block_m(x2, 3, -20, 62)
block_m(x3, 4, -20, 62)
block_m(x4, 6, -20, 62)

# red blocks at positive side abs(x1)...abs(x4)
block_m(abs(x1), 2, -20, 73)
block_m(abs(x2), 3, -20, 73)
block_m(abs(x3), 4, -20, 73)
block_m(abs(x4), 6, -20, 73)

Red blocks on the positive side of the x-axis mirror the blue blue blocks on the negative side:

'Increment by' operator +=

These operators are used very often because they are faster to type:

x += 1 means x = x + 1
x -= 2 means x = x - 2
x *= 3 means x = x * 3
x /= 2 means x = x / 2

Note: some other languages such as C and Java use ++ to increment a variable by 1. Python does NOT support that.

#'Increment by' operator '+='

h, s = 10, 4

# column s, blue
column_m(1, -20, s, 44 )

# original column h, lime
column_m(3, -20, h, 45)

h += s     # now h should have value 14, let's see:

# new column t, red
column_m(4, -20, h, 50)

Yes! We got a red column with height h that has a new value of 14. It's not the same as the original lime green column with a height of h = 10.

You can test the effects of *=, -= yourself.

Results from /= operations might need a type conversion to turn a float into an integer.

results matching ""

    No results matching ""