Python Flow Control Statement

These statements control the flow of execution of code i.e, they decide which Python instructions to execute next and under which conditions.

Flow Control statements can be categories as:

  1. Simple If
  2. If and Else
  3. Ladder if
  4. Nested If

Elements of Flow Control

In Python, flow control statements start with the condition and followed by a block of code.

  • Conditions: Every flow control statement uses a condition or test expression.
  • ○ All boolean expressions are considered as conditions.
    ○ Condition statements always return the result as True and False.

  • Blocks of Code: Lines of Python code grouped together in blocks called the clause.
  • ○ Unlike other programming like C, C++, Java which use braces { } to define a block of code, Python uses indentation.

  • Indentation: A block of code starts with indentation and ends with the first unindented line.
  • ○ You can also use tab in replacement of space one tab equal to four whitespace
    ○ There are three rules for blocks:

    1. Blocks begin when the indentation increases.
    2. Blocks can contain other blocks.
    3. Block ends when indentations will be in decreasing order.

Python Conditional Statements

  • When we write code their chances to raise condition where we need answer in True and False at that condition is called the conditional statement
  • The conditional statements provided by Python are:
  • if statement If statement contain the following syntax

    • If keyword
    • Condition
    • Colon
    if-else statement
    • If-else is optionally when if condition is not satisfied then it comes into the if -else clause.
    • Else part does not consist the conditions)

    Block diagram

    1. Simple if else Statement: In a simple if else statement there is only one condition that exists, if the condition will be true, then block if will be executed, otherwise the else part will be executed. Let’s see the flow chart of simple if else statement
    2. if else statement

    3. Ladder If Statement : In this loop there are more than two condition checked If the condition will not satisfy It check another conscious let’s see the flow chart of the ladder if statement
    4. if statement

    5. Nested If Statement : In this statement every condition consists of another condition whether It is True or False, let’s see the flow chart of the Nested if Statement.
    6. Nested if statement

    Example 1: Program to check whether a number is even and odd.

    user=int(input("Enter the Number"))
    if(user%2==0):
        print("Even",user)

    Output:

    ==== RESTART: C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/df.py ====
    Enter the Number45
    >>> 
    RESTART:C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/df.py
    Enter the Number44
    Even 44
    >>> 
    ======= RESTART: C:/Users/ASUS/AppData/Local/Programs/Python/Python 38-32/df.py ======
    Enter the Number40
    Even 40
    >>> 

    Example 2: Program to check whether a number is even or odd.

    user=int(input("Enter the Number"))
    if(user%2==0):
        print(user,"is even")
    
    else:
        print(user,"is odd")

    Output:

    ==== RESTART: C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/df.py ====
    Enter the Number56
    56 is even
    >>> 
    ==== RESTART: C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/df.py ====
    Enter the Number88
    88 is even
    >>> 
    ==== RESTART: C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/df.py ====
    Enter the Number45
    45 is odd
    >>>

    Example 3: Program to check the greatest of three numbers.

    ram=int(input("Ram"))
    shyam=int(input("Shyam"))
    mohan=int(input("Mohan"))
    if(ram>shyam):
        if(ram>mohan):
            print("Ram is oldest")
        else:
            print("Mohan is oldest")
    else:
        if(shyam>mohan):
            print("Shyam is oldest")
        else:
            print("Mohan is oldest")

    Output:

    ==== RESTART: C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/df.py ====
    Ram56
    Shyam54
    Mohan21
    Ram is oldest
    >>> 
    ==== RESTART: C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/df.py ====
    Ram55
    Shyam60
    Mohan33
    Shyam is oldest
    >>> 
    ==== RESTART: C:/Users/ASUS/AppData/Local/Programs/Python/Python38-32/df.py ====
    Ram60
    Shyam66
    Mohan70
    Mohan is oldest
    >>>

    Conditional Statement Program (Mini Project)

    import random
    name=input("Name : -")
    user=eval(input("Username "))
    pas=eval(input("Password "))
    wpas=eval(input("Re-Enter Password"))
    roll=random.randint(11,99)
    if(pas==wpas):
        print("Hello",name,"You have Successfully Created your roll is ",roll)
        print("*************************************************************")
        print("*****************Login******************************")
        luser=eval(input("Username or Roll Number : - "))
        lpass=eval(input("Password : - "))
        if(luser==user or luser==roll and lpass==pas):
            print("You Have Successfully Login\nName : ",name,"\t\tRoll mber",roll)
            print("\nPlease Enter your subject marks")
            hin=int(input("Hindi : "))
            eng=int(input("English : "))
            math=int(input("Math : "))
            sci=int(input("Science : "))
            com=int(input("Computer : "))
            total=hin+eng+math+sci+com
            per=total/5
            print("Total : ",total)
            print("Percentage :",per) 
            if(per>75):
                print("Grade : A+")
            elif(per<75 and per>60):
                print("Grade : A")
            elif(per<60 and per>50):
                print("Grade :B")
            elif(per<50 and per>45):
                print("Grade :C")
            else:
                print("Grade : Fail")
        else:
            print("Wrong information Provide")
    else:
        print("Password are not Match")
    

    Output

    Name : -Amit
    Username "amt"
    Password 1234
    Re-Enter Password1234
    Hello Amit You have Successfully Created your roll is  50
    *************************************************************
    *****************Login******************************
    Username or Roll Number : - 50
    Password : - 1234
    You Have Successfully Login
    Name :  Amit 		Roll number 50
    
    Please Enter your subject marks
    Hindi : 45
    English : 54
    Math : 33
    Science : 67
    Computer : 43
    Total :  242
    Percentage : 48.4
    Grade :C
    
Subscribe Now