For in loops
A lot of languages have more than one way to loop and python is no exception. For in is the other main loop in python. Below is a example of how for in works –
names= ["bert", "fred", "sally"]
for x in names:
print x
It is a simpler way of iterating over a list or array. It can also be used on strings –
text = "hello"
for x in text:
print x
The text or list comes after the in keyword. Every time it loops it will perform the following operation –
x = array[current_position]
The equivalent while loop would look something like this –
names= ["bert", "fred", "sally"]
current_position = 0
while current_position < len(names):
x = names[current_position]
print x
current_position = current_position + 1
Write a for in loop to print off the following array.
subjects = ["ICT", "maths", "PE", "RE"]
Output: (clear)











