Python Provides conditional statements using if, elif and else keywords.

It does not have support for switching and goto in if the conditional expression

It provides the following features

  • Simple If Statement
  • if else statements
  • if elif else statements

if statements are used to test conditional expressions and execute code blocks.

Code blocks are multiple code statements. These are not enclosed in {}. Instead, it uses Indention as a code block.

For example, indentation on the line removed, tells that the code block is complete for the given condition.

Python simple If Statement

first = 20
second = 8
if first > second:
    print(" First is greater than Second");

if second > first:
    print(" Second is greater than First ");
if second == first:

    print(" First is equal to Second");

Python if else example

If else is used to execute else condition, else is optional.

first = 10
second = 15
if first > second:
    print(" First is greater than Second");
else:
    print(" Second is greater than First ");

Python if elif else example

Syntax

if Boolean_condition:
    #code blocks
elif Boolean_condition:
    #code blocks
else:
    #code blocks

Here is an example

first = 10
second = 15
if first > second:
    print(" First is greater than Second");
elif second> first:
    print(" Second is greater than First ");
else:
    print(" First is equal to Second");