Explanation:
๐ข Step 1: Create the List
x = [10, 20, 30]
The list has these indexes:
Index: 0 1 2
Value: 10 20 30
So:
- x[0] → 10
- x[1] → 20
- x[2] → 30
๐ก Step 2: Understand x[True]
x[True]
In Python, bool behaves like an integer:
True = 1
False = 0
Therefore:
x[True]
is equivalent to:
x[1]
So:
x[True] → 20
๐ต Step 3: Understand x[False]
x[False]
Since:
False = 0
we get:
x[False] → x[0] → 10
๐ฃ Step 4: Understand x[-1]
x[-1]
-1 always refers to the last element of a list.
Therefore:
x[-1] → 30
๐ Step 5: Substitute the Values
Original expression:
print(x[True] * x[False] - x[-1])
Replace each part:
20 * 10 - 30
๐ด Step 6: Multiplication First
According to operator precedence, * is evaluated before -.
20 * 10 = 200
So the expression becomes:
200 - 30
๐ข Step 7: Subtraction
200 - 30 = 170
Therefore Python executes:
print(170)
๐ฏ Final Output
170
Books: 100 Senior-Level Python Interview Questions (Basic to Advanced)

0 Comments:
Post a Comment