For any queries you can reach us at / WhatsApp us: +919158876092

Python Control Statements

Control Statements

Control statements are used to control the flow of execution depending upon the specified condition/logic.

  1. if- Statement

    An if statement is a programming conditional statement that, if the condition is true, it executes the block of code inside of it.

    if- Statement
    books = 2  
    if (books == 2) :
       print(" You have")  
       print("Two books")  
    print("Outside of if statement") 

    In the above code, we are checking if books is equal to 2, then execute the given code.

  2. if-else statement

    The if-else statement executes some code if the test expression is true (non-zero) and some other code if the test expression is false.

    if-else Statement
    if a<100:   
          print('less than 100')  
          print('more than equal 100')  

    In the above code, we are checking if a is less than 100, else will print "more than equal 100'.

  3. Nested If-Else Statement

    The nested if ... else statement allows you to check for multiple test expressions and executes different codes for more than two conditions.

    num = float(input("enter a number:"))  
    if num >=0:  
         if num==0:  
         print("Negative number")  

    In the above code, we are first checking if num is greater than or equal to 0 and then if it is zero if both the conditions are met, "zero" is printed, otherwise "Negative number".