Junior — Middle
Can you explain the purpose of the else construct in conditional statements?
sobes.tech AI
Answer from AI
The else construction in Python conditional statements is used to define a block of code that will be executed if the condition in if (or elif) is false.
Example:
x = 10
if x > 0:
print("Positive number")
else:
print("Zero or negative number")
In this example, if x is greater than 0, the first block is executed; otherwise, the block after else is executed.
Thus, else allows you to specify an alternative behavior when the condition is not met.