Operators are used to perform operations on variables and values.
Arithmetic operators are used with numeric values to perform common mathematical operations.
Operator |
Description |
E.g |
+ (Addition) |
Used to add two operands. |
a = 20, b = 10, a + b = 30 |
- (Subtraction) |
Used to subtract two operands. |
a = 20, b = 10, a - b = 10 |
/ (divide) |
Returns the quotient between two operands. |
a = 20, b = 10, a / b = 2 |
* (Multiplication) |
Used to multiply one operand with the other. |
a = 20, b = 10, a * b = 200 |
% (reminder) |
Returns the reminder of the division between two operands. |
a = 20, b = 10, a % b = 0 |
** (Exponent) |
Calculates the first operand power to second operand. |
a = 2, b = 3, b ** a = 9 |
// (Floor division) |
It gives the floor value of the quotient produced by dividing the two operands. |
a = 10, b = 4, a // b = 2 |
Comparison operators are operators that compare values and return a Boolean value (true or false).
Operator |
Description |
E.g |
== |
If the value of two operands is equal. |
a = 2, b = 2, a == b, returns true |
!= |
If the value of two operands is not equal. |
a = 2, b = 5, a != b, returns true |
<= |
If the first operand is less than or equal to the second operand. |
a = 1, b = 10, a <= b, returns true |
>= |
If the first operand is greater than or equal to the second operand. |
a = 5, b = 5, a >= b, returns true |
> |
If the first operand is greater than the second operand. |
a = 1, b = 10, a < b, returns true |
< |
If the first operand is less than the second operand. |
a = 12, b = 5, a > b, returns true |
As the name says, assignment operators are used for assignment. This operator is used to assign a new value or to update a value from a variable.
Operator |
Description |
E.g |
= |
It assigns the value of the right expression to the left operand. |
a = 2, b = 4, a = b, a will be 4 |
+= |
Increments the value of the left operand by the value of the right operand. |
a = 2, b = 4, a += b, a will be 6 |
-= |
Decrements the value of the left operand by the value of the right. |
a = 2, b = 4, a -= b, a will be -2 |
*= |
Multiplies the value of the left operand by the value of the right operand. |
a = 2, b = 4, a *= b, a will be 8 |
/= |
Divides the value of the left operand by the value of the right operand. |
a = 2, b = 4, a /= b, a will be 0.5 |
%= |
Assigns the reminder of the division between the value of the left operand by the value of the right operand. |
a = 2, b = 4, a %= b, a will be 2 |
**= |
Powers the value of the left operand by the value of the right operand. |
a = 2, b = 4, a **= b, a will be 16 |
//= |
Calculates the floor division between the left operand and right operand. |
a = 2, b = 4, a //= b, a will be 0 |
Bitwise operators are operators that work in bit patterns or binary numerals. With bitwise operations, you can control the individual bits, and this can be useful when you want a fast and simple action that can be directly supported by the processor.
Operator |
Description |
E.g |
& |
Binary and operation between operands. |
a = 0111, b = 0011, a & b = 0011 |
| |
Binary or operation between operands. |
a = 0111, b = 0011, a | b = 0111 |
^ |
Binary xor operation between operands. |
a = 0111, b = 0011, a ^ b = 0100 |
~ |
Binary not operation of a binary number. |
a = 0111, ~a = 1000 |
<< |
Binary shift left operation of a binary number. |
a = 0111, a << 2 = 1100 |
>> |
Binary shift right operation of a binary number. |
a = 0111, a >> 2 = 0001 |
Logical operators are operators that combine multiple Boolean values and return a single Boolean value.
Operator |
Description |
E.g |
and |
If both the expressions are true, then the condition will be true. |
a = true, b = true, a and b = true |
or |
If one of the expressions is true, then the condition will be true. |
a = true, b = false, a or b = true |
not |
If an expression a is true, then not (a) will be false and vice versa. |
a = true, not a = false |
Identity operators are used to compare two objects. It does not compare if the two objects are equal, but instead, it compares if the two objects are the same or not, which returns a Boolean value.
Operator |
Description |
E.g |
is |
Returns true if the reference present at both sides point to the same object. |
a = 2, b = 2, a is b returns true |
is not |
Returns true if the reference present at both sides do not point to the same object. |
a = 2, b = 3, a is not b returns true |