Python if else
- The if...else statement takes care of true as well as false conditions. It has two blocks.
- One block is for if and it is executed when the condition is true. The other block is of else and it is executed when the condition is false.
- The else statement cannot be used without if. No multiple else statements are allowed with one if.
- The most classic control structure is the conditional one. It acts upon the result of an evaluation. If you know any other computer language, chances are that you are familiar with if-else.
- If evaluates an expression. If the expression is true, the block of code just after the if clause is executed. Otherwise, the block under else is executed.
if x%2 == 0 :
print('x is even')
print('x is odd')
- If the remainder when x is divided by 2 is 0, then we know that x is even, and the program displays a message to that effect. If the condition is false, the second set of statements is executed.
- Since the condition must either be true or false, exactly one of the alternatives will be executed. The alternatives are called branches, because they are branches in the flow of execution.