Python - Loops

Using conditional statements, the statements execute sequentially and ends at last statement if the condition is true or false.
Using Loops, we can execute a set of statements multiple times as long as the condition true.

Python has two types of loops.

  1. While loop
  2. For loop

While loop

While loop is used to execute the multiple statements repeatedly until the given condition is true.

Example:

    #Initialise the variables
    n = 5
    i = 1
    sum = 0
    
    while ( i <= n):
        sum = sum + i
        i = i + 1
    print("The Sum value is:", sum)

Output

    The Sum value is : 15
For loop

The for loop is used to execute the multiple statements repeatedly until the given condition is false. The for loop is used to iteratie over the list, tuple, and other objects.

Example:

    Countries = [‘India’, ‘US’,’UK’,’France’]
    for country in countries:
        print(“Country name is - ”+country)

Output

    Country name is - India
    Country name is - US
    Country name is - UK
    Country name is - France


Related Tutorials