Python for loop is a counter controlled iteration. which you should know how many times you want the loop to execute.
1.Python For loop in range method
Syntax
for variable in range(start, stop, step):
# Code to be executed for each iteration
- variable: This is a variable that takes on the values generated by the range() function, one at a time.
- start (optional): The starting value of the sequence. If not provided, the default set to 0.
- stop: The stopping value of the sequence. This ends at a specified number. This is compulsory.
- step (optional): The step size, which determines the spacing between values in the sequence. If not provided, it defaults to 1.
Example 1
To print 1 to 5 numbers
for x in range(1,6):
print(x)
Explanation 1
- Here the starting value is specified by adding a parameter, range(1, 6)
- If not specified, the start value set to 0 by default
Output
1
2
3
4
5
Example 3
In order to get the same output Horizontally you should put “end” keyword with a delimiter.
for x in range(1,6):
print(x, end=”,”)
Output
1,2,3,4,5,
Example 5
To print 1 to 10 Even Numbers
for x in range(2,11,2):
print(x, end=”,”)
Explanation 5
- First Even number in the question is 2, So that you should set the start value as 2
- In order to get the 10 for the output, end value should be set to 11
Output 5
2,4,6,8,10,
Example 2
If we write following code as follows,
for x in range(6):
print(x)
Explanation 2
- Here the start value is not specified, so the start value set to 0 by default
Output
0
1
2
3
4
5
Example 4
To print 1 to 10 Odd Numbers
for x in range(1,10,2):
print(x, end=”,”)
Explanation 4
- Here start value is 1, because first odd number need to be in the question is also 1
- Should increase the next number by 2, in order to get the next odd number. So put the 3rd parameter in the range method as 2
Output
1,3,5,7,9,
Python for loop with examples
Example 6
To print numbers from 5 to 1
for x in range(5,0,-1):
print(x, end=”,”)
Explanation 6
- If you write python for loop as for x in range(10,0): then you will get no output.
- Here should put step value as -1
Output 6
5,4,3,2,1,
Example 7
To write 1 to 10 numbers Total
total=0
for x in range(1,11):
total=total + x
print(total)
Output
55
Nested for Loop
A nested loop is a loop inside another loop.
- Outer Loop: The outer loop is responsible for controlling the overall flow and repeating through the higher level structure.
- Inner Loop(s): The inner loop(s) are nested inside the outer loop and operate within each iteration of the outer loop. They can be used to iterate through elements or perform tasks within the context of the outer loop.
Following is a Simple Pyramid Pattern in Python to illustrate the Nested for Loop concept.
for i in range(0, 5):
for j in range(0, i+1):
print("* ",end="")
print("\r")
Output
Nested loops are a powerful and versatile programming concept used in various situations in python to solve complex problems that involve multiple levels of iteration.
2. For Loop in List
In Python, you can use a for loop to iterate over the elements of a list.
>>> list1 = [1, 2, 3, 4, 5]
>>> for items in list1:
print(items)
output
1
2
3
4
5
You can perform any operations on element within the loop.
Example
To get the total of all the elements of a list
numbers = [10, 20, 30, 40, 50]
tot=0
for n in numbers:
tot=tot+n
print(tot)
Output
150
Example
To find the maximum number in the listn
numbers = [10, 90, 110, 70, 50]
max= numbers[0]
for n in numbers:
if n > max:
max = n
print(max)
Output
110
break and continue statements are used in for loops to control the flow of the loop.