As we've been learning Python, I've referenced operators, and even shown a few in examples. Before we move on, I thought it best to list all of Python's operators, not just make reference to some of the mathematical operators.
The following operators are all used to calclate numeric values in some or other form.
|+||Plus||Adds two numbers together.||1 + 2 gives 3|
Adds two numbers together.
1 + 2 gives 3
Subtracts one number from another.
3 - 1 gives 2
Multiplies one number by another.
2 * 3 gives 6
Divides one number by another. Integers are rounded down to the nearest integer. Add a float to force a floating result.
6 / 3 gives 2
4 / 3 gives 1
4.0 / 3 gives 1.333
Divide a number by another and return a quotient rounded down to the nearest integer.
4.0 // 3.0 gives 1.0
Return the remainder of a division.
5 % 2 gives 1
Raise a number to the power of another number.
2 ** 4 gives 16
(2 * 2 * 2 * 2)
Boolean operators are used to compare two values and result in either a "True" or a "False".
Check if the first value is greater than the second.
10 > 5 gives True
4 > 7 gives False
Check if the first value is less than the second.
8 < 6 gives True
9 < 3 gives False
Greater Than Or Equal To
Check if the first value is equal to or greater than the second.
7 >= 7 gives True
8 >= 7 gives True
6 >= 7 gives False
Less Than Or Equal To
Check if the first value is equal to or less than the second.
5 <= 5 gives True
5 <= 6 gives True
5 <= 4 gives False
Check that the two values are equal.
'Car' == 'Car' gives True
'Car' == 'Bus' gives False
Not Equal To
Check that the two values are different.
'Car' != 'Bus' gives True
'Car' != 'Car' gives False
Return the opposite of an expression.
not True gives False
not False gives True
The result of two expressions needs to be True for everything to be True.
'Car' == 'Car' and 5 == 5 gives True
'Car' == 'Bus' and 5 == 5 gives False
The result of either of the two expressions needs to be True for everything to be True.
'Car' == 'Car' or 5 == 6 gives True
'Car' == 'Bus' or 5 == 5 gives True
'Car' == 'Bus' or 5 == 6 gives False
Bitwise operators are used to manipulate the bits in a byte. If you don't understand this just yet, don't worry about it.
|<<||Left Shift||Shift bits left by a certain number of positions.||0b0010 << 2 gives 0b1000|
|>>||Right Shift||Shift bits right by a certain number of positions.||0b1000 >> 2 gives 0b0010|
|&||Bitwise AND||Perform a bitwise AND operation on two bytes.||0b1001 & 0b0011 gives 0b0001|
||||Bitwise OR||Perform a bitwise OR operation on two bytes.||0b1001 | 0b0011 gives 0b1011|
|^||Bitwise XOR||Perform a bitwise XOR operation on two bytes.||0b1001 ^ 0b0011 gives 0b1010|
|~||Bitwise invert||Perform a bitwise inversion (twos complement) on a byte.||~0b1001 gives -0b0110|
When confronted with an expression such as 1 + 2 * 3 - 4 it is difficult to know how to evaluate it. Just as in normal mathematics, when dealing with operators in Python, or any other programming language for that matter, there is an order of precedence when dealing with operators.
In the table below the operators are listed in order of precendence. The operators at the top of the table will be executed before the ones further down.
*, /, %
Multiplication, Division, Modulo
<, <=, >, >=, !=, ==
is, is not
Identity tests (checking the type of object)
is, is not
Membership tests (checking items in lists, tuples and dictionaries)
If you want to force the order of evaluation (when expressions are evaluated), use brackets to group them, like (4 + 1) * 2.
Update: Part 5