Saturday, November 16, 2019

Order of Operations

Order of Operations


When an expression contains more then one operator, the order of evaluation depends on the order of operations. For mathematical operators, Python follows mathematical convention. The acronym PEMDAS is a useful way.


PEMDAS

Parentheses Exponentiation Multiplication Division Addition Subtraction.


 Parentheses  :

>>> 3*(3-2)
3
>>> 3*(3-1)
6

Exponentiation  :

>>> 1+2**3
9
>>> 1+2*3
7
>>> 2*6/2
6.0


Multiplication Division Addition Subtraction : 

>>> 1+2*3
7
>>> 2*6/2
6.0
>>> 2*3-1
5
>>> 6+4/2
8.0
>>> 5-1+2*5*4/2
24.0

No comments:

Post a Comment