FAQ: Working with Lists in Python - The Power of Range!

This community-built FAQ covers the "The Power of Range! " exercise from the lesson “Working with Lists in Python”.

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

Data Scientist
Analyze Data with Python
Computer Science
Analyze Financial Data with Python
Build Chatbots with Python
Build Python Web Apps with Flask
Data Analyst

Learn Python 3
CS101 Livestream Series

FAQs on the exercise _The Power of Range! _

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

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

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in Language Help.

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

Need broader help or resources? Head to Language Help and Tips and Resources. If you are wanting feedback or inspiration for a project, check out Projects.

Looking for motivation to keep learning? Join our wider discussions in Community

Learn more about how to use this guide.

Found a bug? Report it online, or post in Bug Reporting

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!

For some reason the ‘e’ on range() doesn’t display for me - either on the examples or on the editor input I use. Same across all “range()” pages

It’s only when I copy and paste the blank “e” space to somewhere else it shows

Sounds more like an issue with fonts or rendering in your browser (if you mean an editor on your own device then perhaps it goes a bit further than that). Might be worth a web search for the issue as unfortunately it seems unlikely to have anything to do with codecademy.

It seems to be just limited to the range section of the course, it doesn’t happen on my Atom editor, just in the webpage editor

For the given lesson I get the following which seems fine-

Maybe try some different fonts in your browser, or a different browser, to see if they work.

1 Like

Yeah probably just Chrome bugging out… it doesn’t bother me, I’m passed that chapter now :slight_smile:

Thanks for your help anyways!

Extra Study

Creating an iterator from range(). Scratching the surface…

>>> r = range(10)
>>> next(r)
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    next(r)
TypeError: 'range' object is not an iterator
>>> r = map(lambda x: x, range(10))
>>> next(r)
0
>>> next(r)
1
>>> 

The nice thing about this is we are not confined to a loop construct (though it would be typical). What we have is a consumable object that we can use to access an iterable by index. How we work it into the logic is entirely open to us. Something to look into further when the unit on iterators comes up.

Hello there,

Can we list geometric progress list using range method?

Please explain geometric progress list.

Geometric progression example:
ex 2, 4, 8, 16

It is not possible to do that with range() since all its arguments must be integers.

what is lambda and iterator in python?

Without using technical vernacular, an iterator object is one that has a __next__() attribute. It is consumed when iterated.

Notice in the above example, the first next(r) returns 0, the first member of the range object. We cannot repeat that since the 0 has been consumed, hence the next, next(r) is 1. Now 1 has been consumed.

If you’ve not yet reached the unit on functions, then an explanation of lambda would be jumping the queue. It is an anonymous function which as above is often used as the callback of an iterator. map() returns an iterator object (a map object). This will all come up in due course so stick with the lesson plan and master the basics before jumping into this concept.

1 Like

For this code:

my_list = range(2, 9)

The range starts at 2 but ends at 8. Why is the former number inclusive and the latter number exclusive? Is this unique to Python?

The range function is by default fitted to the zero-indexed model of Python iterables. Say we have a list (or other iterable) that contains 10 items. What are the indices of those ten items?

ignore that you many not have seen this code before, and only examine the output:

>>> sequence = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
>>> for i, x in enumerate(sequence):
        print (f"[{i}] => {x}")

    
    [0] => 1
    [1] => 1
    [2] => 2
    [3] => 3
    [4] => 5
    [5] => 8
    [6] => 13
    [7] => 21
    [8] => 34
    [9] => 55
>>> 

In order to be able to generate an index on the fly, the range function output mirrors that above (the indices). 0…9, not 1…10. So the stop value in the range() argument is the count, or length, and the number of indices that will be produced.

>>> [*range(10)]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

Technically, the stop is not exclusive, since it is not representing the final value, only the final count. Above, that is, 10.

1 Like

Thank you very much! I now understand why it ends at 8, but by the same token, why would range(2, 9) starts at 2 instead of 1 if it’s zero-indexed? I’m still a little confused about how a list is related to the range function in this case.

range(2, 9) is not a default range, and may not necessarily represent indices, but an actual sequence with other meaning. The key here is that the ‘start’ argument is a value, not a count as described of the ‘stop’ argument, earlier. As a value, it is literally in the returned sequence.

The 9 is still the stop value, but treated as though the count began at 1, not 3, as the above does. Keep focus on the index explanation, and it gets easier to accept how the last value is exclusive in any generated sequence. Note the following:

>>> print ([*range(5, 50, 5)])
    [5, 10, 15, 20, 25, 30, 35, 40, 45]
>>> print ([*range(5, 51, 5)])
    [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
>>> 

A few days ago we posted a list of off-time challenges to learn the ins and outs of the range() function (an object constructor) and the expanse of its usage. It’s only a day or two back so should come up in a search of my posts.

Bottom line, this is a wholly useful and configurable constructor that warrants extra effort to learn everything we can about it. Soon you’ll be a whiz with this powerful tool.

1 Like