In Python, the statement is used for execution of code. It allows you to specify a block of code that will be executed only if a certain condition is met. The basic syntax of an if statement in Python is as follows: if condition: # indented code to be executed if the condition is True. You can also include an else block to specify code that should be executed when the condition is not met.
The if-elif-else statement is used when you have multiple conditions to check in a sequence. It allows you to test each condition one by one, and as soon as one of the is satisfied, the corresponding block of code will be executed, and the rest of the branches will be skipped. This is known as a if. The second if will only execute if the first if evaluated as false. Nested if statements are used when you have an if statement inside another if statement, allowing you to create more complex and control flows.
Switch statements are a really useful tool for structured pattern matching. From Python 3.10 onwards it is supported using the match - case statement. When a switch statement is encountered, the expression is evaluated once. Each case constant is compared against the expression value. Upon finding a match, the corresponding code block is executed until a statement is encountered or the switch statement concludes. If none of the cases match the expression value, the code within the _ case is executed. Before Python 3.10 was released there wasn't a switch statement, so programmers had to emulate it using if statements.
Keywords
conditions | value | break | if | conditional | chained | conditions |