Python Operators
Operators are used to manipulating variables and values.
Example :- We use the +
operator in the following example to add two values :
print(10 + 5)
Output :-
Python categorizes operators as follows :
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Identity operators
- Membership operators
- Bitwise operators
Python Arithmetic Operators
With numerical values, arithmetic operators are employed for common mathematical activities :
Operator | Name | Example | Result |
---|---|---|---|
+ | Addition | 5 + 10 | Total of 5 and 10 |
- | Subtraction | 5 - 10 | Minus of 5 and 10 |
* | Multiplication | 5 * 10 | multiplying of 5 and 10 |
/ | Division | 5 / 10 | Dividing of 5 and 10 |
% | Modulus | 5 % 10 | balance of 5 and 10 |
** | Exponentiation | 5 ** 10 | End of 5 and 10 |
// | Floor division | 5 // 10 | floor division of 5 and 10 |
Python Assignment Operators
To assign the values to the variables, assignment operators are also used :
Operator | Example | same as.... |
---|---|---|
= | a = 5 | a = 5 |
+= | a += 3 | a = a + 3 |
-= | a -= 3 | a = a - 3 |
*= | a *= 3 | a = a * 3 |
/= | a /= 3 | a = a / 3 |
%= | a %= 3 | a = a % 3 |
//= | a //= 3 | a = a // 3 |
**= | a **= 3 | a = a ** 3 |
&= | a &= 3 | a = a & 3 |
|= | a |= 3 | a = a | 3 |
^= | a ^= 3 | a = a ^ 3 |
>>= | a >>= 3 | a = a >> 3 |
<<= | a <<= 3 | a = a << 3 |
Python Comparison Operators
Two values are used to make comparisons operators of comparison :
Operator | Name | Example |
---|---|---|
== | Equal | a == b |
!= | Not equal | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
Related Links
Python Logical Operators
To merge conditional formatting, logical operators are employed :
Operator | Description | Example |
---|---|---|
and | Returns True if both statements are true | x < 5 and x < 10 |
or | Returns True if one of the statements is true | x < 5 or x < 4 |
not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |
Python Identity Operators
Used to comparing objects are the identity operators, not if they are equal, but if they are 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 |
Related Links
Python Membership Operators
Membership operators shall be used to test whether an object shows a sequence :
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 |
Python Bitwise Operators
Bitwise are used to compare (binary) numbers by operators :
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 |