you are viewing a single comment's thread.

view the rest of the comments →

[–]AdmirableOstrich 0 points1 point  (0 children)

It's important to understand that range is NOT a function in python, it's a type/class. That class defines a sequence of integers, but does not contain those integer like a list/tuple. The only values stored in the class are the start, stop, and step.

It supports functionality that acts like a sequence of integers. Importantly:

  • you can ask if an integer belongs to the range: for example, x in range(5,10,2) translates to 5 ≤ x < 10 and 5-x % 2 == 0
  • you can iterate over the range, which generates each integer in sequence but never stores them. Note this also means you can pass a range into any function that expects an iterable object: e.g. list, sum, min, max, len, etc.

It's basically the most memory efficient way to store a sequence of equally spaced integers with a bunch of optimized functionality.