Math operations
Let's get on with the next step: Math
All the math operators that we know from Java are still here.
Here are some examples to get us started
# Addition:
sumz = 2+3
print('Sum=', sumz)
# Subtraction:
diff = 3-2
print('difference=', diff)
# Multiplication:
product = 3*2
print('product=', product)
# Division
quotient = 5/2
print('quotient=', quotient)
# exponent, 4 to the power of 2, 4 squared
result = 4**2
print(‘4 to the power of 2 is', result)
minMulti ply = -3**2 # ** takes precedence over the -, so we get nine
print('-3 to the power of 2 =', minMultiply)
minMultiply = (-3)**2 # this is the way to get exponent of a negative number
print('-3 to the power of 2 =', minMultiply)
The output will be as expected, and here it is for reference:
Sum= 5
difference= 1
product= 6
quotient= 2.5
4 to the power of 2 is 16
MinMultiply = -9
MinMultiply = 9
Except for the power of operator everything else is basic fare, nothing new or interesting. It's reassuring that nothing has changed in the world with respect to these.
Let's move on to Loops!
P.S: all the code used in this tutorial is available in github
No comments:
Post a Comment