What are the valid numbers that can be used for range()?

Question

For this exercise, the range() function is given a number and returns a list starting at 0 and including up to the number before the one provided. What are the valid numbers that can be passed to range()?

Answer

You can pass any valid integer number to the range() function. If the number 0 or a negative number is used, the range() function will return an empty list. Otherwise, the function will return a list starting at 0 including each number up to the number before the one passed.

# These are all valid parameters to pass
example1 = range(1)
example2 = range(30000)

# These will return an empty list
nonumber1 = range(0)
nonumber2 = range(-10)

# Passing a decimal number will return a Python TypeError
error1 = range(3.2)
17 Likes

What do you mean by a valid integer number? Does valid mean we need to take into account the memory available?

It will take any integer. In Python 3, memory is not a factor, as range() only produces one integer at a time, not a complete list. More here.

2 Likes

I understand it now.Thank you very much

1 Like

Is there a way to make the range function not include the 0, or start at a selected value (example: numbers between 50 and 100)?

2 Likes

How about range(50, 100)

2 Likes

Yes.

list(range(50, 101))

which will give you a list that starts with 50 and ends with 100.

10 Likes

Ah yes, the very next step in the lesson taught that, guess I jumped the gun. Thanks, guys!

2 Likes

You can generate a sequence of negative numbers as below:

# will produce [-5, -4, -3, -2, -1]
list(range(-5, 0))
11 Likes

I didn’t get it. Suppose if we print(list(range(100000)) then will it take large memory in comparison if the number print(list(range(10))?

In Python 3, range() returns a range object. Regardless the number of data point in the range, it will always be the same size. The object has a built in iterator that does not consume the range, so it remains iterable multiple times.

n = range(10)

for x in n:
    # code
if x in n:
    # code
while x in n:
    # no no (infinite loop since we cannot remove from a range)

range() only needs to know three values: start point, end point, and stride. The returned range has those points assigned and after that it just needs to know the next value in the range.

3 Likes

Hello,
Could you please help explain why range(True) return [0]

my_range = range(True)
print(list(my_range))
#return [0]

range() accepts any integer as an argument. In Python, True coerces to 1.

range(1)  =>  [0]

I got it. Thank you so much :smiley:

1 Like

To confess, there may not be coercion here, as suggested. To Python, True and False are not only bool, but also int.

>>> type(True) == int
False
>>> isinstance(True, int)
True
>>> 

It’s type cannot be manipulated, but the classes it inherits from change everything.

It is surprising and very interesting to learn it. Do you know any real usage of using True/False as an int data type?

The most practical use that comes to mind is toggling 1 and 0

>>> a = 0
>>> for _ in range(20):
    a = int(not(a))
    print (a, end=' ')

    
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 
>>> 

I’m sure other usage will present itself.

2 Likes

Two exploratory questions:

  1. Any thoughts on how we could create a list of even numbers till 10?
  2. Also, any thoughts on how we could create a list of ‘continuous’ data type between 1 and 2? Maybe, at increments of 0.1?

Using the range() function would be one way. If you are familiar with slices that would be another way. The most naive approach would be a for loop over a range.

evens = []
for x in range(11):
    if x % 2 == 0:
        evens.append(x)
print (evens)                               #  [0, 2, 4, 6, 8, 10]

We can set parameters on a range that accomplishes the same thing…

evens = list(range(0, 11, 2))
print (evens)                               #  [0, 2, 4, 6, 8, 10]

and with the slice syntax…

evens = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11][::2]
print (evens)                               #  [0, 2, 4, 6, 8, 10]

or the slice() function…

nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
print (nums[slice(0, 11, 2)])               #  [0, 2, 4, 6, 8, 10]

Since we cannot use floats in a range we would need to do some division.

>>> tenths = []
>>> for x in range(1, 11):
    tenths.append(1 + x / 10)

    
>>> tenths
[1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0]
>>> 

That’s one approach. See you cannot devise some others.

1 Like

Gotcha! Thank you, Roy!

1 Like