Introduction to if statements in Python

The if statements in Python is a so-called condition statement. It is a single or multiple statements that are performed depending on whether a logical precondition is fulfilled or not

HOW TO USE AN IF statement IN PYTHON?

The if statement is used to decide whether or not to perform an operation. If the condition is true, then the operation is performed. If the condition is false, the operation is not performed. The if statement can be combined with several if statements, so-called else-if and else statements.

The if statement is used to decide whether or not to perform an operation

In this chapter, we will go through how to combine if-statements with else-if and else-statements to create conditional programming. The flowchart illustrates whether or not an operation should be performed, depending on whether or not the condition in the if statement is met.

If statements Python

DIFFERENT CONDITION STATEMENTS IN PYTHON

  • if statement  – One or more conditions indicate if one (or more) operations are to be performed

 

  • Elif statement –  The operation / operations inside the elif statement (else if) are performed only if the condition of the if statement is false and the condition of the elif statement is true.

 

  • else – performed when the if and all Elif statements are false.

LOGICAL COMPARISONS IN PYTHON

It might be a good idea to repeat and remind ourselves of the logical operators we saw in the previous chapter. These will be very useful when using the if statement.