Operators help determine the interactions between elements in a Python statement. For example, when you write
2 + 3
, it means to add (using the +
operator) the value 2
to the value 3
for a sum of 5
. It's important to know which operators Python supports, and remembering them all is not always easy. The following table provides a quick summary.
Operator | Type | Description | Example |
− | Arithmetic | Subtracts the right operand from left hand operand. | 5 − 2 = 3 |
− | Unary | Negates the original value so that positive becomes negative and vice versa. | −(−4) results in 4 while −4 results in −4 |
−= | Assignment | Subtracts the value found in the right operand from the value found in the left operand and places the result in the left operand. | MyVar -= 2 results in MyVar containing 3 |
!= | Relational | Determines whether two values are not equal. Some older versions of Python would allow you to use the <> operator in place of the != operator. Using the <> operator results in an error in current versions of Python. | 1 != 2 is True |
% | Arithmetic | Divides the left operand by the right operand and returns the remainder. | 5 % 2 = 1 |
%= | Assignment | Divides the value found in the left operand by the value found in the right operand and places the remainder in the left operand. | MyVar %= 2 results in MyVar containing 1 |
& (And) | Bitwise | Determines whether both individual bits within two operators are true and sets the resulting bit to true when they are. | 0b1100 & 0b0110 = 0b0100 |
* | Arithmetic | Multiplies the right operand by the left operand. | 5 * 2 = 10 |
** | Arithmetic | Calculates the exponential value of the right operand by the left operand. | 5 ** 2 = 25 |
**= | Assignment | Determines the exponential value found in the left operand when raised to the power of the value found in the right operand and places the result in the left operand. | MyVar ** 2 results in MyVar containing 25 |
*= | Assignment | Multiplies the value found in the right operand by the value found in the left operand and places the result in the left operand. | MyVar *= 2 results in MyVar containing 10 |
/ | Arithmetic | Divides the left operand by the right operand. | 5 / 2 = 2.5 |
// | Arithmetic | Performs integer division, where the left operand is divided by the right operand and only the whole number is returned (also called floor division). | 5 // 2 = 2 |
//= | Assignment | Divides the value found in the left operand by the value found in the right operand and places the integer (whole number) result in the left operand. | MyVar //= 2 results in MyVar containing 2 |
/= | Assignment | Divides the value found in the left operand by the value found in the right operand and places the result in the left operand. | MyVar /= 2 results in MyVar containing 2.5 |
^ (Exclusive or) | Bitwise | Determines whether just one of the individual bits within two operators are true and sets the resulting bit to true when they are. When both bits are true or both bits are false, the result is false. | 0b1100 ^ 0b0110 = 0b1010 |
| (Or) | Bitwise | Determines whether either of the individual bits within two operators are true and sets the resulting bit to true when they are. | 0b1100 | 0b0110 = 0b1110 |
~ | Unary | Inverts the bits in a number so that all the 0 bits become 1 bits and vice versa. | ~4 results in a value of −5 |
~ (One's complement) | Bitwise | Calculates the one's complement value a number.
|
~0b1100 = −0b1101 ~0b0110 = −0b0111 |
+ | Arithmetic | Adds two values together. | 5 + 2 = 7 |
+ | Unary | Provided purely for the sake of completeness. This operator returns the same value that you provide as input. | +4 results in a value of 4 |
+= | Assignment | Adds the value found in the right operand to the value found in the left operand and places the result in the left operand. | MyVar += 2 results in MyVar containing 7 |
< | Relational | Verifies that the left operand value is less than the right operand value. | 1 < 2 is True |
<< (Left shift) | Bitwise | Shifts the bits in the left operand left by the value of the right operand. All new bits are set to 0 and all bits that flow off the end are lost. | 0b00110011 << 2 = 0b11001100 |
<= | Relational | Verifies that the left operand value is less than or equal to the right operand value. | 1 <= 2 is True |
= | Assignment | Assigns the value found in the right operand to the left operand. | MyVar = 2 results in MyVar containing 2 |
== | Relational | Determines whether two values are equal. Notice that the relational operator uses two equals signs. A mistake many developers make is using just one equals sign, which results in one value being assigned to another. | 1 == 2 is False |
> | Relational | Verifies that the left operand value is greater than the right operand value. | 1 > 2 is False |
>= | Relational | Verifies that the left operand value is greater than or equal to the right operand value. | 1 >= 2 is False |
>> (Right shift) | Bitwise | Shifts the bits in the left operand right by the value of the right operand. All new bits are set to 0 and all bits that flow off the end are lost. | 0b00110011 >> 2 = 0b00001100 |
and | Logical | Determines whether both operands are true. | True and True is True True and False is False False and True is False False and False is False |
in | Membership | Determines whether the value in the left operand appears in the sequence found in the right operand. | "Hello" in "Hello Goodbye" is True |
is | Identity | Evaluates to true when the type of the value or expression in the right operand points to the same type in the left operand. | type(2) is int is True |
is not | Identity | Evaluates to true when the type of the value or expression in the right operand points to a different type than the value or expression in the left operand. | type(2) is not int is False |
not | Logical | Negates the truth value of a single operand. A true value becomes false and a false value becomes true. | not True is False not False is True |
not in | Membership | Determines whether the value in the left operand is missing from the sequence found in the right operand. | "Hello" not in "Hello Goodbye" is False |
or | Logical | Determines when one of two operands are true. | True or True is True True or False is True False or True is True False or False is False |