IFB104 Lecture 5: IFB104 Lecture 5
Document Summary
Example of repeating an action for each item in a list. As a major demonstration involving a foreach loop, here we will develop a program which follows instructions stored in a list to draw complex pictures. It also refreshes our understanding of conditional statements, if-elif-else, from last week. Simple for-each loops: we have already seen how to do the same action several times using a for-each loop and the built-in range function. In python this is often combined with the len function to do a repeated action for however many items there are in a sequence. Accessing sequence values in for-each loops: the target variable in the for-each statement can be used in the loop body just like any other variable. More on the range function in for-each statements: the range function can be used in for-each statements to access items in sequences via their position.