+ 12
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 ?
17 ответов
+ 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
+ 14
From left to right in your exemple because *, /, %and //have the same precedence
+ 8
*, /, //, and % should have the same precedence.
+ 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
BODMAS (like in traditional arithmetic) in all languages 🤔
+ 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
Sanjay Kamath but BODMAS doesn't have floor division and modulo
+ 3
cero