Skip to content

Latest commit

 

History

History
executable file
·
145 lines (117 loc) · 5.17 KB

File metadata and controls

executable file
·
145 lines (117 loc) · 5.17 KB

Python Range Function

Python range function

:EN

** The basics

  • Range command works like this:
    1. 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”.
    2. 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,7
    3. range(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 by increaseBy. For example, range(2,10,2) will produce: 2,4,6,8.
    4. 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.

Try it out

  1. Example 1:
  1. What numbers are produced in this loop:

  1. What numbers are produced in this loop:

  1. write a for loop that will print out all positive whole numbers (integers) up to 100 that are divisible by 5.