Basic conditional execution with if and else
The if statement is the fundamental conditional construct in Python. It evaluates a condition (Boolean expression) and executes the indented code block only if the condition is True. The optional else clause provides an alternative block that executes when the if condition is False. The condition can be any expression that evaluates to a Boolean value, including comparisons, logical combinations, function calls that return Booleans, or even values that Python interprets as truthy/falsy (non-zero numbers, non-empty sequences are truthy; zero, empty sequences, None are falsy). Proper indentation is crucial as it defines the code blocks. The if and else statements form the basis of decision-making in programs, allowing different code paths based on variable values, user input, calculation results, or other conditions. This enables programs to handle different scenarios appropriately rather than following a single fixed sequence of operations.