Operator Precedence
Operator precedence is a very important concept in programming from a programmer aspect. Python’s order of operations is the same as that of normal mathematics: parentheses first, then exponentiation, then multiplication/division, and then addition/subtraction.
The following table lists all of Python’s operators, from highest precedence to lowest :
Operator | Description |
** | Exponent (power) |
~ + – | Compliment, Unary plus (@+), and Unary minus (@-) |
* / % // | Multiply, Divide, Modulo and Floor division |
+ – | Addition and Subtraction |
>> << | Right and Left bitwise shift |
& | Bitwise ‘AND’ |
^ || | Bitwise exclusive ‘OR’ and regular ‘OR’ |
< <= > >= | Comparison operators |
<> == != | Equality operators |
= %= /= //= -= += *= **= | Assignment operators |
is is not | Identity operators |
in not in | Membership operators |
not and or | Logical operators |
In the above list precedence of operators decrease from top of the list to bottom. Let’s take some examples for better understanding.
Example – 1 :
>>> False == False or True True >>> False == (False or True) False >>> (False == False) or True True # As the first result is identical with the last one so from this example we # can conclude that == has a higher precedence than or, that means Equality # operators have higher precedence than Logical operators. This supports the # precedence list also.
Example -2 :
if 1 + 1 * 3 == 6: print("Yes") else: print("No")
The output of the above code is :
>>>No #__________________________________________________________________________ # As Python's order of operations is the same as that of normal mathematics # So first you must multiply 1 * 3 = 3 # Next, add 1 + 3 = 4 # == Meaning "Does 1 + 1 * 3 equal 6? # Print 'Yes' if true or 'No' if false" # It is not true that 4 = 6, so "No" will be the output
Example – 3 :
x = 4 y = 2 if not 1 + 1 == y or x == 4 and 7 == 8: print("Yes") elif x > y: print("No")
The output of the above code will be :
>>>No
Let me explain the output.
First, we have initialized x with 4 and y with 2.
The next line is : if not 1 + 1 == y or x == 4 and 7 == 8;
Step 1:
As per the operator precedence First AND is considered, i.e. x == 4 and 7 == 8
this will be like TRUE and False, which will result FALSE.
Now it’ll look like, if not 1 + 1 == y or FALSE. As 1 + 1 == 2 which is equals to y so this is TRUE.
Step 2 :
Now as per the operator precedence OR will be considered, TRUE or FALSE, which will result TRUE.
Now it’ll look like, if not TRUE, Which will be as if False, always the statement within False will not be considered.
Step 3 :
So the elif is considered. Now as 4 is greater than 2, which means x>y, so the condition in elif statement is
satisfied and the answer is ‘NO’.
not Operator in Python
NOT operator in python ,Unlike other two boolean operators, ‘not’ operator takes only one argument and inverts it. That means if as an argument we are passing a True value, ‘not’ operator will return False and if we are passing False then ‘not’ operator will return True. This concept is also applicable to numerical values, means we know in Python every non-zero value is considered as True, and zero is considered as False. So if we pass any non-zero value to a ‘not’ operator, it will return False, and if we pass zero then it will return ‘True’. This concept is applicable for variables also if the variable contains a value. If the value is anything except zero, then the variable will be considered as True, and if the variable contains zero then it will be treated as False.
As example:
# Using boolean values #_____________________ >>>not True False >>not False True # Using numeric values #_____________________ >>>not 5 False >>>not 0 True
Let’s take an example of ‘not’ operator:
a=5 if not a: print("1") elif not (1+1==2): print("2") else: print("3")
As output we will get:
3
Let’s take an explanation of the output. We have declared a variable a with the value 5. So as a contain a non-zero value then a will be treated as True.
So as a contains True so the condition of the if statement is False (as not a => not True => False). So command comes to elif block. Now as 1+1 is equals to 2, so the condition is True. But as there is a not present before the condition so the overall condition of the elif statement is False. So, at last, the command comes to else block and 3 is printed.
That was all about ‘not’ operator. I hope you like this tutorial.