Python 4.3 - Operator precedence and associativity
Operator precedence and associativity
We can easily solve a simple statement like x=5+2 where only
one or two operators (‘=’ and ‘+’) are used. Because we know that, first we will
add 5 and 2 i.e. would use ‘+’
operator and then store the result 7 into ‘x’ i.e. use ‘=’ operator. So when a statement or expression contains one or two
operators then it becomes easy for us to solve such statement but how would you
solve if a statement contains many operators together? For example:
x=35+6/3-(2+2)*(5-3)**4//2+2.2
There are many operators used in a single statement. You
might be confused by seeing the statement. You would think that just execute
the statement as it is written i.e. first add 35 and 6 then divide the result
by 3 then add 2 and 2 and so on. But it does not work like this.
When multiple operators are used in a single statement
then it must be known which operator will be executed first, which will be
executed next and so on. The process of deciding that which operator will be
executed first is called operator precedence.
Each operator has its own priority to get executed in a
statement or expression. Operator precedence represents the priority of an
operator which tells that when will that particular operator be executed in a
statement. See the table below that tells the precedence of different operators.
Operator |
Name |
Precedence or priority |
( ) |
Parentheses |
1 |
** |
Exponent |
2 |
+, -, ~ |
Unary plus, Unary minus, bitwise NOT |
3 |
*, /, %, // |
Multiplication, Division, Modulus, Floor division |
4 |
+, - |
Addition, Subtraction |
5 |
<<, >> |
Bitwise shift operators |
6 |
& |
Bitwise AND |
7 |
^ |
Bitwise XOR |
8 |
| |
Bitwise OR |
9 |
<, >, <=, >=, !=, == |
Relational or comparison operators |
10 |
=, +=, -=, /=, //=, %=, *= |
Assignment operators |
11 |
is, is not |
Identity operators |
12 |
in, not in |
Membership operators |
13 |
not |
Logical not |
14 |
and |
Logical and |
15 |
or |
Logical or |
16 |
Operator with highest precedence or priority will get
executed first in a statement than operator with lowest priority. Numbers in
table represent the precedence of operators which means operator with priority
1 will be executed before operator with priority 2 in a statement and so on.
for example, if we have an expression x=5+6/2**2 then we can solve like this-
x=5+6/2**2 # exponent operator ‘**’ has higher priority than ‘+’
and ‘/’
x=5+6/4 # first perform 2**2, then perform 6/4
x=5+1.5 # then addition
x=6.5
As you can see in above expression, first ‘**’ operator got
executed then ‘/’ operator and then ‘+’ operator.
okay! So what will happen if division, multiplication and
modulus all come together in a single statement. Whom will get executed first
because all these operators have same precedence 4. For example, in statement ‘x=12/2//2%3’
which operator will be executed first? All have same priority (see above table).
In such situation, operator associativity comes into action.
When multiple operators with same precedence appear in a
single statement or expression then from where to start execution i.e. from
left to right or from right to left. This is called operator associativity.
Almost all operators in Python have left to right associativity. So when
operators with same precedence appear in an expression then start executing
from Left to Right.
now let’s solve above expression with the knowledge of
precedence and associativity –
x=35+6/3-(2+2)*(5-3)**4//2+2.2 # parentheses has highest
precedence
x=35+6/3-4*2**4//2+2.2 # ** operator has highest precedence
x=35+6/3-4*16//2+2.2 # divide, multiplication and Floor
division,
#
all have same precedence
#
so start executing from left to right
#
i.e. divide->multiply->Floor division
x=35+2.0-64//2+2.2
x=35+2.0-32+2.2 # addition and subtraction have same
precedence
# so go
from Left to Right
# i.e.
addition->subtraction->addition
x= 7.2
Importing modules
A module is a file which contains Python code such as
variables, functions, classes etc. Our Python file is also a module. For
example, we created a program and saved the file named ‘sample.py’ then the
name ‘sample’ will be a module. The advantage of modules is that we can import
them in our program so that we can avoid writing lengthy code. Whenever I want
to use a module then, first I have to include it in my program by using
‘import’ keyword. You must be thinking, how modules are important? Then let’s
take an example. Suppose we have written a program to calculate simple interest
and saved the file named ‘s_interest.py’. Remember here module name is
‘s_interest’. Now we are writing other programs and at that time, we need to
calculate simple interest at many parts in our program. So one option is that
we write code for simple interest again and again whenever we need it. This
will be very tedious, time consuming and unnecessary job.
Second and best option is that we can import the file ‘s_interest’
in our new program and then just use the code that we have already written.
This will save our lots of time and effort.
We can not only import our Python files but also other
modules or libraries that are already available in Python. We have already
discussed about Python libraries such as pandas, math, numpy etc. in 2nd
chapter. These libraries contain many pre-defined functions which we can use in
our programs whenever we need them. For example, if we want to calculate the
square root of a number then first option is that we have to write some code or
logic for that. And second option is that we can simply import math module in
our program and use its sqrt() function. So we do not need to write separate
code to calculate the square root, which will save our time.
How to import
modules?
We can import a module in many ways in our program by using
‘import’ keyword. The first method to use ‘import’ is as follows –
import <module-name>
Where module name is the name of the module which we want to
import. To import ‘math’ module in our program, we can simply write –
import math
Now math module is imported in our program so we can use its
functions. To calculate the square root of number 25 we can use the function
sqrt() of math module. The syntax to use any function of a module is–
<module-name>.<function-name>
Do not use angular brackets <>. It is only for syntax
representation.
import math
x = math.sqrt(25)
We can also import a module as a nick name. This helps us to
write less statement. If we import as shown above, then we always have to write
module name followed by function name which will waste our time. So we can use a
short nick name method so that we do not need to write module name when calling
a function. We can just use nick name instead–
import <module-name> as <nick-name>
To use sqrt() function of math module with nick name –
import math as m
x = m.sqrt(25)
We can also import a particular function from a module using
‘from’ keyword. In this case, we
need not write module name. But we cannot use other functions in this method since
we specify the name of functions–
from <module-name> import <function-name>
from math import sqrt
x = sqrt(25)
x = sin(25) # error, because we imported only ‘sqrt’ function
The best and mostly used method is that we can import all
the functions of a module at once. When we do this, then we need not write the
module name–
from <module-name> import *
from math import *
x = sqrt(25)
Comments
Post a Comment