7 - Arithmetic-Operators
7 - Arithmetic-Operators
Recap
In the previous video, we wrote this piece of code, which prints out the sum
of 5 and 3.
The "+" in the code is called an arithmetic operator, and it is one of many
operators in python.
Arithmetic Operators
Operators
As you might've noticed, addition and subtraction are just like normal math operators.
Operators
For multiplication, we use the "*" operator which is called an asterisk, and for division,
we use the "/" operator, which is called a forward slash.
Operations Ordering
Note that the order of operations in python, is the same as in math.
Python follows a rule called PEMDAS, which you can know more about here if
you are not familiar with
Example:
Note that adding parentheses to the first code changed the value of the
mathematical operation, so always take care about your calculations.
Exponent Operator
Note:
** is different from ^, make sure not to get confused and use ^ instead of **
The ^ operator is called the caret operator, which is used to perform bitwise XOR.
This is something a little advanced for now, but if you want, you can read about
that here.
Modulus Operator
Pro Tip:
To determine whether a number is even or odd, you can get the modulo of two for
this number, if it is equal to one, then this number is odd, otherwise, it is even.
Have a look on the previous snippet of code.
Integer Division Operator
The // operator is used to get the integer floor division (aka integer division) of two
numbers. It rounds down the division result, and ignores the floating points.
Notice the different between the first example and the second one.
Integer Division Operator
The // operator is used to get the integer floor division (aka integer division) of two
numbers. It basically get the integer division and ignores the floating points.