FAQ: Creating and Modifying a List in Python - Range II

This community-built FAQ covers the “Range II” exercise from the lesson “Creating and Modifying a List in Python”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Range II

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Why does the ‘range’ function exclude the upper limit from the list? Isn’t it more intuitive for it to include both the lower and upper limits?

I believe the “my_range3()” portion is incorrect, the range will stop at 99 (not 100), right? So 101 would not appear because the range stops at 99.

image

I came here to report the same. It makes confusion and needs to be fixed.

Edit: I think I need another coffee, I guess you meant that using range(1, 100, step) could only ever reach a maximum of 99. In that case the text is a little hazy. If you treat the stopping point as the value passed to range then it makes sense but it could easily be read as the maximum possible value (regardless of step). A bit ambiguous I’d agree.

Original-
Since range takes arguments along the lines of (start, stop, step) I’d say it’s an accurate statement that the number 101 would not appear because it is greater than the stop value.

I think the confusion may come from the use of ‘sequence’ which is guess refers to the sequence of numbers which insreases by 10 each time (xn = 10n + 1) and not to the range object itself which has strict limits.

Looking at the explanation of the page, which says:

By default, range creates a list starting at 0 . However, if we call range with two arguments, we can create a list that starts at a different number. For example, range(2, 9) would generate numbers starting at 2 and ending at 8 (just before 9 )

The page defines the “ending number” one before the written number for function argument. In that way, the stopping point is where the function stops the range, regardless of step number. In other words, we can create a range from 2 (starting point) to 8 (stopping point) by giving 2 and 8+1 as parameters to the function range(x,y) that is starting at x=2 and stopping at y-1=8.
Or maybe my interpretation is not correct.

I tried

list1 = range(5, 15, 0.3)
print(list(list1))

And it says an error, can’t increment by floats. Why not, and how can I do that if for some reason I needed to

It’s just because the range function is only designed to work with integers. There are good reasons for this as you’d want to be avoiding floats wherever possible. A simple solution is to do something like multiply/divide your range output by a float e.g. [x * 0.1 for x in range(10)] but you will wind up with floating point errors this way. You could mix this with something like the Decmial or Fractions module to try to minimise long term rounding errors if you continue to use these values but where possible it’s better just to try and avoid them.

If you’re doing this a lot you could use the thrid party but nontheless exceedingly popular numpy library which has options for creating floating ranges (linspace or arange) but they’re actual arrays and not as memory efficient as range itself.

I wonder, is it actually possible to create a range in reverse order (like starting from 10 and then 9, 8, 7 … etc), or are the range values always sorted from smallest to largest?

If you consult python documentation you can see that range accepts up to 3 arguments: start, stop en step. Where you can make the steps negative (-1 for example). Then the values will decrease

1 Like

Thanks a lot @stetim94, now I see.

How is the second parameter of the range() function required (and presumably not preset), but the first value has a preset?

If I create a function (e.g. def simple_function(a = "value 1", b, c = "value 2"):) where a parameter without a preset value follows a parameter with a preset value, then I get an error message:

  File "script.py", line 1
    def simple_function(a = 'value 1', b, c = 'value 2'):
                     ^
SyntaxError: non-default argument follows default argument

By contrast, the function range() seems to work with def range(start = 0, end_before, count_by = 1): .

How is this so? Is this because range() is a predefined function?

even built-in/predefined functions have to follow the syntax.

so range uses *args:

python - How can the built-in range function take a single argument or three? - Stack Overflow

which allow us to pass any number of arguments we desire, then the function checks how many arguments are provided and acts accordingly.

1 Like