ITP 115 Lecture Notes - Lecture 6: Reserved Word, Random Access, Sequential Access
Document Summary
For loop repeats code, but not based on a condition. For loop repeats its loop body for each element of the sequence, in order. Instead, it repeats based on a sequence: when it reaches the end of the sequence, the loop ends. Follow with a new variable (variable will only be used for this loop) Follow w/the sequence you want to loop through for variable in sequence: Sequence is an ordered set of things . A group of items stored together in a collection. Examples in python: a range of numbers (e. g. 1, 2, 3 or 2, 4, 6 etc. , string variables (e. g. hello start with h, then e, l, l, o, lists, tuples. Create sequences with range() range() function will generate a sequence based on some parameters (aka arguments) range(int stop) Up to but not including stop range(6) 0, 1, 2, 3, 4, 5. Returns sequence range(int start, int stop, int step)