print(5 + 8 + 3)16
We can use Python as a calculator.
print(5 + 8 + 3)16
Note that the extra spaces are added to make the code more readable. 5 + 8 + 3 works just as well as 5+8+3. And it is considered good style. Use the extra spaces in all your Notebooks.
Below are all the possible arithmetic operations you can do:
# Sum
print(5 + 2)7
# Subtraction
print(5 - 2)3
# Multiplication
print(5 * 2)10
# Division
print(5 / 2)2.5
# Exponentiation
print(5 ** 2)25
# Quotient
print(5 // 2)2
# Remainder
print(5 % 2)1
You store 10K euros in a bank account. Compute the compound interest after 10 years at a rate of 5%.
print(10000 * (1 + 0.05) ** 10)16288.94626777442
The order of operations in Python is the same as in arithmetic. First, you compute parentheses, then exponents, then you multiply and divide (from left to right), and finally, you add and subtract (from left to right).
Convert 32 degrees Celsius into Farenheit.
\[(F - 32) \cdot 5/9 = C\]
print(32 * 9 / 5 + 32)89.6
Fix this code to compute a square root.
print(4 ** 1/2)The code runs, but it computes \((4^1)/2\) because exponentiation happens before division. You need parentheses around the exponent.
print(4 ** (1/2))2.0