Python for Loop


Contents

It has the aptitude to iterate more than the entries of any series, such as a catalog or a string.

Syntax

for iterating_var in sequence:
   statements(s)

If a series holds a look list, it is assessed primary. Then, the primary item in the series is allocated to the iterating changeable iterating_var. Next, the declarations wedge is performed. Each thing in the list is allocated to iterating_var, and the statement(s) wedge is performed until the whole sequence is fatigued.

Flow Diagram

Example

#!/usr/bin/python

for letter in 'Python':     # First Example
   print 'Current Letter :', letter

fruits = ['banana', 'apple',  'mango']
for fruit in fruits:        # Second Example
   print 'Current fruit :', fruit

print "Good bye!"

When the above code is performed, it creates the subsequent result:

Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!

Iterating by Sequence Index

An option way of iterating during every item is by index make up for into the series itself. Subsequent is a easy example:

#!/usr/bin/python

fruits = ['banana', 'apple',  'mango']
for index in range(len(fruits)):
   print 'Current fruit :', fruits[index]

print "Good bye!"

When the above code is executed, it produces the following result:

Current fruit : banana
Current fruit : apple
Current fruit : mango
Good bye!

Here, we took the support of the len() built-in function, which supplies the total number of constituents in the Tuple as well as the range() fixed function to provide us the real series to iterate over.

Using else Statement with Loops

Python reinforces to contain an else statement connected with a loop declaration

  • Besides statement is utilized with a loop, the else report is performed when the loop has tired iterating the catalog.
  • Condition the else statement is adopted with a loop, to boot statement is performed when the state turned out to be false.

The subsequent example exemplifies the mixture of an else declaration with a statement that looks for prime numbers from 10 through 20.

#!/usr/bin/python

for num in range(10,20):     #to iterate between 10 to 20
   for i in range(2,num):    #to iterate on the factors of the number
      if num%i == 0:         #to determine the first factor
         j=num/i             #to calculate the second factor
         print '%d equals %d * %d' % (num,i,j)
         break #to move to the next number, the #first FOR
   else:                  # else part of the loop
      print num, 'is a prime number'

When the above code is executed, it produces the following result:

10 equals 2 * 5
11 is a prime number
12 equals 2 * 6
13 is a prime number
14 equals 2 * 7
15 equals 3 * 5
16 equals 2 * 8
17 is a prime number
18 equals 2 * 9
19 is a prime number

Here at Intellinuts, we have created a complete Python tutorial for Beginners to get started in Python.



Navigation
Tutorial
Tutorial
Tutorial