Keywords: if statement, conditional statement, else statement, elif statement
Summary of the chapter “If statements in Python”. In this chapter we have looked at the basics of conditional statements that allow you to decide what will happen in the program depending on the state of a Boolean expression (True or False). We do this with the if, elif and else statements.
A condition statement in Python always starts with an if condition. If the if condition is true, then all the operation belonging to the if statement will be performed. However, the if condition is false, the program skips all operations in the if statement and proceeds to the else if statements accordingly. If none of the if or else if statements are fulfilled, then the operations in the else statement will be performed.
The conditional statements are evaluated by conditions. The terms can have two states, true, or false. If the condition is true, the condition statement will be executed, or if the condition statement is false, then the condition statement will not be executed.
With the help of if-statements we can:
The if statement in Python is a conditional statement that determines whether the program should perform an operation, or several operations, depending on whether one or more specified conditions are met or not. The condition is a logical expression that is either true or false.
if (logical expression): Operation / Operations …
The else statement in Python is used to specify a block of code to be executed if the condition in the if statement is false. The keywords used for the if statement and else statement describes their relationship quite well. “If this condition is true, perform the associated code block, else, execute this code block”.
if logical expression statement doing else what this statement should perform
if (logical expression): statement executing elif (logical expression): what elif statement should perform else (logical expression): what else statement should do