Range allows us to count from one number to another while being able to define where to start and end and how much we increment or decrement by. Meaning that we could count every other number or every fifth number if we wanted to. When used with a for loop, it gives us the ability to loop a certain number of times. In the previous example, we saw that a range of 5 printed out five numbers. This is because range defaults to starting at 0 and increments by 1 each time. Let’s see another example:
# providing the start, stop, and step for the range function for num in range(2, 10, 2):
print( "Value: { }".format(num) ) # will print all evens between 2 and 10
Go ahead and run that cell. This time we’ve specified our program to start the loop at the value of 2 and count to 10 but increment by 2. The output for our values becomes
“2, 4, 6, 8”.