Python Operators
Here’s a comprehensive table of Python operators, along with examples and explanations for each type:
Arithmetic Operators
Operator | Name | Example | Explanation |
---|
+ | Addition | 5 + 3 | Adds two numbers |
- | Subtraction | 5 - 3 | Subtracts second number from first |
* | Multiplication | 5 * 3 | Multiplies two numbers |
/ | Division | 5 / 3 | Divides first number by second |
// | Floor Division | 5 // 3 | Divides and returns the integer part |
% | Modulus | 5 % 3 | Returns the remainder of division |
** | Exponentiation | 5 ** 3 | Raises first number to the power of second |
Assignment Operators
Operator | Name | Example | Explanation |
---|
= | Assignment | x = 5 | Assigns value to variable |
+= | Addition Assignment | x += 3 | Adds and assigns |
-= | Subtraction Assignment | x -= 3 | Subtracts and assigns |
*= | Multiplication Assignment | x *= 3 | Multiplies and assigns |
/= | Division Assignment | x /= 3 | Divides and assigns |
//= | Floor Division Assignment | x //= 3 | Floor divides and assigns |
%= | Modulus Assignment | x %= 3 | Modulus and assigns |
**= | Exponentiation Assignment | x **= 3 | Exponentiates and assigns |
Comparison Operators
Operator | Name | Example | Explanation |
---|
== | Equal to | 5 == 3 | Checks if two values are equal |
!= | Not equal to | 5 != 3 | Checks if two values are not equal |
> | Greater than | 5 > 3 | Checks if first value is greater |
< | Less than | 5 < 3 | Checks if first value is lesser |
>= | Greater than or equal to | 5 >= 3 | Checks if first value is greater or equal |
<= | Less than or equal to | 5 <= 3 | Checks if first value is lesser or equal |
Logical Operators
Operator | Name | Example | Explanation |
---|
and | Logical AND | True and False | Returns True if both are True |
or | Logical OR | True or False | Returns True if one is True |
not | Logical NOT | not True | Returns the opposite boolean value |
Bitwise Operators
Operator | Name | Example | Explanation |
---|
& | AND | 5 & 3 | Bitwise AND |
` | ` | OR | `5 |
^ | XOR | 5 ^ 3 | Bitwise XOR |
~ | NOT | ~5 | Bitwise NOT |
<< | Left Shift | 5 << 1 | Shifts bits left |
>> | Right Shift | 5 >> 1 | Shifts bits right |
Identity Operators
Operator | Name | Example | Explanation |
---|
is | Identity | x is y | Checks if both variables are same object |
is not | Not Identity | x is not y | Checks if both variables are not same object |
Membership Operators
Operator | Name | Example | Explanation |
---|
in | Membership | 'a' in 'apple' | Checks if value is in sequence |
not in | Not Membership | 'b' not in 'apple' | Checks if value is not in sequence |
Comments
Post a Comment