0
Why 4 not printed (not weird)?
3 Answers
+ 4
replace & by "and", four spaces before print in line 5,9
+ 3
It's printing "Weird" because of operator precedence, not the missing parentheses.
1. "N%2!=0" is False as 4==2*2+0.
2. "N%2==0 & N in range(2,6)" is treated as "(N%2==0 & N) in range(2,6)" because "bitwise AND (&)" has higher precedence than "in".
"N%2==0 & N" equals "1 & 4" equals 0, which is not in range(2,6).
3. Same goes for range(6,21).
4. "N>20" is clearly False.
5. "else" part is executed and "Weird" is printed.
https://docs.python.org/3/reference/expressions.html#operator-precedence
+ 1
Pulkit Kamboj Although they may increase readability, parentheses are not necessary around if/elif/else statements.
Take a look at the codes in the documentation.
https://docs.python.org/3/tutorial/controlflow.html