Basic Math
Python has a bunch of built-in arithmetic operators. The table below provides a brief
comparison of the short notations and their longer equivalents.
Assume that initially the following is true: a = 3.
Finding the square root of a number is also easy in Python, but it’s not a built-in language
feature. You’ll find out about it later in the book!
You can calculate the absolute value of a digit (e.g. |-3|), using abs(). For example, abs(-
3) returns 3.
Operators precedence
The order of execution of mathematical operations in Python is similar to the order used
in conventional mathematics.
In maths there are three general operator priority levels:
1. exponents and roots
2. multiplication, division and modulus
3. addition and subtraction
Exponents and roots are represented by functions of a standard library in Python and are
covered further on in this book.
All other priority levels have their own built-in Python operators.
Higher order operations are performed before the lower ones. All the operations of the
same order are executed one by one from left to right.
For example, the statement: 2 + 3 * 4 - 1 returns 13. The multiplication is performed
before the addition and subtraction.
In order to affect the order of operator execution, you should use parentheses (). For
example, (3 + 6) * 2 equals 18 and 8 / (1 + 1) equals 4.
*********************
*Code Samples
The code snippet below shows how simple maths operations are processed in the Python
interpreter.
26
>>> 10 - 32
-22
>>> 11 % 10
1
>>> 6 ** 2
36
>>> 12 / 8
1.5
>>> 12 % 8 + (12 // 8) * 8
12
>>> 6 - -16
22
>>> a = 16
>>> a += 3
>>> a
19
>>> a = 4
>>> b = 6
>>> a *= b
>>> a
24
Exercises
PROBLEM: Try to answer the following questions without Python and then use the
interpreter to verify your answers:
1. What is the result of the following calculation: 2 + 2 + 2 + 2 + 2 + 2 * 0?
2. What about this one: (2 + 2 + 3 + 4) * (4 * 4 - (2 * 6 + 4))?
ANSWER: 10 - if you answered 0 you forgot that multiplication has a higher precedence
than addition
1. 0 - if you answer was something else you forgot that operations in brackets should
be performed first - a product of the second expression in brackets - (4 * 4 - (2 * 6
+ 4)) is 0 and anything multiplied by 0 is 0 as well
*********************
********************
Exercises
PROBLEM: A car has a speed of 100 km/h. Write some code that calculates the distance
in meters (not kilometers!) that the car would travel in N hours. Set N to 10.
ANSWER:
1. N = 10
2. distance_in_kilometers = 100 * N
3. distance_in_meters = distance_in_kilometers * 1000
*********************
********************
Exercises
PROBLEM: Rewrite the code using short notations
1. N = N * J
2. i = i + j * 18
3. f = f - k / 2
ANSWER:
1. N *= J
2. i += j * 18
3. f -= k / 2
*********************
********************
Exercises
PROBLEM: Given that f(x) = |x^3 - 20|, find f(-3). Your answer should consist of two lines
of code: one which initialises x to -3, and one that calculates the result.
ANSWER: The following Python code shall do the job:
>>> x = -3
>>> abs(x ** 3 - 20)
47
*********************
********************
Exercises
PROBLEM: Try to solve the previous problem using -3 inline, rather than using the x
variable.
ANSWER:
Your solution should look like this:
>>> abs((-3) ** 3 - 20)
47
If it looks like this:
>>> abs(-3 ** 3 - 20)
47
it is wrong because negation (i.e. subtraction from zero) has lower precedence than the
exponent and thus has to be enclosed in parentheses. Even though your solution gives
the same result as the correct one, it is a pure coincidence. Try to find value of x for f(x)
= |x^2 - 20| with and without parentheses around the negation to see the difference.
*********************
********************
Exercises
PROBLEM: How can you check if an integer is odd using Python?
ANSWER: Use the modulo operator to find the remainder when you divide by 2.
If the remainder is 1, the digit is odd.
>>> 3 % 2
1
>>> 4 % 2
0
*********************
Comments
Post a Comment