Explanation:
๐น Step 1: Understand Operator Priority
and has higher priority than or
So Python first evaluates:
[1] and 7
Expression becomes:
[] or 0 or False or 7
๐น Step 2: Evaluate [1] and 7
[1] and 7
๐ [1] is a non-empty list
Non-empty list = Truthy ✅
For and:
If first value is truthy → return second value
๐ Result:
7
๐น Step 3: Expression Now Becomes
[] or 0 or False or 7
๐น Step 4: Evaluate or Left to Right
๐ First value:
[]
Empty list = Falsy ❌
Move ahead
๐ Second value:
0
0 = Falsy ❌
Move ahead
๐ Third value:
False
False = Falsy ❌
Move ahead
๐ Fourth value:
7
7 = Truthy ✅
For or:
Python returns first truthy value
๐ Result:
7
๐น Step 5: Final Output
print(7)
๐ Output:
7
⚡ Key Concepts
๐งฉ and Operator
A and B
Returns first falsy value
Otherwise returns second value
๐งฉ or Operator
A or B
Returns first truthy value
๐ Final Answer:
7

0 Comments:
Post a Comment