Explanation:
1. Code
print(True + True * 2)
\
2. True as an Integer
In Python, bool is a subclass of int.
So Python treats:
True
as:
1
Therefore:
True = 1
3. True * 2
First, Python evaluates the multiplication:
True * 2
Since True is 1:
1 × 2 = 2
So:
True * 2
becomes:
2
4. True + 2
Now the expression becomes:
1 + 2
Therefore:
3
5. Operator Precedence
Python performs multiplication before addition.
So:
True + True * 2
is evaluated as:
True + (True * 2)
not:
(True + True) * 2
6. print()
Finally:
print(3)
displays the result.
✅ Final Output
3

0 Comments:
Post a Comment