** The basics
- Range command works like this:
range(upToNumber)- it produce a sequence of numbers from 0 up to BUT NOT equal to the upToNumber. For example, range(5) will produce:0,1,2,3,4, therefore, there are 5 numbers, for each number the loop going through, it prints out “Welcome to the range”.range(startNumber, upToNumber)- it produces a sequence of numbers from startNumber to BUT NOT equal to the upToNumber. For example, range(3,8) will produce:3,4,5,6,7range(startNumber, upToNumber, increaseBy)- it produces a sequence of numbers from startNumber to BUT NOT equal to the upToNumber, each time the next number increase by the number specified byincreaseBy. For example, range(2,10,2) will produce:2,4,6,8.- the startNumber, upToNumber, increaseBy must be integers
- What can it be used for:
x + Most often you will use the range function to control how many times a loop should repeat
- Another common use is to iterate through a list one item at a time or even skip by a fixed number by giving the increaseBy parameter a number.
- Example 1:
- What numbers are produced in this loop:
- What numbers are produced in this loop:
- write a for loop that will print out all positive whole numbers (integers) up to 100 that are divisible by 5.