Step 1: Understand if x:
In Python:
0, None, False, "", [] → False
-
Any non-zero number (positive or negative) → True
Here:
x = -1➡️ -1 is non-zero, so it is True
Step 2: if block executes
Since x is True, Python enters this block and prints:
Non-zeroStep 3: elif and else are skipped
Once an if condition is True:
-
Python does NOT check elif
Python does NOT check else
So this part is never executed:
✅ Final Output
Non-zero๐ Key Concept (Tricky Point)
if x: does NOT check the value of x, it checks truthiness.
✔️ -1 → True
✔️ 1 → True
❌ 0 → False
๐ก Tip for Quizzes
To specifically check -1, always write:
if x == -1:
.png)

0 Comments:
Post a Comment