 # 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()`?

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)?

1 Like

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))
``````
10 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