Python Conditional Statements|(if,if-else,elif) with Example

0
Python Conditional Statements|(if,if-else,elif) with Example
Python Conditional Statements|(if,if-else,elif) with Example 

📑 Table of Contents

Python Conditional Statements Definition

Conditional Statement in Python: A conditional statement can be using decision making control system. It can be used condition are satisfied or not satisfied. If the given condition are true and given condition are false than return compiler are false. Conditional statements in python are used to perform different actions based on whether a certain condition is true or false. They allow you to control the flow of your program by executing specific blocks of code depending on the your program conditions. The most common conditional statements in Python are `if`, `elif`, and `else`.

If Statement: This statement is used to test a condition. If the condition evaluates to true, the block of code inside the `if` statement is executed.

Elif Statement: This stands for "else if" and is used to test multiple conditions. If the first `if` condition is false the program checks the `elif` condition. If it is true the individual block of code is executed.

Else Statement: This statement is used to execute a block of code when if conditions is false than execute else.

Read C Conditional Statement

Types of Decision Making in Management

  • If Statement in Python
  • If-Else Statement in Python
  • Elif Statement in Python
  • Nested If Statement in Python

Decision Making With If Statement

If Statement: If the given condition is true than if body will be executed or given condition are false than no if body will be executed.  It is used to test the condition and the result is based on the condition. If the condition is true than if statement executed or if condition is false than statements are not executed. Python if statement is a conditional statement that allows you to execute a block of code only if a specified condition is true. The basic syntax of an if statement in Python is as follows:

Syntax of If Statement in Python

if condition:
    Statements;

Explain If Statement in Python

age=18;
if age>=18:
        print("Your age is 18 this statement are executed");

Example of If Statement in Python

age=int(input("Enter Your Age:"))
if age>=18:
    print("Your Age 18 You can derive a car")
============OUTPUT============
Enter Your Age:18
Your Age 18 You can derive a car

Check Given No is Positive or Negative in Python

age=int(input("Enter any Number:"))
if n>0:
    print("You entered a Positive Number:",n)
if n<0:
    print("You entered a Negative Number:",n)
if n==0:
    print("You entered Zero:",n)

============OUTPUT============
Enter any Number:18
Your Age 18 You can derive a car

Definition of If Else Statement in Python

If Else Statement: A if-else statemen can be used two condition are applied if condition are true and if statement/ body will be executed or if condition is false than else statement will be executed. It can be used to test the condition and gives the result in both situation either condition is true and false. Python if-else conditional statement is used to execute a block of code based on whether a specified condition is true or false.

Syntax of If Else Statement in Python

if condition:
    Statements
else:
    Statements

Explain If Else Statement With Syntax

age=18;
if age>=17:
    print("Your age is 18 this statement are executed")
else:
    print("Your age is less than 18 this statement are executed")


Example of If Else Statement in Python 

age=int(input("Enter Your Age:"))
if age>=18:
    print("Your Age 18 You can derive a car")
else:
    print("Your Age is below 18 You can not derive a car")
============OUTPUT============
Enter Your Age:17
Your Age is below 18 You can not derive a car

Definition of If Elif Else Statement in Python

Elif Statement: A python elif statement can be used where multiple condition are applied that use else if statement. It is a part of decision making  that execute only one condition at a time. If all condition is false than else part will be execute. Python elif statement is a conditional statement that allows you to execute different blocks of code based on multiple conditions. It is used when you have more than two possible paths to take based on the value of a variable or expression. Here is the syntax for using elif in Python:

Syntax of Else If  Ladder in Python

if condition1:
    # Code to execute if condition1 is True
else if condition2:
    # Code to execute if condition2 is True
else if condition3:
    # Code to execute if condition3 is True
else:
    # Code to execute if none of the above conditions are True

Explain Else If Statement With Syntax

int age=18;
if age>=18:
    cout<<"Condition A Execute";
else if(Condition B):
    cout<<"Condition B Execute";
else if(Condition C):
    cout<<"Condition C Execute";
else:

    print("if not matched Condition Than Execute Else")


Example of Else If Ladder in Python

per=int(input("Enter Your Percentage:"))
if per<=100 and per>=80:
    print("You have A+ Grade")
elif per<80 and per>=60:
    print("You have A Grade") 
elif per<60 and per>=50:
    print("You have B Grade")
elif per<50 and per>=35:
    print("You have C Grade")
else:
    print("You are Fail")
============OUTPUT============
Enter Your Percentage:61
You have A Grade

Nested If Definition in Python

Nested If Statement: A python nested statement can be used one inside if to another if that is called nested if statement. Nested if statement can be used multiple inside if condition but only one condition execute at a time. Nested if in python can be used to check multiple conditions in a hierarchical manner. Here is an example of how to use nested if statements in Python.

Syntax of Nested If in Python

if condition1:
    Statements
    if condition 2:
         Statements
        if condition 3:
             Statements

Explain Nested If Statement With Syntax

if (x>y):
      statements
    if(x>y):
        statements

Nested if Program in Python

a=int(input("Enter a number: "))
b=int(input("Enter b number: "))
c=int(input("Enter c number: "))
if a>b:
    if a>c:
        print("Number a is Largest: ",a)
if b>a:
    if b>c:
        print("Number a is Largest: ",b)
if c>a:
    if c>b:
        print("Number a is Largest: ",c)

============OUTPUT============
Enter a number: 30
Enter b number: 50
Enter c number: 20
Number b is Largest:  50

Nested if Program Example in Python

def check_number(n):
        if n > 0:
           print("The number is positive.")
           if n % 2 == 0:
                print("The number is also even.")
           else:
                print("The number is odd.")
        elif n < 0:
            print("The number is negative.")
            if n % 2 == 0:
                print("The number is also even.")
            else:
                print("The number is odd.")
        else:
            print("The number is zero.")
check_number(10)

============OUTPUT============
The number is positive.
The number is also even.

Conclusion of Conditional Statement in Python

Conclusion: This is all about python conditional statement it is very most important features of every programming languages. It can be used condition creaking true and false. I can explain every conditional statement in this article if your any question or query you can contact me by email or comments. I can provide solution simple and easy way.   

Post a Comment

0 Comments
Post a Comment (0)
To Top