Range Function

  1. Replace <1> in the following code with a call to range() so that the for loop prints out all integers from 1 to 10, inclusive.
     
    1
    for i in <1>:
    2
        print(i)
    3

    (range1)

    The range function takes 1, 2, or 3 integer parameters. The parameters are range(start=0, stop, step=1). If a start value is not provided, the value 0 is used. If a step value is not provided, the value 1 is used. The stop value indicates the upper limit that the result should be less than. E.g., if stop is 10, then the result of the range call will end at 9.
    3
     
    1
    for i in range(1, 11):
    2
        print(i)
    3

    (range1_a)

  1. Replace <1> in the following code with a call to range() so that the for loop prints out all integers from 0 to 99, inclusive.
    3
     
    1
    for i in <1>:
    2
        print(i)
    3

    (range2)

    Remember that you can omit the first parameter if you want the result to start at 0.
    3
     
    1
    for i in range(100):
    2
        print(i)
    3

    (range2_a)

  1. Replace <1> in the following code with a call to range() so that the for loop prints out all integers from -10 to 10, inclusive.
    3
     
    1
    for i in <1>:
    2
        print(i)
    3

    (range3)

    Parameters can be negative, and if step is one, the results will increase by 1 each time through the loop.
    3
     
    1
    for i in range(-10, 11):
    2
        print(i)
    3

    (range3_a)

  1. Replace <1> in the following code with a call to range() so that the for loop prints out all even integers from 0 to 100, inclusive.
    3
     
    1
    for i in <1>:
    2
        print(i)
    3

    (range4)

    Parameters can be negative, and if step is 1, the results will increase by 1 each time through the loop.
    3
     
    1
    for i in range(0, 101, 2):
    2
        print(i)
    3

    (range4_a)

  1. Replace <1> in the following code with a call to range() so that the for loop prints out all multiples of 5 from 0 to 200, inclusive.
    3
     
    1
    for i in <1>:
    2
        print(i)
    3

    (range5)

    Go back and review previous questions if you need a hint.
    3
     
    1
    for i in range(0, 201, 5):
    2
        print(i)
    3

    (range5_a)

  1. Replace <1> in the following code with a call to range() so that the for loop prints out all integers from 10 down to 1, inclusive.
    3
     
    1
    for i in <1>:
    2
        print(i)
    3

    (range6)

    Provide a step of -1.

    range() produces values down to but not equal to or less than the stop value, if the step is negative.

    3
     
    1
    for i in range(10, 0, -1):
    2
        print(i)
    3

    (range6_a)

Next Section - For Loops