16. Range type#

Author: Tue Nguyen

16.1. Outline#

  • What is a range?

  • Create a range

  • Examples with ranges

  • Why uses ranges?

16.2. What is a range?#

  • A range is an immutable sequence of evenly-spaced integers such as

    • 0, 1, 2, 3, 4, …

    • 2, 4, 6, 8, …

  • We use range to quickly make a sequence of integers (often used in a for loop)

16.3. Create a range#

  • A range object is created using range() function

  • Syntax

    • range(stop)

    • range(start, stop)

    • range(start, stop, step)

  • The rule for start, stop, and step is the same as in Python slicing

    • start is included

    • stop is excluded

    • step specifies the gap between jumps

  • A range is very similar to a tuple but

    • It doesn’t contain actual elements

    • Each element will be generated on-the-fly when being asked

    • This helps save memory and increase performance

  • To see the actual elements of a range we need to convert it to a list or a tuple

Syntax 1: range(stop)

# Create a range from 0 to stop-1
r = range(10)
print(r)
print(type(r))
range(0, 10)
<class 'range'>
# See the actual elements
tuple(r)
(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

Syntax 2: range(start, stop)

# Create a range from 5 to 10
r = range(5, 11)
tuple(r)
(5, 6, 7, 8, 9, 10)

Syntax 3: range(start, stop, step)

# Create a range of even integers from 0 to 10
r = range(0, 11, 2)
tuple(r)
(0, 2, 4, 6, 8, 10)

Negative steps

# When step is negative, start must > stop
# Ex: create 5, 4, 3, 2, 1
tuple(range(5, 0, -1))
(5, 4, 3, 2, 1)
# If not, we will have an empty range
tuple(range(0, 5, -1))
()

16.4. Examples with ranges#

a) Ex 1: ranges in list comp

# Generate a list of squares <= 100
[e**2 for e in range(11)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# Generate a list of even squares <= 100
[e**2 for e in range(11) if e % 2 == 0]
[0, 4, 16, 36, 64, 100]

b) Ex 2: ranges in for loops

# Print out even numbers divisible by 7 and less than 100
for i in range(0, 100, 2):
    if i % 7 == 0:
        print(i)
0
14
28
42
56
70
84
98

16.5. Why uses ranges?#

  • First, using ranges saves typing as shown in the examples above

  • Second, using ranges save memory

    • range(10) doesn’t produce 10 integers

    • It is like a blueprint or instruction for Python on how to generate those integers

    • This blueprint will yield one element at a time when being asked, for example, in each iteration of a for loop

    • Thus, range(10) or range(100000) have the same (very small) size in the memory

  • Let’s see an example

# Compute the sum of first 1 million integers
total = 0
for i in range(1, 1000001):
    total += i
    
total
500000500000

Remarks

  • In the above example, we iterate through a range of the first 1 million integers

  • In each iteration, the range() function will yield an appropriate integer through variable i

  • And we add i to total to accumulate the sum (Note: total += i is equivalent to total = total + i)

  • Each integer is used only once, thus it is a waste of memory if we keep a list of 1 million integers sitting there through each iteration

  • Using a range is a better solution

16.6. Summary#

What is a range?

  • A range is an immutable sequence of evenly-spaced integers

  • We use range to quickly make a sequence of integers (often used in a for loop or in a comprehension)

Create a range

  • We use range() to create a range

  • Syntax

    • range(stop)

    • range(start, stop)

    • range(start, stop, step)

  • A range is very similar to a tuple but it doesn’t contains actual elements

    • Instead, it is more like a blueprint or instruction for Python on how to generate integers

    • The actual elements will be generated when being asked

    • All ranges have the same size in memory

Why uses ranges?

  • Save typing

  • Save the memory

16.7. Practice#

To be updated