PYTHON OPERATORS
Operator is a symbol that performs certain operations. Python provides the following set of operators
1. Arithmetic Operators
2. Logical operators
3. Relational Operators or Comparison Operators
4. Bitwise operators
5. Assignment operators
6. Special operators
PYTHON ARITHMETIC OPERATORS
Arithmetic operators are used with numeric values to perform common mathematical operations
a=5
b=3
Operator |
Name |
Example |
output |
+ |
Addition |
a+ b |
8 |
- |
Subtraction |
a - b |
2 |
* |
Multiplication |
a * b |
15 |
/ |
Division |
a / b |
a = 12 b = 3 4.0 |
% |
Modulus |
a % b |
a = 5 b = 2 1 |
** |
Exponentiation |
a ** b |
a = 2 b = 5 32 |
// |
Floor division |
a // b |
a = 15 b = 2 7 |
Note: #the floor division // rounds the result down to the nearest whole number.
PYTHON ASSIGNMENT OPERATORS
Assignment operators are used to assign values to variables:
Example:
>>>a = 5
>>>print(a)
Output: 5
Operator |
Example |
Same As |
output |
= |
a = 5 |
a = 5 |
5 |
+= |
a += 3 |
a = a + 3 |
8 |
-= |
a -= 3 |
a = a - 3 |
2 |
*= |
a *= 3 |
a = a * 3 |
15 |
/= |
a /= 3 |
a= a / 3 |
1.6666666666666667 |
%= |
a %= 3 |
a = a % 3 |
2 |
//= |
a //= 3 |
a = a // 3 |
1 |
**= |
a **= 3 |
a = a ** 3 |
125 |
&= |
a &= 3 |
a = a & 3 |
1 |
|= |
a |= 3 |
a = a | 3 |
7 |
^= |
a ^= 3 |
a = a ^ 3 |
6 |
>>= |
a >>= 3 |
a = a >> 3 |
0 |
<<= |
a <<= 3 |
a = a << 3 |
40 |
PYTHON COMPARISON OPERATORS
Comparison operators are used to compare two values:
Example:1
>>>a = 5
>>>b = 3
>>> print(a == b)
# returns False because 5 is not equal to 3
Operator |
Name |
Example |
output |
== |
Equal |
a == b |
false |
!= |
Not equal |
a != b |
true |
> |
Greater than |
a > b |
true |
< |
Less than |
a < b |
false |
>= |
Greater than or equal to |
a >= b |
true |
<= |
Less than or equal to |
a <= b |
false |
PYTHON LOGICAL OPERATORS
Logical operators are used to combine conditional statements:
Example:1
>>x = 5
print (x > 3 and x < 10)
Operator |
Description |
Example |
output |
and |
Returns True if both statements are true |
a > 3 and a < 10 |
true |
or |
Returns True if one of the statements is true |
a > 3 or a < 4 |
true |
not |
Reverse the result, returns False if the result is true |
(not(a >3 and a < 10)) |
false |
SPECIAL OPERATORS
1) Python Identity Operators
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 |
output |
is |
Returns true if both variables are the same object |
a is c a is b a == b |
True |
is not |
Returns true if both variables are not the same object |
x is not z x is not y x !=y |
False True False |
Example:1. is operator
a = ["tiger", "lion"]
b = ["tiger", "lion"]
c = a
print(a is c)
# returns True because c is the same object as a
print(a is b)
# returns False because a is not the same object as b, even if they have the same content
print(a == b)
# to demonstrate the difference between "is" and "==": this comparison returns True because a is equal to b
Example : 2. is not operator
a = ["tiger", "lion"]
b = ["tiger", "lion"]
c = a
print(a is not c)
# returns False because c is the same object as a
print(a is not b)
# returns True because a is not the same object as b, even if they have the same content
print(a != b)
# To demonstrate the difference between "is not" and "!=": this comparison returns False because a is equal to b
2) Python Membership Operators
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 |
true |
not in |
Returns True if a sequence with the specified value is not present in the object |
true |
Example:1
x = ["apple", "banana"]
print("banana" in x)
# returns True because a sequence with the value "banana" is in the list.
Example:2
x = ["apple", "banana"]
print("pineapple" not in x)
# returns True because a sequence with the value "pineapple" is not in the list
PYTHON BITWISE OPERATORS
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 |