iterate over an array in python
Code snippet on how to iterate over an array in python
my_list = [1,2,3,4]
for item in my_list:
print(item)
This code iterates over a list, or an array, of numbers. The array is assigned to the variable ‘my_list’. The for loop starts by taking the first item in ‘my_list’, which is 1, and assigning it to the variable ‘item’. The loop then runs the code block, which in this case is the ‘print’ statement. This will print out the value of ‘item’, which is currently 1. The loop then takes the next item in ‘my_list’, which is 2, and assigns it to ‘item’. The loop then runs the ‘print’ statement again, and prints out the value of ‘item’, which is currently 2. This continues until the last item in ‘my_list’, which is 4, is reached. After this, the loop ends and the program moves onto the next line of code.