Loops in Python:-

The Python programming language provides the following types of loops to handle looping requirements.

Python offers three ways to execute the loop. While all methods provide the same basic functionality, but they differ in their syntax and status check times.

Python While Loop:-

In python while loop, expression is checked first.In while loop the body of the loop is entered only if the expression evaluates to True means while loop is used to execute a block of statements repeatedly until a given a condition is satisfied. After one iteration, the expression is checked again and again until the condition becomes false.

Syntax:-

while expression: Body of while statement(s)

In Python, After a programming construction, all the statements given by the letters spacing of the same number are considered part of the same block of code.While in the Python the body of the While loop is determined through indentation. The body starts with indentation and the first unindentated line indicates the end.

Python While Loop Example:

#Program to add sum of 10 Integers #1+2+3+..+10 num = 0; sum = 0 while (num <= 10): sum = sum + num num = num + 1 else : print('The sum of first 10 integers : ',sum)

Output:-

Python for Loop :- For loop are used for sequential traversal. For in loops is used to iterate over a sequence list, tuple, string etc. or other iterable objects. Syntax: for val in sequence: Body of for statements Example: Python for Loop # Program to find the sum of numbers # List of numbers numbers = [1, 2, 3, 4, 5, 6,7,8,9,10] # variable to store the sum sum = 0 # iterate over the list for var in numbers: sum = sum + var print('The sum of first 10 integers : ',sum) # Output: The sum of first 10 integers: 55

After execution of program, the output will be:-