Looping / Iterative Statements in Python

 

Control Structure in Python
Looping / Iterative Statements in Python

 

What is Loop?

In any programming language loop is a statement used to execute a certain block of code repeatedly until certain condition met.  A statement or group of statements are executed repeatedly until condition satisfy. Once condition becomes false loop iteration will stop.

In Python there are two types of loops

1.     for  Loop:

Normally for loop is used to iterate the elements in the sequence in Python.

Syntax,

for  variable in sequence:

          statement to be executed repeatedly

for keyword is used to write the for loop. Variable is the name of variable used for loop iteration. In keyword is used to check each element from given sequence i.e. in list, in tuple, in set, in dictionary. Sequence is the name of sequence.

For example,

my_list=["book1","book2","book3",1,2,3]

for element in my_list:

    print(element)

Output:

book1

book2

book3

1

2

3

In above program list is created with some random elements. For Loop is applied ove the list my_list. Element is loop variable to iterate loop until condition satisfy.

For Loop with range Method:

For loop is used with range method to give particular range to loop variable.

Syntax:

for variable in range(start,stop,step):

          statement to execute

Here for keyword is used to write for loop in keyword is used to give the range for the iteration. In range() function three parameters are passed start to start your loop from a particular value, end is nothing but to terminate the loop and step is nothing but after each iteration loop will be incremented or decremented by which value is given. This is followed by the colon. After colon the body of the loop has to followed by indentation. By default, 4 spaces are given before starting the loop statement.

For example,

sum=0

for i in range(1,11,1):

    sum=sum+i

print("The sum of first 20 natural numbers is:",sum)

Output:

The sum of first 20 natural numbers is: 55

Above program is written to perform sum of first 10 numbers. By using range function three parameters are passed i.e. loop will start from 1 it is start value. Loop will end on 11. So total 1 to 10 numbers will be taken for sum. Third value is again 1 i.e. step value. After each iteration loop variable, I will be incremented by 1. In this way we can use for loop to perform sum of natural numbers using range function

2.     The while Loop:

The while loop is used to execute statement or group of statements repeatedly but you don’t know initially how many iterations will be there. In advance if you don’t know how many iterations are there.

General syntax:

while variable testcondition:

              statement to execute

while keyword is used to write while loop. Variable is name of loop variable to iterate. Test condition is written to iterate the loop up to that number. Colon is part of syntax and then the while loop statement is written after following indentation.  The loop statement is executed repeatedly until test condition is true. Once condition becomes false loop iteration will stop.

For example,

i=1

while i<=10:

    print(i)

    i+=1

Output:

1

2

3

4

5

6

7

8

9

10

In above program the while loop is written to print first 10 natural numbers.  The I variable  is initialized with 1. In while loop test condition is written i<=10 means loop the statement written in while loop print(i) will be executed repeatedly until condition satisfy. Once compiler will get 11 loop iteration will stop. After each iteration loop will be incremented by 1.   

 

 

 

 

टिप्पणी पोस्ट करा

0 टिप्पण्या