What is loop in python programming?
In computer programming, In some situations we may require to execute certain code repeatedly. Looping simply means execution of specific code again and again till the provided condition gets satisfied. Python for loop is one of the most widely used loop statement.
Typically Loop in python programming contains code blocks and test condition. During execution of program code blocks are first executed, Then the condition is verified. If the condition is not satisfied then same code block is executed again. Eventually when condition gets satisfied the loop is exited.
There are different types of looping statements in python. In this article we are mainly going to focus on python For Loop.
For loop in python:
Python For loop is most widely used loop statement. It is used when the number of code repetitions/iterations are fixed. When we know the number of code repetitions before actual execution then in that case for loop is used.
For loop in python is used to iterate over elements of sequence data type.
- Following seven sequence data types available in python:
- Unicode strings
- byte arrays
- xrange objects.
We can iterated python For Loop over elements of above mentioned data types. Click on following link to learn more about python data types in detail.
Python For Loop flowchart:
Syntax of python for loop:
for i in "sequence": execute for block
In above syntax “i” is the variable which takes the value of each sequence element for each iteration (You can use any variable name other than “i”). Execution of for loop continues till the iteration reaches to the last sequence element.
Python for loop execution steps:
- Firstly it checks if this is the last element of sequence? If it is not the last sequence element then element value is stored in variable “i”.
- At this steps for block gets executed. The code which is written inside for statement is also known as “for Block”. For block is separated from other code by using the indentation.
- Now it again go back and check the next sequence elements whether it is the last element or not. If it is not the last element then second element value is now gets stores in variable “i” and again for block get executed.
- This iteration continues till it reaches the last sequence element. When it reached the last sequence element for loop is exited.
Examples of python for loop:
- Python program for printing each character from word “Hello”.
word = "Hello" for character in word: print(character) #Result: H e l l o
In above example:
- In the first line we have created string variable named “word” and stored the string “Hello”.
- Second line, We have initiated for loop, Created new variable named “character”. This variable is going to store different characters of string for each iteration of for loop.
- Third line is the body of for block or the code we want to iterate. Here we are printing the value of variable “character”.
- So when the actual for-loop code execution starts it stores the first character of word “Hello” in “character” variable. (i.e character = “H”)
- Now the second line prints the value of “character” variable.
- After printing it again go back to initial stage and check for the next character of “Hello”. Again next character is stored in “character” variable and printed.
- This for loop iterates till it reaches to the last character of string “Hello” i.e “o”.
Python range() function:
This function in python is used to generate list of numbers in specified sequence.
- All the parameters given to the range() function must be integer.
- Function parameters can be positive or negative.
- List indexing is zero ‘0’ based indexing that means indexing starts at ‘0’ and not at ‘1’. Due to that end number is never be the part of list.
Suppose we want to print the numbers starting from 0 to 100. In this case if we go by traditional way then we’ll have to create a manual list of numbers from 0-100 written in it. So instead of writing complete list manually we can make use of python range() function. It is an inbuilt python function.
Example#1: Printing the list of numbers staring from 0 to 5.
Numbers_List = range(0, 6) for i in Numbers_List: print(i) #Result: 0 1 2 3 4 5
In above example we have provided the range from 0-6. As we know range function follows zero based indexing and due to which last input number of list is not considered as part of list. Now since we wanted to print list starting from zero ‘0’ and ending at 5 in above example we provided input to the list function as (0, 6)
Python range() function syntax:
range(start, stop, step size)
- start: The starting number of sequence.
- stop: Number up-to which we want to generate the list. Stop number will not be included in the list.
- Step size: Difference between two list numbers. This is the increment/Decrement while generating the list. Step size number can be positive or negative. If you don’t specify the step size its default considered value is ‘1’.
Example#2: Printing the even numbers between 0 to 10 using python range() function.
Numbers_List = range(0, 11, 2) for i in Numbers_List: print(i) #Result: 0 2 4 6 8 10
Since we have specified the step size as ‘2’. That’s why the list generated contains difference of 2 between two list numbers.
Example#3: Using python range function print the list of all the even numbers between 0 – 25 then do the sum of all the numbers.
Numbers_List = range(0, 26, 2) sum1 = 0 for i in Numbers_List: sum1 = sum1 + i print(i) print('Sum of all the list numbers is:', sum1) #Result: 0 2 4 6 8 10 12 14 16 18 20 22 24 Sum of all the list numbers is: 156
- Flow control statements in python (If…elif….else statement).
- List data type in python
- Number data type in python
Also you can get more help on python forum. Click Here