Why 4 not printed (not weird)? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
7th Jun 2019, 1:20 AM
Ahmed Gamal
Ahmed Gamal - avatar
3 Answers
+ 4
replace & by "and", four spaces before print in line 5,9
7th Jun 2019, 1:39 AM
**🇦🇪|🇦🇪**
**🇦🇪|🇦🇪** - avatar
+ 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
7th Jun 2019, 2:14 AM
Diego
Diego - avatar
+ 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
7th Jun 2019, 1:58 AM
Diego
Diego - avatar