Explanation:
๐ป Code
x = [] or [1] or [2]
print(x)
๐น Line 1: x = [] or [1] or [2]
x = [] or [1] or [2]
Python evaluates the values from left to right.
๐น Step 1: []
[]
[] is an empty list, so Python considers it Falsy.
Therefore, Python moves to the next value.
๐น Step 2: [1]
[1]
[1] is a non-empty list, so it is Truthy.
Python stops here because or has already found a Truthy value.
๐น Step 3: [2]
[2]
This value is never evaluated as the result, because Python already found [1].
๐น Line 2: print(x)
print(x)
x contains the first Truthy value, which is [1].
๐ฏ Output
[1]

0 Comments:
Post a Comment