Your ultimate guide

Showing posts with label Python Notes. Show all posts
Showing posts with label Python Notes. Show all posts

Python Control Statements



Control Statements:
    There are 3 types of control statements:
  1. Conditional Statements
    1. if
    2. if - else
    3. if - elif - else
    4. Nested if
  2. Looping Statements/Iterative Statements
    1. while
    2. for
  3. Jumping Statements/Transfer Statements
    1. break
    2. continue
    3. pass
    Conditional Statements
        1) if  -  When the given condition is true, then the block of statements executes
            Syntax:
                if condition :
                        statement1
                        statement2
                        -
                        -
            Indentation: Indentation is the leading whitespace (spaces or/and tabs) before any statement in Python. Other programming languages often use curly brackets "{ }" for this purpose.
            Syntax 2:
                if condition :
                        statement 1
                        statement 2
                statement 3

            (Here statement 3 is not in the if statement)   

            Example:
                1)    if 10<2 :
                2)        print("Hello")
                3)    print("Testing Colleges")
                output:
                >>> Testing Colleges

        2) if-else    - When the given condition is true, then it executes if-block statements. if the condition is false then else-block statements will be executed.
            Syntax:
                if condition :
                        statements 1
                else :
                        statements 2
            (If the condition is true then statement 1 will be executed, if the condition is false then statement 2 will be executed).

            Example:
                a = 10
                if a%2==0 :
                        print("Even number")
                else :
                        print("Odd number")

                Output: Even number

        3) if - elif - else
                When we have multiple conditions, then we need to give as elif expressions.
            Syntax:
                if condition 1 :
                        Statement 1
                elif condition 2 :
                        Statement 2
                elif condition 3 :
                        Statement 3
                - - -
                else :
                        Statement

            Example:
            1)    a,b = 10,20
            2)    if a>b :
            3)        print("a is big")
            4)    elif a<b :
            5)        print("b is big")
            6)    else :
            7)        print("a and b both are same")
            Output: b is big

        4) Nested if    - if statement having another if-block, then it can be called Nested if.
                Syntax:
                    if condition 1 :
                            if condition 2 :
                                    Statement 1
                            Statement 2
                    else :
                            Statement 3

    Looping Statement:
        1) while loop:
            while loop is used to execute a group of statements iteratively until some condition becomes false.
            Syntax:
            while condition :
                    statement 1
                    statement 2
                    - - -
            Example:
                1) num = 1
                2) while num<5 :
                3)        print(num)
                4)        num +=1
            Output:    
                    1
                    2
                    3
                    4

            Example 2: (infinite loops)
                1) i = 1
                2) while True :
                3)    print(i, "Hello")
                4)    i += 1

        2) for loop:
            for loop is used to execute some action for every element present in some sequence (it may be a string or collection).
            Syntax:
            for value in sequence :
                    statements
                    - - -
            Example:
                1) name = "Test"
                2) for x in name :
                3)         print(x)
            output:
                    T
                    e
                    s
                    t

            Example 2:
                1) names = ["apple", "boy", "cat", 4 , True]
                2) for x in names :
                3)         print(x)
            output:
                apple
                boy
                cat
                4
                True

        
        range() function:
            The range() function returns a sequence of numbers, starting from 0 (by default), and increments by 1 (by default), and stops before a specified number.
            Syntax:
                range(start, stop, step)
            Example 1: To print Hello 10 Times
            1) for x in range(10) :
            2)        print(x)

            Example 2: To display numbers from 0 to 10
            1) for x in range(11) :
            2)        print(x)

            Example 3: To display odd numbers from 10 to 20
            1) for x in range(10, 20) :
            2)         if(x%2!=0) :
            3)                print(x)

            Example 4: To display numbers from 10 to 1 in descending order
            1) y = range(10, 0, -1)
            2) for x in y :
            3)        print(x)

    Jumping Statements:
        1) break:
            We can use break statements inside loops to break loop execution based on some condition.
            Example:
            1) for x in range(10) :
            2)         if x=5 :
            3)                print("terminated")
            4)                 break
            5)        print(x)
            output:
            0
            1
            2
            3
            4
            terminated 

        2) continue:
            We can use continue statement to skip current iteration and continue next iteration.
            Example: To print even number in the range 0 to 9
            1) for x in rang(10) :
            2)         if x%2==1 :
            3)             continue
            4)         print(x)
            output:
            0
            2
            4
            6
            8

        3) pass:
            pass statement acts as a placeholder statement that does nothing, used to represent an empty block of code or as a temporary placeholder for code that will be added later.
    (It is an empty statement, it is null statement and it won't do anything.)
            Example:
            1) for i in 'hello' :
            2)         if(i == 'l') :
            3)                 pass
            4)         else :
            5)                 print(i)
            output:
            h
            e
            o