Operators in general are used to perform operations on values and variables in Python.
These are standard symbols used for the purpose of logical and arithmetic operations.
Arithmetic operators are used with numeric values to perform common mathematical 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 |
Comparison operators are used to compare two values:
Operator | Name | Example |
---|---|---|
== | Equal | a==b |
!= | Not Equal | a!=b |
< | Less than | a<b |
> | Greater than | a>b |
<= | Less than or equal to | a<=b |
>= | Greater than or equal to | a>=b |
Assignment operators are used to assign values to variables:
Operator | Example | As same as |
---|---|---|
= | a = 10 | a = 10 |
+= | a += 10 | a = a+10 |
-= | a -= 10 | a = a-10 |
*= | a *= 10 | a = a*10 |
/= | a /= 10 | a = a/10 |
%= | a %= 10 | a = a%10 |
**= | a **= 10 | a = a**10 |
//= | a //= 10 | a = a//10 |
&= | a &= 10 | a = a&10 |
|= | a |= 10 | a = a|10 |
^= | a ^= 10 | a = a^10 |
>>= | a >>= 10 | a = a>>10 |
<<= | a <<= 10 | a = a<<10 |
Logical operators are used to combine conditional statements:
Operator | Description | Example |
---|---|---|
and | Returns True if both statements are true | a > 5 |
or | Returns True if one of the statements is true | a > 10 |
not | Reverse the result, returns False if the result is true |
|
The unary structure implies character, restoring the same value as its operand. The unary structure implies negate, restoring the nullified incentive as its operand: zero to zero, positive to negative, and negative to positive.
Unary positive also known as plus and unary negative also known as minus are unique operators.
The plus and minus when used with a constant value represent the concept that the values are either positive or negative.
print (-3 + +5)
Bitwise operators are used to compare (binary) numbers:
Operator | Name | Description |
---|---|---|
& | AND | Sets each bit to 1 if both bits are 1 |
| | OR | Sets each bit to 1 if one of two bits is 1 |
^ | XOR | Sets each bit to 1 if only one of two bits is 1 |
~ | NOT | Inverts all the bits |
<< | Zero fill left shift | Shift left by pushing zeros in from the right and let the leftmost bits fall off |
>> | Signed right shift | Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
Membership operators are used to test if a sequence is presented in an object:
Operator | Description | Example |
---|---|---|
in | Returns True if a sequence with the specified value is present in the object | a in b |
not in | Returns True if a sequence with the specified value is not present in the object | a not in b |
Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location:
Operator | Description | Example |
---|---|---|
is | Returns True if both variables are the same object | a is b |
is not | Returns True if both variables are not the same object | a is not b |