Wednesday, June 17, 2020

For Loop Statements in Python



Hello guys! Welcome back to another section of my tutorial on Python. In this tutorial guide, we are going to be discussing about the Python for Loop Statements.

The for Loop statement has the ability to iterate over the items of any of sequence, such as a list or a string.

Syntax

The syntax for a Python for Loop statement is as follows -

for iterating_var in sequence:
   statements(s)

If a sequence contains an expression list, then it is first evaluated. The first item in the sequence would then be assigned to the iterating variable iterating_var. Next, the statements block will be executed. Each item in the list is going to be assigned to the iterating_var, and the statement(s) block will be executed until the entire sequence is exhausted.

Flow Chart



Example

Below is a very short example -

#!/usr/bin/python

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

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

print "Welcome to Web design tutorialz!"

Output

The following below is the output of the above example -

Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : h
Current Letter : o
Current Letter : n
Current fruit : banana
Current fruit : orange 
Current fruit : apple
Welcome to Web design tutorialz!

Iterating by Sequence Index 

An alternative way which can be used for iterating through the items is by index offset into the sequence it self.

Example

The following below is a simple example -

#!/usr/bin/python

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

print "Welcome to Web design tutorialz!"

Output

The following below is the output of the above example -

Current fruit : banana
Current fruit : orange
Current fruit : apple
Welcome to Web design tutorialz!

Here, we took the assistance of the len() built-in function, which will provide the total number of elements in the tuple as well as the range() built in function to give us the actual sequence to be iterated over.


Using else Statement with Loops

Python Lang. supports the feature of having an else statement associated with a loop statement.

  • If the else statement is used with a for loop, the else statement will be executed when the loop has finished iterating the list.
  • If the else statement is used with a while loop, the else statement will be executed as soon as the condition returns false.

Example
The example that we have  below illustrates the combination of an else statement with a for loop statement that searches for the 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'

Output
The following below is the output of the above example -

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

Alright guys! This is where we are rounding up for this tutorial. In my next tutorial, we will be discussing about the Python nested Loops.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.