In this example, we used the keyword for
followed by a variable name fruit
, and the keyword in
followed by the list fruits
. The variable fruit
will take on the value of each item in the list, one at a time. The code inside the loop is indented, and it will be executed once for each item in the list.
We can also use the range()
function to create a sequence of numbers. For example, to print the numbers from 1 to 5: