Python Academy: The Stress Free Way To Learning Python Inside & Out (2014)
Chapter 4. Operators
The values of your operands are manipulated by operators. There are seven types of operators used in the Python programming language. The following tables display these operators and provide brief explanations regarding their function.
Arithmetic Operators
Operator |
Description |
Addition ( + ) |
It adds the values. |
Subtraction ( - ) |
It subtracts the second operand from the previous operand. |
Multiplication ( * ) |
It multiples the values. |
Division ( / ) |
It divides the first operand by the second operand. |
Modulus ( % ) |
It divides the first operand by the second operand and returns the remainder |
Exponent ( ** ) |
It performs exponential calculation on the operators. |
Floor Division ( // ) |
It divides the operands but eliminates the decimal points after the result. |
Comparison Operators or Relational Operators
Operator |
Description |
== |
If the values of both operands are equal, the condition is true. |
!= |
If the values of both operands are not equal, the condition is true. |
<> |
If the values of both operands are not equal, the condition is true. |
> |
If the value of the left operand is bigger than the value of the right operand, the condition is true. |
< |
If the value of the left operand is less than the value of the right operand, the condition is true. |
>= |
If the value of the left operand is bigger or equal to the value of the right operand, the condition is true. |
<= |
If the value of the left operand is less than or equal to the value of the right operand, the condition is true. |
Assignment Operators
Operator |
Description |
= |
It assigns values from the right operand to the left operand. |
+= add AND |
It adds the right operand to the left operand, and then allocates the result to the left operand. |
-= subtract AND |
It subtracts the right operand from the left operand, and then allocates the result to the left operand. |
*= multiply AND |
It multiples the left operand and the right operand, and then allocates the result to the left operand. |
/= divide AND |
It divides the left operand with the right operand, and then allocates the result to the left operand. |
%= modulus AND |
It uses the two operands to find the modulus, and then allocates the result to the left operand. |
**= exponent AND |
It performs exponential computation on the operators and then assigns the value to the left operand. |
//= floor division |
It performs floor division on the operators and assigns the value to the left operand. |
Bitwise Operators
Operator |
Description |
& binary AND |
It copies the bit if it is present in both operands. |
| binary OR |
It copies the bit if it is present in either operand. |
^ binary XOR |
It copies the bit if it is present in one operand, but not both. |
~ binary ones complement |
It flips bits. |
<< binary left shift |
It moves the value of the left operand towards the left based on the number of bits assigned by the right operand. |
>> binary right shift |
It moves the value of the left operand towards the right based on the number of bits assigned by the right operand. |
Logical Operators
Operator |
Description |
And logical AND |
The condition is true if both operands are true. |
Or logical OR |
The condition is true if an operand is non-zero. |
Not logical NOT |
It reverses the logical state of the operand. |
Membership Operators
Operator |
Description |
Is |
If the variables on either side of the operator point toward the same object, it evaluates to true. Otherwise, it evaluates to false. |
Not in |
If it does not find a variable in a particular sequence, it evaluates to true. Otherwise, it evaluates to false. |
Identity Operators
Operator |
Description |
Is |
If the variables on either side of the operator point towards the same object, it evaluates to true. Otherwise, it evaluates to false. |
Is not |
If the variables on either side of the operator point towards the same object, it evaluates to false. Otherwise, it evaluates to true. |