For Loops
Visit every item in a list, one by one.
1fruits = ["apple", "banana", "cherry"]
2for fruit in fruits:
3 print(fruit)
$ python3 main.py
apple
banana
cherry
Every line, explained
fruits = ["apple", "banana", "cherry"]- A list stores several values in one variable. The values go between square brackets, separated by commas. Lists are one of Python's favourite tools, and unlike some languages, you can print one directly and see its contents.
for fruit in fruits:- for ... in ... visits each item in the list, one trip around the loop per item. Each time, the loop variable holds the current item. Like if, the line ends with a colon and the loop's body is indented. Naming the loop variable fruit (singular) and the list fruits (plural) makes the line read like English: "for each fruit in fruits".
print(fruit)- print(...) shows whatever is inside its round brackets in the terminal, then moves to a new line. It is written all in lowercase, and it needs both brackets. This runs three times, once per item, and fruit is different each time.