Python Operators
Python offers a range of operators to carry out operations on variables and values.
Here's a list of different types of Python operators:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Identity operators
- Membership operators
Arithmetic Operators
Arithmetic Operators are employed to do arithmetic operations.
| Operator | Name | Example |
|---|---|---|
| + | Addition | a+b |
| - | Subtraction | a-b |
| * | Multiplication | a*b |
| / | Division | a/b |
| % | Modulus | a%b |
| ** | Exponentiation | a**b |
| // | Floor division | a//b |
Assignment Operators
Assignment operators are employed in assigning values to variables.
| Operator | Example |
|---|---|
| = | a = 5; |
| += | a += 5; |
| -= | a -= 3; |
| *= | a *= 2; |
| /= | a /= 2; |
| %= | a %= 2; |
| //= | a //= 2; |
Comparison Operators
Comparison operators are employed to compare two values.
| Operator | Name | Example |
|---|---|---|
| == | Equal | a==b |
| != | Not equal | a!=b |
| > | Greater than | a>b |
| >= | Greater than or equal to | a>=b |
| << /td> | Less than | a<>b |
| <=< /td> | Less than or equal to | a<=b< /td> |
Logical Operators
Logical operators carry out logical operations and produce a Boolean value.
| Operator | Name | Example |
|---|---|---|
| && | AND | x && y |
| || | OR | x || y |
| ! | NOT | !x |
Bitwise Operators
| Operator | Name | Example |
|---|---|---|
| & | Bitwise AND | a & b |
| | | Bitwise OR | a | b |
| ~ | Bitwise NOT | ~a |
| <<< /td> | Left shift | b<<< /td> |
Membership operators
| Operator | Description | Example |
|---|---|---|
| in | Returns True if a is present in given sequence | a in b |
| not in | Returns True if a is not present in given sequence | a not in b |
Identity operators
| Operator | Description | Example |
|---|---|---|
| is | Returns True if a and b are same | a is b |
| is not | Returns True if a and b are not same | a is not b |
Operator Precedence in Python
| Name | Operator |
|---|---|
| Parenthesis | () |
| Exponential | ** |
| Multiply, divide, modulus, floor division | *, /, %, // |
| Addition, subtraction | +, - |
| Left shift and right shift operators | <<,>> |
| Bitwise and | & |
| Bitwise or and xor | ^, | |
| Comparison operators | <,>, >=, <=< /td> |
| Assignment operators | =, %=, /=, //=, -=, +=, *= , **= |
| Logical operators | and, or, not |