In Zed Shaw Learn the Python Tough Path (pp. 15-16), it has an example exercise
100 - 25 * 3 % 4
the result is 97 (try it!)
I do not see the order of operations that could do this.
100 - 25 = 753% 4 = 0or (100-25 * 3) = 225% 4 = ??? but in any case not 97 I don’t think ...
A similar example 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6that gives 7
3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
In what order are operations performed?
: * % -, 25 * 3 % 4. * % , , 25 * 3. 75. 75 % 4, 3. , 100 - 3 - 97.
*
%
-
25 * 3 % 4
25 * 3
75
75 % 4
3
100 - 3
97
→ →
In [3]: 25 * 3 Out[3]: 75 In [4]: 75 % 4 Out[4]: 3 In [5]: 100 - 3 Out[5]: 97
, .
, - Zac , 1/4 - , Python 2.X . , , (, float, 0.
3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6 3 + 2 + 1 - 5 + (0) - (0) + 6 6 - 5 + 6 1 + 6 7
:
'*' '%' , .
Q.E.D.
: 100 - 25 * 3 % 4
, 25 * 3 75% 4 , .
, % python , x% y x / y. , 75 / 4 18, 3, 100 - 3 = 97.
x / y
75 / 4
100 - 3 = 97
% , *, 3 + 2 + 1-5 + 4% 2-1/4 + 6 = 3 + 2 + 1-5 + (4% 2) - (1/4) + 6 = 1 + (4% 2) - (1/4) +6 = 1 + 0- (1/4) + 6 = 1- (1/4) + 6 = 0,75 + 6 = 6,75, , , , , , - .
, , , . , .
75, 4, 18,75
18, 4, 72 ( 3 75)
100-25 * 3% 4 97. PEMDAS, :
#!/bin/python A = 100 B = 25 C = 3 D = 4 E = B*C # 75 F = E%D # 3 G = A-F # 97 print("B * C ="), E print("E % D ="), F print("A - F ="), G
, modulo (%) ,
Python evaluates% after *, but before + or _.
So,
(100 - 25 * 3 % 4) (100 - 75 % 4) (100 - 3) (97)