Python Loop & range() Function
Concept
The function range()
can take 1 to 3 of the following arguments.
i) Start
ii) End
iii) Step
range(start, stop, step)
Note:
The Default value of start is Zero always.
End value user need to be mention
The Default value for step is 1 always
Code Example
------------------------------------------------------- For Loop -----------------------------------------------------
Example 1:
for i in range(1, 6, 1):
print(i)
Output:
1
2
3
4
5
Example 2:
for i in range(1, 51, 10):
print(i)
Output:
1
11
21
31
41
Example 3:
for i in range(6):
print(i)
Output:
0
1
2
3
4
5
Example 4:
for i in range(50, 0, -10):
print(i)
Output: 50
40
30
20
10
------------------------------------------------------- While Loop -----------------------------------------------------
Example 1:
>>> while count < limit:
print(count)
count += 1
OutPut:
1
2
3
4
5
6
7
8
9
# infinite loop with while
Example 2:
while True:
print("I will be printing infinite times")
Comments
Post a Comment