What is priority order of multiply, divide, modulo and floor division (*, /, %, //) in python?

And hence what should be the output of 3*7//5*10%4 ?

6/16/2019 7:52:04 PM

Rishu Kumar

17 Answers

New Answer


*, /, //, and % have the same operation precedence. 3*7 is executed first, because it appears to be there first. Then 21//5 Then 4*10 Then 40%4


You can check here. http://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html Hope this may helped you. 😇😇😇


Rishu Kumar It is mentioned there that. "Operators in the same box evaluate left to right."


From left to right in your exemple because *, /, %and //have the same precedence


Rishu Kumar Python follows PEDMAS P - Parentheses () E - Exponention (power op **) D - Divison M - Multiplication A - Addition S - Subtraction order of evaluation left to right for all but right to left for (**) if more than one operator of same precedence then left to right associativity ie 5/3*2 - divison first 5*3/2 multiplication first.


*, /, //, and % should have the same precedence.




Rishu Kumar import numpy as np


*,/,%,// Have the same precedence..... These operators on occurrence on a same line the associativity is from left to right....


Seb TheS thanks a lot


Rishu Kumar https://docs.python.org/2/reference/expressions.html#operator-precedence


S S Skynet But the link u provided doesn't have // and puts *, /, % in the same row and doesn't tell which has more priority​


S S Skynet So * > / > % thanks for the help but what about //?


just a note on the side here: you should definitely know operator precedence and use it. but if you want your code to be pythonic, try to make it readable as a first priority. my personal preference is to be concise and also that the code is easily understood. if i make a shorter statement that relies heavily on operator precedence, it is likely that it won’t be as readable at least in some cases. so i always prefer to be a bit more explicit with parethesis.


BODMAS (like in traditional arithmetic) in all languages 🤔


Sanjay Kamath but BODMAS doesn't have floor division and modulo