What is Loop in python?

A loop statement allows us to execute a statement or group of statements multiple times.

Loops are used to repeat a specific block of code.

File name : index.py

  • While loop
  • do-while loop
  • for loop
  • nested loop

  • while loop

    The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.

    We generally use this loop when we don't know the number of iterations then the while loop is most effective to use.

    File name : index.py

    while expression:
        statements

    Example

    File name : index.py

    n = 10

    # initialize sum and counter
    sum = 0
    i = 1

    while i <= n:
    sum = sum + i
    i = i+1 # update counter

    # print the sum
    print("The sum is", sum)

    Example

    File name : index.py

    # prints all letters except 'a' and 't'
    i = 0
    str1 = 'javatpoint'

    while i < len(str1):
    if str1[i] == 'a' or str1[i] == 't':
    i += 1
    continue
    print('Current Letter :', a[i])
    i += 1

    File name : index.py

    # The control transfer is transfered
    # when break statement soon it sees t
    i = 0
    str1 = 'javatpoint'

    while i < len(str1):
    if str1[i] == 't':
    i += 1
    break
    print('Current Letter :', str1[i])
    i += 1

    print 1 to 10 using while loop

    File name : index.py

    i=1
    #The while loop will iterate until condition becomes false.
    While(i<=10):
    print(i)
    i=i+1

    Program to print table of given numbers

    File name : index.py

    i=1
    number=0
    b=9
    number = int(input("Enter the number:"))
    while i<=10:
    print("%d X %d = %d \n"%(number,i,number*i))
    i = i+1

    Using else with while loop

    Python allows us to use the else statement with the while loop also. The else block is executed when the condition given in the while statement becomes false. Like for loop, if the while loop is broken using break statement, then the else block will not be executed, and the statement present after else block will be executed. The else statement is optional to use with the while loop.

    File name : index.py

    i=1
    while(i<=5):
    print(i)
    i=i+1
    else:
    print("The while loop exhausted")

    For Loop

    A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).

    File name : index.py

    str = "Python"
    for i in str:
        print(i)


    Output :-
    P
    y
    t
    h
    o
    n

    for loop

    File name : index.py

    # Program to find the sum of all numbers stored in a list

    # List of numbers
    numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]

    # variable to store the sum
    sum = 0

    # iterate over the list
    for val in numbers:
    sum = sum+val

    print("The sum is", sum)

    Example

    File name : index.py

    list = [1,2,3,4,5,6,7,8,9,10]
    n = 5
    for i in list:
    c = n*i
    print(c)

    Output :-

    5
    10
    15
    20
    25
    30
    35
    40
    45
    50

    Nested for loop in python

    Python allows us to nest any number of for loops inside a for loop. The inner loop is executed n number of times for every iteration of the outer loop.

    File name : index.py

    for iterating_var1 in sequence: #outer loop
        for iterating_var2 in sequence: #inner loop
        #block of statements
    #Other statements

    File name : index.py

    # User input for number of rows
    rows = int(input("Enter the rows:"))
    # Outer loop will print number of rows
    for i in range(0,rows+1):
    # Inner loop will print number of Astrisk
    for j in range(i):
    print("*",end = '')
    print()





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here