What is priority order of multiply, divide, modulo and floor division (*, /, %, //) in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 ?

16th Jun 2019, 7:52 PM
Rishu Kumar
Rishu Kumar - avatar
13 Answers
+ 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
16th Jun 2019, 8:21 PM
Seb TheS
Seb TheS - avatar
+ 14
From left to right in your exemple because *, /, %and //have the same precedence
17th Jun 2019, 4:53 PM
Mr.Fish
Mr.Fish - avatar
+ 8
*, /, //, and % should have the same precedence.
17th Jun 2019, 3:40 AM
Rick Shiffman
Rick Shiffman - avatar
+ 6
Rishu Kumar import numpy as np
16th Jun 2019, 8:29 PM
Seb TheS
Seb TheS - avatar
+ 6
*,/,%,// Have the same precedence..... These operators on occurrence on a same line the associativity is from left to right....
18th Jun 2019, 1:01 AM
Thanigai Velan
Thanigai Velan - avatar
+ 5
Seb TheS thanks a lot
16th Jun 2019, 8:28 PM
Rishu Kumar
Rishu Kumar - avatar
+ 5
BODMAS (like in traditional arithmetic) in all languages 🤔
17th Jun 2019, 12:15 PM
Sanjay Kamath
Sanjay Kamath - avatar
+ 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​
16th Jun 2019, 8:03 PM
Rishu Kumar
Rishu Kumar - avatar
+ 4
S S Skynet So * > / > % thanks for the help but what about //?
16th Jun 2019, 8:17 PM
Rishu Kumar
Rishu Kumar - avatar
+ 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.
16th Jun 2019, 11:47 PM
George
George - avatar
+ 4
Sanjay Kamath but BODMAS doesn't have floor division and modulo
17th Jun 2019, 12:37 PM
Rishu Kumar
Rishu Kumar - avatar
+ 3
cero
17th Jun 2019, 11:47 AM
Orlando Yván Niño Vásquez
Orlando Yván Niño Vásquez - avatar