+11

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

+9

*, /, //, 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

+15

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

+13

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

+12

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

+10

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.

+8

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

+6

https://www.sololearn.com/learn/Python/2280/

+6

Rishu Kumar import numpy as np

+6

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

+5

Seb TheS thanks a lot

+5

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

+4

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​

+4

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

+4

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.

+4

BODMAS (like in traditional arithmetic) in all languages 🤔

+4

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

+3