What is the output of `print(3 > 2 > 1)`?

Master Python with the PCAP Certification! Explore interactive quizzes and detailed explanations to ensure your exam success. Gain confidence and get certified!

The expression 3 > 2 > 1 utilizes chained comparison operators in Python. In this case, Python evaluates the comparisons from left to right. First, it checks if 3 > 2, which evaluates to True. Then, it checks if 2 > 1, which also evaluates to True. Since both comparisons are true, the whole expression evaluates to True.

Chained comparisons in Python allow for a cleaner and more readable way to express conditions where multiple comparisons are logically connected. The expression 3 > 2 > 1 can be understood as asserting "3 is greater than 2 and 2 is greater than 1" simultaneously, making it a straightforward true condition.

This behavior is unique to Python and differentiates it from many other programming languages, where comparisons would need to be evaluated separately. Thus, the correct output of the expression is indeed True.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy