In this article, we will be covering FOR loops in Python.
In Python, in order to iterate through a list, we can use FOR loops. In python, for works similar to for each loops. Lets look at syntax of FOR loop in Python.
# course 10:
# FOR loops in Python
x=0
for index in range(5):
x +=1
print(x)
Output of above FOR loop in Python example:
In above Python example, we first declared a variable called x and set it to 0. We declared a surrogate variable called index and called it 5 times with range function in Python. Each iteration increased value of x by 1.
Notice that, range function indicates loop to cycle 5 times over. That way, value of x ended up with final value of 5 due to that.