Skip to Content
Pandas for Everyone: Python Data Analysis, First Edition
book

Pandas for Everyone: Python Data Analysis, First Edition

by Daniel Y. Chen
December 2017
Beginner to intermediate
410 pages
12h 45m
English
Addison-Wesley Professional
Content preview from Pandas for Everyone: Python Data Analysis, First Edition

P. Ranges and Generators

The Python range function allows the user to create a sequence of values by providing a starting value, an ending value, and if needed, a step value. It is very similar to the slicing syntax in Appendix L. By default, if we give range a single number, this function will create a sequence of values starting from 0.

# create a range of 5 r = range(5)

However, the range function doesn’t just return a list of numbers. In Python 3, it actually returns a generator. (In Python 2, this is the behavior of the xrange function.)

print(r)

range(0, 5)

print(type(r))

<class 'range'>

If we wanted an actual list of the range, we can convert the generator to a list.

lr = list(range(5)) print(lr)

[0, 1, 2, 3, 4]

Before you decide to convert ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Pandas for Everyone: Python Data Analysis, 2nd Edition

Pandas for Everyone: Python Data Analysis, 2nd Edition

Daniel Y. Chen

Publisher Resources

ISBN: 9780134547046Purchase book