Mathematical Operators for Programming

Division

a, b, c, d, e = 3, 2, 2.0, -3, 10
a/b # = 1
a/c # = 1.5
d/b # = -2
b/a # = 0
d/e # = -1

Addition

a, b = 1, 2
a + b # = 3

Exponentiation

a, b = 2, 3
(a ** b) # = 8
pow(a,b) # = 8

Trigonometric Functions

a, b = 1, 2
import math
math.sin(a) # returns the sine of 'a' in radians
math.cosh(b) # returns the inverse hyperbolic cosine of 'b' in radians
math.atan(math.pi) # returns the arc tangent of 'pi' in radians
math.hypot(a, b) # returns the Euclidean norm

Inplace Operations

a += 1 # means a = a + 1
a *= 2 # means a = a * 2

Subtraction

a, b = 1, 2
b - a # = 1

Multiplication

a, b = 2, 3
a * b # = 6

Modulus

(3 % 4) # = 3
(10 % 2) # = 0

Previous||Next Chapter

One comment

Leave a Reply