for loop, what,Python For Loops. A for loop is used for iterating over a sequence, java, python, software, PHP, C programming, Alpha Codist, course
For loop
Python For Loops. A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
The example will help you learn about it better.
Example:
In the example below there is a list named basket. It includes three apples and one orange. Here, the For loop checks the items each time it loops. So the first time it loops it checks the first string and attaches it in a variable called "item", then it runs the code inside the For loop which checks if the variable item( for the first loop item is "apple") is "apple". As it is True it prints "It is an apple." it also happens in the second and third loop. But in the fourth loop "orange"(the fourth item in the basket list) gets the place of apple(This time item = "orange") and since it is not equal to "apple" it is False and the program prints "It is not an apple."
basket = ["apple", "apple", "apple", "orange"]
for item in basket:
if item == "apple":
print("It is an apple")
else:
print("It is not an apple.")
COMMENTS