Python Operators
Python Operators are used to perform operations on values and variables. Several groups of operators exist in Python.
What are operators in Python?
Operators are special symbols that indicate that some kind of calculation is to be performed.
The values on which an operator acts are called operands.
For example
What operators exist in Python?
Python divides operators into the following groups:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
They are discussed individually, with examples, on the dedicated pages.
Arithmetic operators
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, etc.
Operator | Description | Example |
---|---|---|
+ | Add two operands or unary plus | x + y + 1 |
- | Subtract right operand from the left or unary minus | x - y - 1 |
* | Multiply two operands | x * y |
/ | Divide left operand by the right one (always results into float) | x / y |
% | Modulus: remainder of the division of left operand by the right | x % y (remainder of x/y ) |
// | Floor division: division that results into whole number adjusted to the left in the number line | x // y |
** | Exponent: left operand raised to the power of right | x**y (x to the power y) |
Assignment operators
Assignment operators are used to assign values to variables
Operator | Description | Example | Equivalent to |
---|---|---|---|
= | Assign value of right side of expression to left side operand | x = 2 | x = 2 |
+= | Add right-side operand with left side operand and then assign to left operand | x += 2 | x = x + 2 |
-= | Subtract right operand from left operand and then assign to left operand | x -= 2 | x = x - 2 |
*= | Multiply right operand with left operand and then assign to left operand | x *= 2 | x = x * 2 |
/= | Divide left operand with right operand and then assign to left operand | x /= 2 | x = x / 2 |
%= | Takes modulus using left and right operands and assign the result to left operand | x %= 2 | x = x % 2 |
//= | Divide left operand with right operand and then assign the value(floor) to left operand | x //= 2 | x = x // 2 |
**= | Calculate exponent(raise power) value using operands and assign value to left operand | x **= 2 | x = x ** 2 |
&= | Performs Bitwise AND on operands and assign value to left operand | x &= 2 | x = x & 2 |
&124;= | Performs Bitwise OR on operands and assign value to left operand | x &124;= 2 | x = x &124; 2 |
^= | Performs Bitwise XOR on operands and assign value to left operand | x ^= 2 | x = x ^ 2 |
>>= | Performs Bitwise right shift on operands and assign value to left operand | x >>= 2 | x = x >> 2 |
<<= | Performs Bitwise left shift on operands and assign value to left operand | x <<= 2 | x = x << 2 |
Comparison operators
Comparison operators are used to compare values.
They return either True
or False
according to the condition.
Operator | Meaning | Example |
---|---|---|
> | Greater than: True if left operand is greater than the right | x > y |
< | Less than: True if left operand is less than the right | x < y |
== | Equal to: True if both operands are equal | x == y |
!= | Not equal to: True if operands are not equal | x != y |
>= | Greater than or equal to: True if left operand is greater than or equal to the right | x >= y |
<= | Less than or equal to: True if left operand is less than or equal to the right | x <= y |
Logical operators
Logical operators are the and
, or
, not
operators.
They are used to combine conditional statements.
Operator | Meaning | Example |
---|---|---|
and | True if both the operands are true | x and y |
or | True if either of the operands is true | x or y |
not | True if operand is false (complements the operand) | not x |
Identity operators
Identity operators are used to check if two values are located on the same part of the memory.
Operator | Meaning | Example |
---|---|---|
is | True if the operands are identical (refer to the same object) | x is True |
is not | True if the operands are not identical (do not refer to the same object) | x is not True |
Membership operators
Membership operators are used to test whether a value or variable is in a sequence.
Operator | Meaning | Example |
---|---|---|
in | True if value/variable is found in the sequence | 2 in x |
not in | True if value/variable is not found in the sequence | 2 not in x |
Bitwise operators
Bitwise operators act on bits and perform the bit-by-bit operations.
These are used to operate on binary numbers.
Operator | Meaning | Example |
---|---|---|
& | Bitwise AND | x & y = 0 (0000 0000) |
&124; | Bitwise OR | x &124; y = 14 (0000 1110) |
~ | Bitwise NOT | ~x = -11 (1111 0101) |
^ | Bitwise XOR | x ^ y = 14 (0000 1110) |
>> | Bitwise right shift | x >> 2 = 2 (0000 0010) |
<< | Bitwise left shift | x << 2 = 40 (0010 1000) |