operator precedence in python
Welcome everyone, today’s we are going to see what is operator precedence in python.
So let’s start:
There are Two type Of operator Precedence
- highest precedence
- highest precedence
|operator precedence in python- operator associativity|
following table lists show operator from highest to lowest precedence.
For example, z = 4 + 5 * 6; here, z is assigned 34, not be 54 reason is because operator * has higher precedence than +operator, so it first multiplies 5*6 and then adds into 4.Operator precedence affect how an expression is evaluated in arthi.
#programming with pythonslearning.com p = 10 z = 20 r = 30 s = 5 pa = 0 pa = (p + z) * r / s #( 10 + 20 )* 30/ 5 print "ans of (p + z) * r / s is shown ", pa pa = ((p + z) * r) / s # ((10 + 20 )*30)/ 5 print "ans of ((p + z) * r) / s is shown ", pa pa = (p + z) * (r / s); # (10+20) * (30/5) print "ans of (p + z) * (r / s) is shown ", pa pa = p + (z * r) / s; # 10 + (20*30)/5 print "ans of p + (z * r) / s is shown ", pa
After the execute the above programs, it produce the following outcome results −
ans of (p + z) * r / s is shown 180 ans of ((p + z) * r) / s is shown 180 ans of (p + z) * (r / s) is shown 180 ans of p + (z * r) / s is shown 130
1. What is operator precedence in Python?
|[**]||This is Exponentiation precedence it’s raise to the power|
|[~ + -]||This is Complement, unary plus and minus precedence|
|[* / % //]||This is Multiply, divide, modulo and floor division precedence|
|[+ -]||This is Addition and subtraction precedence|
|[>> <<]||This is Right and left bitwise shift precedence|
|[&]||This is Bitwise ‘AND’td> precedence|
|[^ |]||This is Bitwise exclusive `OR’ and regular `OR’ precedence|
3. Which operator has the lowest precedence in Python?
|[<= < > >=]||This is Comparison operators precedence|
|[<> == !=]||This is Equality operators precedence|
|[= %= /= //= -= += *= **=]||This is Assignment operators precedence|
|[is is not]||This is Identity operators precedence|
|[in not in]||This is Membership operators precedence|
|[not or and]||This is Logical operators precedence|
Tags: operator precedence in python- operator associativity ,lowest precedence,highest precedence.
For the similar post like python operator click on operators in python.
if you want to learn python for free then click on python tutorials point.
If you have any question about this section then comment please and if you like this post then please comment and share.
BEST OF LUCK!!!