Arithmetic operators are used to perform common mathematical operations.
| Operator | Name | Description | Example |
|---|---|---|---|
| + | Addition | Add two values together | a + b |
| - | Subtraction | Subtract one value from another | a - b |
| * | Multiplication | Multiplies two values | a * b |
| / | Division | Divides one value by another | a / b |
| % | Modulus | Returns the division remainder | a % b |
| ** | Exponentiation | Do exponential operation | a**b |
| // | Floor Division | Divides one value by another and return the result of an integer | a // b |
Assignment operators are used to assign a value to a variable.
| Operator | Example | Meaning |
|---|---|---|
| = | a = 5 | a = 5 |
| += | a += 5 | a = a + 5 |
| -= | a -= 5 | a = a - 5 |
| *= | a *= 5 | a = a * 5 |
| /= | a /= 5 | a = a / 5 |
| %= | a %= 5 | a = a % 5 |
| &= | a &= 5 | a = a & 5 |
| |= | a |= 5 | a = a | 5 |
| ^= | a ^= 5 | a = a ^ 5 |
| >>= | a >>= 5 | a = a >> 5 |
| <<= | a <<= 5 | a = a << 5 |
Comparison operators help us to compare two values to determine if the value is True or False.
These operators will return the value of the comparison in a boolean value.
| Operator | Name | Example |
|---|---|---|
| == | equal to | x == y |
| != | not equal to | x != y |
| > | greater than | x > y |
| < | less than | x < y |
| >= | greater than or equal to | x >= y |
| <= | less than or equal to | x <= y |
Logical operators are used to determine the logic between variables or values.
These operators also return a boolean value (True or False).
| Operator | Name | Description | Example |
|---|---|---|---|
| and | logical AND | returns true if all statements are true | x < 10 and x > 3 |
| or | logical OR | returns true if one of the statements is true | x > 100 or x < 30 |
| not | logical NOT | returns false if the statement is true and vice versa | not(x > 100 or x < 30) |
Identity operators are used to check if the objects are the same or not, with the same memory location or not.
| Operator | Description | Example |
|---|---|---|
| is | returns True if both variables are the same object | x is y |
| is not | returns True if both variables are not the same object | x is not y |
Membership operators are used to test if a variable is available in the sequence or not.
| Operator | Description | Example |
|---|---|---|
| in | returns True if a sequence with the specified value is present in the object | x in y |
| not in | returns True if a sequence with the specified value is not present in the object | x not in y |
Bitwise operators are operators that operate numbers on bit-level operation.
| Operator | Name | Example |
|---|---|---|
| & | bitwise AND | x & y |
| | | bitwise OR | x | y |
| ^ | bitwise XOR | x ^ y |
| << | bitwise left shift | x << y |
| >> | bitwise right shift | x >> y |
| ~ | bitwise NOT | ~x |