Facebook Pixel

if Statement

Sometimes the programmer needs to check the evaluation of certain expression(s), whether the expression(s) evaluate to True or False. If the expression evaluates to False, then the program execution follows a different path than it would have if the expression had evaluated to True.

Based on this, the conditional statements are further classified into the following types: if, if......else, elif, nested if.

if Statement:

A simple if statement works on the following principle:

  • Execute the block of code inside the if statement if the expression evaluates to True.
  • Ignore the block of code inside the if statement if the expression evaluates to False and return to the code outside the if statement.

if statement

Example:

applePrice = 180
budget = 200
if (applePrice <= budget):
    print("Alexa, add 1kg Apples to the cart.")

Output:

Alexa, add 1kg Apples to the cart.
if Statement | Python Tutorial | CodeWithHarry