What Is Chained Conditional in Python?

What Is Chained Conditional in Python?

Python provides an alternative way to write nested selection such as the one shown in the previous section. This is sometimes referred to as a chained conditional. if x < y: print("x is less than y") elif x > y: print("x is greater than y") else: print("x and y must be equal")

What is the difference between a chained conditional and a nested conditional?

# A chained conditional is when you use if/elif/else flow controls and they are all indented to the same depth. ... # A nested conditional is when you use if/elif/else flow controls and the conditionals vary in depth to create a more nuanced sequence of conditionals.

How many if statements can be contained in a chained conditional?

Again, exactly one branch will be executed. There is no limit of the number of elif statements but only a single (and optional) final else statement is allowed and it must be the last branch in the statement.

Maya Lin-Takahashi
Author

Maya Lin-Takahashi

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.