Python broadly categories the operators in the following groups:
print(a + b) # Sum of a + b Output: 8 print(a - b) # Subtract a - b Output: 2 print( a * b) # Multiple a * b Output: 15 print(a/b) # Divide a / b Output: 1.6666666666666667 print(a % b) # Modulus a % b Output: 2 print(a ** b) # Exponentiation a ** b Output: 125 print(a // b) # Floor Division a // b Output: 1 |
Simple assignment
x = 5 print(x) #Output: 5 x = x + 1 print(x) #Output: 6 |
Above example x = x + 1 is same as x += 1, which is an Augmented assignment operator
x = 5 x += 1 print(x) #Output: 6 |
You can use this Augmented assignment operator for all other operators as well
x = 5 x -= 3 #same as x = x - 3 print(x) #Output: 2 x = 5 x &= 3 #same as x = x & 3 print(x) #Output: 1 x = 5 x &= 3 #same as x = x & 3 print(x) #Output: 1 x = 5 x ^= 3 #same as x = x ^ 3 print(x) #Output: 6 x=5 x <<= 3 #same as x = x << 3 print(x) #Output: 40 |
This operator is used to compare two elements and return Boolean value as result
x = 10 y = 20 print(x == y) #Equal #Output: False print(x != y) #Not equal #Output: True print(x > y) #Greater than #Output: False print(x < y) #Less than #Output: True print(x >= y) #Greater than or equal to #Output: False print(x <= y) #Less than or equal to #Output: True #Not Function print(not(True)) #False print(not(False)) #True |
x = 10 y = 20 print(x > 5 and x < 10) # Returns True if both statements are true Output: False print(x < 5 or x < 10) # Returns True if one of the statements is true Output: False print(not(x > 3 and x < 10)) # Reverse the result, returns False if the result is true Output: True |
Identity operators compare the memory locations of two objects. There are two Identity operators
x = ["apple", "banana"] y = ["apple", "banana"] z = x print(x is z) # returns True because z is the same object as x print(x is y) # returns False because x is not the same object as y, even if they have the same content print(x == y) # to demonstrate the difference betweeen "is" and "==" #this comparison returns True because x is equal to y print(x is not z) # returns False because z is the same object as x print(x is not y) # returns True because x is not the same object as y, even if they have the same content print(x != y) # to demonstrate the difference betweeen "is not" and "!=" #this comparison returns False because x is equal to y |
The membership operators in Python are used to test whether a value is found within a sequence.
#Returns True if a sequence with the specified value is present in the object x = ["apple", "banana"] print("banana" in x) # returns True because a sequence with the value "banana" is in the list #Returns True if a sequence with the specified value is not present in the object x = ["apple", "banana"] print("banana" in x) # returns True because a sequence with the value "banana" is in the list |
Operator |
Description |
Example |
& Binary AND |
Operator copies a bit to the result if it exists in both operands |
(a & b) (means 0000 1100) |
| Binary OR |
It copies a bit if it exists in either operand. |
(a | b) = 61 (means 0011 1101) |
^ Binary XOR |
It copies the bit if it is set in one operand but not both. |
(a ^ b) = 49 (means 0011 0001) |
~ Binary Ones Complement |
It is unary and has the effect of 'flipping' bits. |
(~a ) = -61 (means 1100 0011 in 2's complement form due to a signed binary number. |
<< Binary Left Shift |
The left operands value is moved left by the number of bits specified by the right operand. |
a << 2 = 240 (means 1111 0000) |
>> Binary Right Shift |
The left operands value is moved right by the number of bits specified by the right operand. |
a >> 2 = 15 (means 0000 1111) |
a = 60 # 60 = 0011 1100 b = 13 # 13 = 0000 1101 c = 0 c = a & b; # 12 = 0000 1100 print("Line 1 - Value of c is ", c) c = a | b; # 61 = 0011 1101 print("Line 2 - Value of c is ", c) c = a ^ b; # 49 = 0011 0001 print("Line 3 - Value of c is ", c) c = ~a; # -61 = 1100 0011 print("Line 4 - Value of c is ", c) c = a << 2; # 240 = 1111 0000 print("Line 5 - Value of c is ", c) c = a >> 2; # 15 = 0000 1111 print("Line 6 - Value of c is ", c) |
To evaluate the expressions there is a rule of precedence in Python. It guides the operations in the order that should carry out.
1. Brackets ()
2. Power off **
3. Multiplication, Division, Floor division, Modulus *, /, //, %
4. Addition & Subtraction +, -
5. Comparisons Operators ==, !=, >, >=, <, <=
6. Logical Operators not, and, or
print((2**2 + 1) * (3 + 7)) #Output: 50 print((2 == 2 ) and ( True or True)) #Output: True print(5 + (4 * 10) / 2) #Output: 25.0 print(5 + 4 * 10 // 2) #Output: 25 |
If you have any doubts or queries related to this chapter, get them clarified from our Python Team experts on ibmmainframer Community!