Python Conditional Statements

Table Of Contents

  1. What Is Python Conditional Statement?
  2. Types Of Conditional Statement.
  3. if statements
  4. if-else statements
  5. if-elif ladder
  6. Nested statements

(1) What Is Python Conditional Statements?

  • When our program executes, it will run line by line.
  • Suppose, you want to execute some set of codes based on some condition, you can use control flow statements.

  • Decision-making statements in programming languages decide the direction of the flow of program execution.
  • In the above picture, you can see that if there is water on the road, you will not walk on that path.
  • So in python, you can also implement this logic by using conditional statements.

(2) Types Of Conditional Statements?

  1. if Statements.
  2. if-else Statements.
  3. if-elif Statements.
  4. Nested Statements.

(3) If Statement

  • Python if Statement is used for decision-making operations.
  • It contains a body of code that runs only when the condition given in the if statement is true.
  • If the condition is false, then the optional else statement runs .

Python If Statement Syntax:

if Condition:
   Body Of If Block

Example-1

mark = 60
if mark >= 60:
   print("Pass")
Output:
Pass

Example-2

company = "Nokia"
price = 10000

if company=="Nokia" and price <= 10000:
    print('Buy')
Output:
Buy

(4) If-else Statement

  • Suppose you have two possible options to execute, if the first option is not satisfying then you need to run the second option.
  • In that case, you can use an if-else statement.

Python If Statement Syntax:

if Condition:
   Body Of If Block
else:
   Body Of If Block

Example-1

number = 10
if number % 2 == 0:
    print("Even Number")
else:
    print("Odd Number")
Output:
Even Number

Example-2

grade = 'A'
experience = 5
if grade == 'A' and experience>=5:
    print('Eligable')
else:
    print('Not Eligable')
Output:
Eligable

(5) If-elif Statement

  • When you have more than two conditions to check, then you can use if-elif statement.
  • It allows us to implement multiple conditions in our programming.

Python If Statement Syntax:

if Condition:
   Body Of If Block
   
elif Condition:
   Body Of elif Block
   
elif Condition:
   Body Of elif Block
   
......

else:
   Body Of else Block

Example-1

num = -10
if num > 0:
    print("Positive number")
elif num == 0:
    print("Zero")
else:
    print("Negative number")
Output:
Negative Number

Example-2

country = 'India'
if country == 'India':
    print('Democratic Country')
elif country == 'China':
    print('Non Democratic Country')
elif country == 'USA':
    print('Democratic Country')
else:
    print('Not In The List')
Output:
Democratic Country

(6) Nested If Statement

  • A Nested IF statement is one in which an If statement is implemented inside another If statement.
  • This is used when a variable must be processed more than once.
  • If, If-else, and If…elif…else statements can be used in the program.
  • In Nested If statements, the indentation (whitespace at the beginning) to determine the scope of each statement should take precedence.

Python Nested If Statement Syntax:

if (condition 1):
    #Executes if condition 1 is true
    if (condition 2):
        #Executes if condition 2 is true
        #Condition 2 ends here
#Condition 1 ends here
elif (condition 3):
    #Execute this if condition 3 is true
    if (condition 4):
        #Execute this if condition 4 is true
    if (condition 5):
        #Execute this if condition 5 is true
else:
    #Execute this block if non of the condition satisfy

Example-1

num = 8
if num >= 0:
    if num == 0:
        print("zero")
    else:
        print("Positive number")
else:
    print("Negative number")
Output:
Positive Number

Example-2

price=100
quantity=10
amount = price*quantity
if amount > 200:
    if amount >1000:
        print("The amount is greater than 1000")
    else:
        if amount  800:
            print("The amount is between 800 and 1000")
        elif amount  600:
            print("The amount is between 600 and 1000")
        else:
            print("The amount is between 400 and 1000")
elif amount == 200:
    print("Amount is 200")
else:
    print("Amount is less than 200")
Output:
 “The amount is between 400 and 1000.”

Leave a Reply

Your email address will not be published. Required fields are marked *