Day 12: Do It Like The Greeks – Basic Arithmetic Operations In Python

 

Today just some samples of basic operation and their output on the shell.

 

print(type(1))      # class "int"
print(type(2.45))   # class "float"

#ADDITION
print(2 + 3.5)      # 5.5 <-- int gets converted to float auto


number = 5
number += 4         # 9 <-- number =  number + 4

print(number)

print(.4+.01)       # 0.4100000000003 <-- float will have some rounding error

#SUBSTRACTION
print(2 - 3.5)      # -1.5

#MULTIPLICATION
print(6 * 2)        # 12
print(6 * .25)      # 1.5
print(4.5 * 2.3)    # 10.35

#DIVISION
print(12 / 4)       # 3
print(3 / 4)        # 0.75 <-- output is a float
print(3 // 6)       # 0 integer division

#MODULO
print(4 % 3)        # 1
print(3 % 2)        # 1 <-- odd number
print(6 % 3)        # 0 <-- even number

#POWER
print(4 ** 2)       # 16
print(4 ** 6)       # 4096

#ORDER OF OPERATION
print(3 + 6 * 2)    # 15 <-- * and / before + and -
print((3 + 6) * 2)  # 18 <-- paranthesises come before * and /
print(((3 / 2) + 6 * .5) * 2)   # 9.0