For loop is an essential looping construct in all programming languages. Python too provides this loop to iterate through elements of arrays, lists, strings and other collections. In this post you will be learning about Using For Loop in Python with the help of multiple examples.
Note: Python allows blocking of statements by indentation. All statements at same indent belong to a block of statements
Syntax of For Loop in Python
You can use for loop in these two ways though the working of for loop is same in both cases.
Method 1
In first method of Using For Loop in Python you have to specify a variable-name that will take one value from the array-name specified, in each iteration. for loop will terminate after all the elements of the array has been used .
for variable-name in array-name:
statements
Method 2
In second method you have to specify a variable-name that will take one value from the range of values specified with the integer-number in parenthesis. It is the counter in each iteration. for loop will terminate after all the elements of the array has been used .
for counter-name in range(integer-number):
statements
Examples-Using For Loop in Python
Example 1- Using range Function with Array
In this example an array names ‘Fruits’ of string is created. In for loop variable i gets values from 0 to 6 from range(len(Fruits)) where range functions generates a range 0-6 using the count 7 returned by len function. len function returns the number of elements present in the declared array.
Fruits=('Apples','Pineapple','Grapes','Papaya','Banana','Orange','Mango');
for i in range(len(Fruits)):
print(Fruits[i]);
Example 2- Using For Loop in Python with Array storing elements in Loop Variable
In this example the for loop variable uses variable Fru to store one element from array Fruits in each iteration. This accessed value in Fru variable can be used as desired. Here we have displayed it using the print function.
Fruits=('Apples','Pineapple','Grapes','Papaya','Banana','Orange','Mango');
for fru in Fruits:
print(fru);
Example 3 – With Python List
In this example the for loop variable uses variable Fru to store one element from Python List Fruits in each iteration. This accessed element of List is stored in Fru variable. It is displayed using the print function.
Fruits=['Apples','Pineapple','Grapes','Papaya','Banana','Orange','Mango'];
for fru in Fruits:
print(fru);
Example 4 – With Python String
In this example the for loop uses variable i to store index taken from the range of indexes defined by range function used with name of string variable StartStr. In each iteration of for loop print function combines ith element of StartStr string and EndStr to generate a three lettered word ending in ‘at’.
StartStr='BCFHMPRS'
EndStr='at'
for i in range(len(StartStr)):
print(StartStr[i]+EndStr)
Here we have demonstrated Using for Loop in Python with examples with array, list and string. You can try your hands on similar kind of problems where the repetitive tasks are to be done.
Try your hands to write a program where you combine three strings to make different words with alphabets at same index in three string variables.
Str1=’ABCDEFGH’
Str2=’peaigaeu’
Str3=’etpggntt’
Output must be as shown in image below
Share your code with us in comments section!!!
great