Python Operator Precedence rules and its real-time usage

Hello readers, welcome back to know the Python Operator Precedence rules and guidelines.  It is very important to know which comes the first and next, as a mathematician rules we know the operators to be executed correctly. But to identify the same with computer programs we have Operator Precedence rules and executes mathematical expressions correctly.

Python operators have a set order of precedence, which determines what operators are evaluated first in a potentially ambiguous expression. For instance, in the expression 3 * 2 + 7, first 3 is multiplied by 2, and then the result is added to 7, yielding 13. The expression is not evaluated the other way around, because * has higher precedence than +.

python operator precedence

Continuation of the Python Operators from the previous article was written here,

Python Logical Operators:

Python language supports logical operators, and, or, and not as a truth table.
The following assumptions for the below, variable a is 10, b is variable 20:

Operators Logical expression Description Examples
and x and y Boolean “and” – if x is False, x and y return’s False, else it returns evaluation of y. (A and b) returns 20.
or x or y Boolean “or” – If x is non-zero, it returns the value of x, else it returns evaluation of y. (A or b) returns 10.
not not x Boolean “not” – If x is True, it returns False. If x is False, it returns True. not (a and b) returns False

Python Bitwise Operators:

Bitwise operators are considered binary numbers to make the calculation. Python is Bitwise rule as follows:

As ‘a’ variable in the following table 60, ‘b’ variable 13.

Operators Description Examples Output
& Bitwise and operator: two values involved in operation, if both corresponding bits are 1, the result bit is 1, 0 otherwise (A & b) Output is 12, binary explanation: 0000 1100
| Bitwise or operator: as long as the two corresponding binary bit is a 1, the resulting bit is 1. (A | b) Output is 61, binary explanation: 00111101
^ Bitwise exclusive OR operator: When the two corresponding binary bit different and the result is 1 (A ^ b) Output of 49, binary explanation: 00110001
~ Bitwise operators: each binary data bit inversion, that is 1 to 0, the 0 to 1 (~ A) -61 output, binary explanation: 1100 0011 in a symbolic form complement binary number.
<< Left mobile operators: each binary operands all left a number of bits specified by the number “<<” right of the decimal moves, discarding the high and lower 0s. a << 2 Output is 240 Binary interpretation: 11110000
>> Right Mobile operators: to each binary “>>” the left operand Several all right, “>>” on the right the number of bits specified a >> 2 Output is 15, binary explanation: 0000 1111

Python Identity Operators:

Identity operator is used to comparing two objects of storage units.

Operators description Examples
is is judging the two identifiers are not referenced from an object x is y, if id (x) equals id (y), it is returned 1 as a result
is not It is not to judge the two identifiers are not referenced from different objects x is not y, if id (x) is not equal to id (y). it is not returned 1 as a result

Python Member Operator:

In addition to some of the above operators, Python also supports member operator, test case contains a number of members, including strings, lists or tuples.

Operators Description Examples
in If you find the value in the specified sequence returns True, otherwise False. x in y sequence, returns True if x in y sequence.
not in If the value is not found in the specified sequence returns True, otherwise False. x is not y sequence, if x is not y sequence returns True.

We have finished all the Python Operators, the next is Python Operator Precedence rules and guidelines.

Python Operator Precedence:

The following table lists from highest to lowest priority of all operators:

Operators Description
** Index (highest priority)
~ + – Bitwise inversion, unary plus and minus (the last two method named + and @ – @)
* /% // Multiplication, division, and modulo take divisible
+ – Addition Subtraction
>> << Right, left operator
& Bit ‘AND’
^ | Bitwise Operators
<= <>> = Comparison
<> ==! = Equality operator
=% = / = @ = – = + = * = * = Assignment Operators
is is not Identity operator
in not in Member operator
not or and Logical Operators

To know Basic Python Operators and its real-time usage, click here.

If you like the post and it is really useful, please share and comment.

Python Operator Precedence rules and its real-time usage
Rate this post

Reply