2️⃣

5.2 For Loops + Range

For Loops + Range

For Loops

For loops keep doing something for a "known" number of times. This is similar to while loops. In fact, the usages of for loops and while loops overlap in some cases.
💡
Similar to if statements, while and for loops must be indented in order for them to work properly

Range

Let's first introduce range(). range() takes an integer x and keeps spitting out numbers from 0 to x-1 in order. This has 2 uses:
  1. we can use these numbers like printing them
  1. we can take advantage of the fact that range(x) spits out numbers x times to perform operations x times. For example
# stop for i in range(5): print(i)
Notice how 0 to 4 are printed on 5 separate lines. This indicates that print was run 5 separate times, and the number printed was from the range 0 to x-1. In programming terms, we say that it printed the numbers 0-5, exclusive, since 5 was not included
💡
i is just a name for a variable. You can replace it with anything (that follows Python variable naming rules) to make your code more readable
 
range() also can take the starting point. Before, we put in one value for range() and that indicated the endpoint (exclusive). The starting point by default was 0. Now range(x, y) means x is the starting point, and y is the ending point (exclusive). For example:
# start, stop for i in range(1, 5): print(i)
Notice how 1 to 4 were printed on 4 separate lines
 
range(x, y, z): if three numbers are put into the range function, the first two are the same as before and the third one represents the interval of increase (also called the 'step') between each number outputted. The step is 1 by default. For example:
# start, stop, step for i in range(2, 5, 2): print(i)
Notice how 2 and 4 were printed on 2 separate lines.
 
If you give range(x, y, z) a negative step value, it will go backwards, as you can see below
# negative step for i in range(5, 0, -1): print(i)
The above code will print 5, 4, 3, 2, 1 on separate lines.
 
We can take advantage of range to repeat groups of code a certain number of times. For example:
# using for loops to repeat code # a certain number of times for i in range(5): print("May the Force be with you")
The above code prints "May the Force be with you" 5 times.
 
View code on GitHub.

Practice

Hello World

Print "Hello World" 5 times

Even

Print every even number greater than 10 and less than 101

FizzBuzz

Fizz Buzz is a game played between 2 people where they take turns counting up starting at 1. However, if the number is divisible by 3, the person should say "fizz" instead of the number. If the number is divisible by 5, the person should say "buzz" instead of the number. If the number is divisible by both 3 and 5, the person should say "fizzbuzz" instead of the number. For example, this is how the first 5 results would look like: 1 2 fizz 4 buzz Write a program that outputs the first 100 results.
 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.