We have understand how while loop is work, the same as while loop. This article you will understand between using while loop and for loop in Python. I will explain step by step with simple concept for beginner.
"For loop" is used to repeated block of code and iterate over sequence. For loop is required specific number of time to run which is different from while loop.
Syntax
for one_element in multi_element :
# to do statment
Example #1:
# name list
names = ["Heng","John","Sok","Sao","Dara"]
for item in names :
print("item=> ",item)
Output #1:
item=> Heng item=> John item=> Sok item=> Sao item=> Dara
Explain #1:
Line 3: the loop start take one element from names variable and store in item variable and print out the item and start repeat the loop. This for loop does not required index.
Using for loop with range()
function we can loop through bunch of elements with specified number of times. range()
function generate a sequence of numbers with parameters.
range(start,stop,step)
we pass parameters with start index by default is zero, where to stop and by default step to run is one.
Example #1:
for i in range(5):
print(i)
Output #1:
0 1 2 3 4
range(5) the second parameter is requred that is to know where to stop by default start value is zero and the result should list in 0-4 which is 5 elements.
Example #2:
for x in range(3, 6):
print(x)
Output #2:
3 4 5
range(3, 6) third parameter default is one range with start value 3 and stop at 5
Example #3:
for x in range(3, 6, 2):
print(x)
Output #3:
3 5
range(3, 6, 2) the step define as 2 and initialize value is 3 plus with step 2 is 5, and next loop is out of range so print line only 3 and 5
We can use else
with for loop statement which is executed after loop is finished.
Example #1:
people = ["Heng","John","Sok","Sao","Dara"]
# get length of list which is 5
size = len(people)
for i in range(size) :
print("My name is",people[i])
else :
print("There are",size,"people")
Output #1:
My name is Heng My name is John My name is Sok My name is Sao My name is Dara There are 5 people
Using else
with loop will not executed if the loop interrupted by break
statement
Example #2:
for i in range(5):
if i == 2:
break
print(i)
else :
print("Not print because break statment")
Output #2:
0 1
We can write while loop inside other for loop statement. The combination help to solve problem in different algorithm.
Example:
# first loop
for i in range(3):
# second loop
for j in range(2):
print (i,j)
Output:
0 0
0 1
1 0
1 1
2 0
2 1
We can use loop with multiple levels, but becareful the more levels more resources are expand.
Hope you learn and help you to understand basic how to using for loop statement in Python.
PythonAs the founder and passionate educator behind this platform, I’m dedicated to sharing practical knowledge in programming to help you grow. Whether you’re a beginner exploring Machine Learning, PHP, Laravel, Python, Java, or Android Development, you’ll find tutorials here that are simple, accessible, and easy to understand. My mission is to make learning enjoyable and effective for everyone. Dive in, start learning, and don’t forget to follow along for more tips and insights!. Follow him