× Python Introduction What is Python Python Features Python History Python Applications Python Install Python Path Python Example Execute Python Keywords Constant Variable Statements & Comments Python I/O and Import Operators UnaryBinaryTernary Unary Operators Unary Minus Binary Operators Arithmetic Operators Assignment Operators Relational Operators Logicaloperators Bitwise Operator Ternary Operators Control Statements in Python conditonal Statements IF if else Else If Nested if Switch For loop Nested For Loop While Loop Nested while Loop Unconditonal Statemets Continue Break Pass FUNCTIONS Python Function Function Argument Python Recursion Anonymous Function Python Modules NATIVE DATATYPES Python List Python Numbers Python Tuple Python String Python Set Python Dictionary OOPS PRINCIPALS Encapsulation Class Variable Method Object Or Instance CreationMethod Calling OOPS Syntax And Explanation DATA ABSTRACTION Constructor Inheritance 1.Single or simple Inheritance 2.Multilevel Inheritance 3.Hierarchical Inheritance 4.Multiple Inheritance 5.Hybrid Inheritance Operator Overloading File Operation Python Directory Python Exception Python - Multithreading Python - Database Access Python - CGI Python - Reg Exp Python - Date Python - XML Processing Python - GUI
  • iconPython Online Training In Andhra Pradesh and Telangana
  • icon9010519704

Opening Hours :7AM to 9PM

Looping Statements In Python


Looping Statements are used to execute a statement several number of times based on the condition.In Python language these are classified into following types.
1.For Loop
2.While Loop

for:
'for' is a keyword in python language which is used to execute one block of statements several number of times based on the condition.
Syntax
for variable in range('starting value','ending value','increment/decrement'):
        ...
        ...
        ...
In the above syntax :
increment/decrement is incrementation(+1) or decrementation(-1)

Key Points

  • What is For Loop
  • What is While Loop
  • What is Do While Loop


Image
Display 1 to 10 Values Using For Loop



def myfun():
    for x in range(1,10+1,1):
        print(x)

    
if __name__=="__main__":
    myfun()
                                

Output:
1
2
3
4
5
6
7
8
9
10

Display 10 to 1 Values Using For Loop


def myfun():
    for x in range(10,0,-1):
        print(x)

    
if __name__=="__main__":
    myfun()
                                

Output:
10
9
8
7
6
5
4
3
2
1

Nested For Loop In Python


Nested For Loop :
One for loop within another for loop is knows as Nested For Loop Syntax

for variable in range('starting value','ending value','increment/decrement'):
        for variable in range('starting value','ending value','increment/decrement'):
                ...
                ...
                ...
        ...
        ...
        ...
In the above syntax :
Outer loop is Row
Inner loop is Column

Key Points

  • What is For Loop
  • What is While Loop
  • What is Do While Loop


Image
***
***
***
***
***
***


def myfun():
    for x in range(1,7,1):
        for y in range(1,4,1):
            print("*",end="")
            
        print(end="\n")

    
    
if __name__=="__main__":
    myfun()
                                

Output:
***
***
***
***
***
***

*
**
***
****
*****
******


def myfun():
    for x in range(1,7,1):
        for y in range(1,x+1,1):
            print("*",end="")
            
        print(end="\n")

    
    
if __name__=="__main__":
    myfun()
                                

Output:
*
**
***
****
*****
******

******
*****
****
***
**
*



Row Decrement column increment
******
*****
****
***
**
*


def myfun():
    for x in range(6,0,-1):
        for y in range(1,x+1,1):
            print("*",end="")
            
        print(end="\n")

    
    
if __name__=="__main__":
    myfun()

...........................

Column Decrement row increment
******
*****
****
***
**
*

def myfun():
    for x in range(1,7,1):
        for y in range(6,x-1,-1):
            print("*",end="")
            
        print(end="\n")

    
    
if __name__=="__main__":
    myfun()

                                

Output:
******
*****
****
***
**
*