Author | Message |
---|---|
rabbott
Posts: 1649
|
Posted 18:49 Mar 09, 2019 |
Can you explain the following?
|
bfazeli
Posts: 6
|
Posted 19:12 Mar 09, 2019 |
When i = 2 Last edited by bfazeli at
19:14 Mar 09, 2019.
|
rabbott
Posts: 1649
|
Posted 19:25 Mar 09, 2019 |
I think you have it, but would you provide more elaboration. Your explanation is difficult to follow since it is so compressed. |
jpatel77
Posts: 44
|
Posted 20:14 Mar 09, 2019 |
1. LHS: (0+1) = 1 i.e True since it’s truthy; RHS=(1) since i%2==0 is true = 1 i.e. True since it’s Truthy; therefore LHS = RHS 2. Since 0%2 is 0; therefore same goes as above 3. Pure magic (or a possible bug?) of precedence. Mod is evaluated first, which returns 1, then + which returns 2, then == which returns False i.e. 0 as well, and finally ternary, which anyway retuns false case i.e. 2. Edit: I think this has more to it since I just learned if else has higher precedence. Have to look at it. Edit 2: My bad. This table lists the precedence in least to most binding order which made me confused. So ignore all edits: https://docs.python.org/3/reference/expressions.html#operator-precedence
Last edited by jpatel77 at
20:49 Mar 09, 2019.
|
jungsoolim
Posts: 38
|
Posted 20:28 Mar 09, 2019 |
I tested few cases. Based on the experiments, the if-then-else function has higher precedence than mod, add, and comparison. If-then-else is evaluated first. Then %, +, and finally ==. (Maybe?) i=1 |
jpatel77
Posts: 44
|
Posted 20:36 Mar 09, 2019 |
Don't you think it gave these outputs because the ternary is evaluated at the end? Last edited by jpatel77 at
20:48 Mar 09, 2019.
|
jungsoolim
Posts: 38
|
Posted 21:07 Mar 09, 2019 |
Yes, you are right. If the condition is true, the first part is evaluated. Otherwise it evaluates the else part. i = 1 Thus, it is same with (i%2 + 1 == 1) if i%2 == 0 else 2 Or, it is equivalent with (i%2 + 1 == 1, 2)[i%2 ==0] The first part is for true condition, and the second part is for false condition. (i%2 + 1 == 1, 77)[i%2 !=0] (i%2 + 1 == 1) if i%2 != 0 else (i%2 + 1 == 2) (Maybe ?)
Last edited by jungsoolim at
21:12 Mar 09, 2019.
|
jpatel77
Posts: 44
|
Posted 21:16 Mar 09, 2019 |
This is so clever! I really liked this one. And yes, you’re right. |
asharm33
Posts: 2
|
Posted 00:08 Mar 10, 2019 |
1. Case when i = 2 and the expression is `(i%2 + 1) == (1 if i%2==0 else 2)`, then it works following way:
|
rabbott
Posts: 1649
|
Posted 13:22 Mar 10, 2019 |
Nice discussion! As Jay pointed out, the key is the precedence of the operators. (See the table he linked to.) The
Last edited by rabbott at
13:23 Mar 10, 2019.
|
rabbott
Posts: 1649
|
Posted 13:25 Mar 10, 2019 |
That raises a second (easier) challenge. Apparently P.S. It really is easy to prove. Last edited by rabbott at
13:26 Mar 10, 2019.
|
dvariya
Posts: 3
|
Posted 14:35 Mar 10, 2019 |
They will always produce the same result because: i % 2 will either return 0 or 1. (no values other than 0/1) When i % 2 returns 0: "i%2+1" evaluates to 1 In a similar way, "i%2+1" evaluates to 2 |
rabbott
Posts: 1649
|
Posted 15:18 Mar 10, 2019 |
That's good. Can you (or anyone) show how to transform Last edited by rabbott at
15:19 Mar 10, 2019.
|
dvariya
Posts: 3
|
Posted 16:03 Mar 10, 2019 |
Not sure, If I have understood correctly. In order to transform, we can use Bitwise AND as shown below: a = (i & 1) + 1 print(a) |
jpatel77
Posts: 44
|
Posted 16:20 Mar 10, 2019 |
I would say: 1 if i%2==0 else 2 Last edited by jpatel77 at
16:21 Mar 10, 2019.
|
rabbott
Posts: 1649
|
Posted 19:42 Mar 10, 2019 |
Terrific! Just what I was hoping for. Another way of putting the next to last step is as follows. A tautological way to write i%2 is 0 if i%2 == 0 i%2 = = 0 if i%2 == 0 else 1 1 if i%2 == 1 Last edited by rabbott at
19:47 Mar 10, 2019.
|