For-loop-in-Python .

In this session i have discussed for loop in Python .


The for loop also a entry control loop in Python .

For Loop Syntax :

    for variable in range(lowerlimit,upperlimit):
        loop body
    
    

Example : Write a python program that print the value 1-10 using for loop .

for i in range(1,11):
    print(i,end=" ")
    
   OUTPUT : 1 2 3 4 5 6 7 8 9 10
    

Example : Write a python program that print the table of 2 using for loop .

for i in range(1,11):
    t = i * 2;   
    print(t,end=" ")
                
    
    OUTPUT : 2 4 6 8 10 12 14 16 18 20
    

Example : Write a python program that print the even number using for loop .

for i in range(1,11):
    if (i % 2 == 0):
        print(i,end=" ")
    
    OUTPUT : 2 4 6 8 10
    

Example : Write a python program that print the odd number using for loop .

for i in range(1,11):
    if (i % 2 != 0):
        print(i,end=" ")
                 
    
    OUTPUT : 1 3 5 7 9
    

Example : Write a python program that print the following triangle using for loop .

for i in range(1,6):
    for i in range(1,i+1):
        print("*",end=" ")
    print("\n")            
    
     OUTPUT :   *
                * *
                * * *
                * * * *
                * * * * *
    

Example : Write a python program that print the following number triangle using for loop .

for i in range(1,6):
    for j in range(1,i+1)
        print(j,end=" ")
    print("\n")
    
     OUTPUT :   1
                1 2
                1 2 3
                1 2 3 4
                1 2 3 4 5
    

Example : Write a python program that print the following number triangle using for loop .

for i in range(1,6):
    for j in range(1,i+1):
        print(i,end=" ")
    print("\n")            
                
    
     OUTPUT :   1
                2 2
                3 3 3
                4 4 4 4
                5 5 5 5 5
    

Example : Write a python program that print the following triangle using for loop .

k = 8
for i in range(0, 5):
    for j in range(0, k):
        print(end=" ")
    k = k - 2
    for j in range(0, i+1):
        print("* ", end="")
    print()
    
     OUTPUT :        *
                   * * 
                 * * * 
               * * * * 
             * * * * *
    

Example : Write a python program that print the following number triangle using for loop .

 
k = 8
m=1
for i in range(0, 4):
    for j in range(0, k):
        print(end=" ")
    k = k - 2
    for j in range(0, i+1):
        print(m, end=" ")
        m=m+1
    print()
             
         
    
     OUTPUT :          1
                     2 3
                   4 5 6
                 7 8 9 10
    

Example : Write a java program that print the following number triangle using for loop .

k = 8
m=1
for i in range(0, 4):
    for j in range(0, k):
        print(end=" ")
    k = k - 2
    for j in range(0, m):
        print("*", end=" ")
    m=m+2
    print("\n")
       
    
     OUTPUT :        *
                   * * *
                 * * * * *
               * * * * * * *
    

Welcome to AndroidATPoint.

back image                     next image