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



Read More:


Make money with Python in 2022

Did you know that data science was the fifth most common job on LinkedIn in 2017? Did you know that the demand for Python programmers is expected to grow by 19% from 2020-2021? Did you know that Python is one of the fastest-growing programming languages, and it will likely continue to expand as its adoption…

Continue Reading Make money with Python in 2022

Top 15 Python Libraries For Machine Learning in 2022

Introduction  In today’s digital environment, artificial intelligence (AI) and machine learning (ML) are getting more and more popular. Because of their growing popularity, machine learning technologies and algorithms should be mastered by IT workers. Specifically, Python machine learning libraries are what we are investigating today. We give individuals a head start on the new year…

Continue Reading Top 15 Python Libraries For Machine Learning in 2022

Team Points in Python | Assignment Expert

Problem Statement: This is IPL and matches are going on. You are a programmer who writes programs to output summarized charts of matches already played.You will be given some input and you need to print a summarized chart in descending order of points. Points criteria->Match won-> 2Draw-> 1Loss-> 0 For exampleInput:4RR-RCB-lossMI-KKR-lossCSK-RR-winGT-RCB-drawOutput:Team: RR | Total Matches:…

Continue Reading Team Points in Python | Assignment Expert