For Loops
Lesson
Control flow statements
- Change the way your code runs
- Different control flow statements allow you to repeatedly run the same block of code over and over, or even not run it at all!
- For loops are one type of control flow statement
- If/else statements are another type (which we will discuss later)
For loops
- Write something once, then execute it over and over!
- You have to know before you run a for loop how many times it will run
Syntax
- Line 1: for {loopVariable} in {loopList}:
- {loopVariable} is an element of the list
- {loopList} is the list to cycle through
- Line 2+: tab, then statement to loop over
The len() Function
- This gives the length of a list
- Use this when cycling through a list! For an example, see Example 4 in the Playground!
Playground