for loop in python

A for loop is used for iterating over sequence(i.e.  a list, tuple, dictionary, set, string).

Syntax of for Loop:f1

The for loop used in python is different from the for keyword used in other programming languages, and work more like iterator as found in other object-oriented programming languages.

Example:f2

>Even strings are iterable objects, they contain a sequence of characters, so on every string iteration can be done using for loop:f3

The break Statement

With the help of break statement we can stop the for loop when-ever we do not want the looping functionality (i.e. break statement will help to stop the loop before it has looped through all the items).

f4

The continue Statement

With the continue statement we can stop the current it

eration of the loop and continue with the next (i.e when-ever the for loop encounters the mentioned condition it will not print that condition and will proceed further):f5

The range() Function

To loop through a set of code a specified number of times, we can use the range() function, the range() function return a sequence of numbers starting from 0(default) and increments by one(default) and ends at 1 less than the provided range value.

f6

f7

 

>>All the Nested loop property can be used for for loop.

Leave a comment