Lesson 7 has three parts A, B, C which can be completed in any order.
So far, we have performed math calculations using Python's operators
/ and the functions
min. In this lesson we will see some more operators and functions and learn how to perform more complex calculations.
We have already seen how to use operators for addition (
a + b), subtraction (
a - b), multiplication (
a * b) and division (
a / b). We will now learn about three additional operators.
- The power operator
a ** bcomputes
amultiplied by itself
btimes). For example,
2 ** 3produces
8(which is 2×2×2).
- The integer division operator
a // bcomputes the "quotient" of
band ignores the remainder. For example,
14 // 3produces
- The modulus operator
a % bcomputes the remainder when
ais divided by
b. For example,
14 % 3produces
The modulus operator is used for a variety of tasks. It can be used to answer questions like these ones:
- If the time now is 10 o'clock, what will be the time 100 hours from now? (requires modulus by 12)
- Will the year 2032 be a leap year? (requires modulus by 4, 100, and 400)
Checking leap years is an example of divisibility testing; in the next exercise we ask you to write a program that performs divisibility testing in general.
Python can compute most of the mathematical functions found on a scientific calculator.
sqrt(x)computes the square root of the number
log(x)are the exponential and natural logarithmic functions.
tan(x)and other trigonometric functions are available.
pi, the mathematical constant
3.1415..., is also included.
| When using Python's trigonometric functions, the angle |
Python includes such a large number of functions that they are organized into groups called modules. The above functions belong to the
math module. Before using any functions from a module, you must import the module as shown in the example below. To use a function from a module you must type the module name, followed by a period, followed by the name of the function.
Putting it all together
As you saw in the previous exercise, you can build mathematical expressions by combining operators. Python evaluates the operators using the same "order of operations" that we learn about in math class:
Brackets first, then Exponents, followed by Division and Multiplication, then finally Addition and Subtraction,
which we remember by the acronym "BEDMAS". Integer division and modulus fit into the "Division and Multiplication" category. For example, the expression
3 * (1 + 2) ** 2 % 4is evaluated by performing the addition in brackets (1+2 = 3), then the exponent (3 ** 2 = 9) , then the multiplication (3 * 9 = 27), and finally the modulus, producing a final result of 27 % 4 =
6 - 52 // 5 ** 2
Integer division with negative numbers: The expressions
a // b and
int(a / b) are the same when
b are positive. However, when
a is negative,
a // b uses "round towards negative infinity" and
int(a / b) uses "round towards zero."
Integers and Floating-Point Numbers
The result of a mathematical expression is a number. As we saw previously, each number is stored as one of the two possible types:
int type represents integers, both positive and negative, that can be as big as you want.
| Python does not accept numbers written in the form |
float type represents decimal numbers. Just as a simple calculator stores
1/3 as its approximate value
0.33333333, Python also stores decimal numbers as their approximate values.
| Because Python uses approximations of decimal numbers, certain equations which are mathematically true may not be true in Python.
For this reason, it is important to allow some tolerance for these approximations when comparing numbers of type |
We finish this lesson with some exercises.
Congratulations! After completing these exercises you are ready to move to another lesson.