Hello dear readers! Welcome back to another edition of our tutorial on Python. In this tutorial guide, we are going to be discussing about the Python for Loop Statements.
For Loop statement has the ability to iterate over the items of any of the sequence, such as a list or a string.
For Loop statement has the ability to iterate over the items of any of the 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 holds 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 is going to be executed until entire sequence is exhausted.
Flow Chart
RECOMMENDED: Python While Loop Tutorial with examples
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 means which can be used to iterate 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 help of the len() built-in function, that provides the total number of elements in the tuple and range() built in function to give us the actual sequence to be iterated over.
RECOMMENDED: Loop Control Statements in Python Programming
Using else Statement with Loops
Python has support for 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, then the else statement is going to 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 our 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.
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.