Python If-Else

Python If-Else is used in decision making. Decision making is the most important aspect of all programming languages because sometimes the programmer may want to execute some block of code only on some specific condition which he/she may not know in advance.

There are three keywords to execute a block of code according to some conditions.

"if" , "elif" and "else"

You can use "if" keyword alone while "elif" and "else" needs “if” statement to work.

If statement

Syntax

if condition:
    # this code will be executed
    # if the condition is True

Note : Most of the languages uses curly-brackets to define some block of code while Python uses indentation(white spaces(usually 4) at the beginning of a line) and here we are using indentation for if statement.

Read this to know more about indentation.

Example

a = 10
b = 20
if b > a:
    print("b is greater then a")

Output

b is greater then a

Indentation error

Example

a = 10
b = 20
if b > a:
print("b is greater then a")  # indentation error

Output

  File "f:/vscode/python/if-else.py", line 4
    print("b is greater then a")
        ^
IndentationError: expected an indented block

If….elif… statement

To use "elif" statement we should have "if" statement before elif statement.

Syntax

if condition1:
    # this code block will get executed
    # if condition1 is True
elif condition2:
    # this code block will get executed
    # if condition2 is True

Example

a = 10
b = 20
if a > b:
    print("a is greater then b")
elif b > a:
    print("b is greater then a")

Output

b is greater then a

You can simply use any number of elif statements.

Example

a = 10
b = 10
if a > b:
    print("a is greater then b")
elif b > a:
    print("b is greater then a")
elif b == a:
    print("b is equal to a")

Output

b is equal to a

If…else… statement

If all of the above contitions gets failed and we want that at least one block of code should be executed then at that place we apply "else" statement which needs if statement to work.

Syntax

if condition:
    # some code 
else:
    # there can be any number of if.. elif.. statement
    # but if all the conditions applied to them fails
    # then this block of code will get executed

Example

a = 10
b = 10
if a > b:
    print("a is greater then b")
else:
    print("Above condition is not True")

Output

Above condition is not True

Else statement can be used with elif statement also and there can be any number of elif statements but else statement will get executed only if all the conditions which have been applied with if…elif… statements are False.

Example

a = 10
b = 10
if a > b:
    print("a is greater then b")
elif b > a:
    print("b is greater than a")
else:
    print("None of the conditions are True")

Output

None of the conditions are True

Let’s see one more example.

Example

a = 10
b = 10
if a > b:
    print("a is greater then b")
elif b > a:
    print("b is greater than a")
elif a > b or b > a:
    print("a is greater than b")
    print("or b is greater than a")
else:
    print("None of the conditions are True")

Output

None of the conditions are True