Skip to Content
Python for Data Analysis, 3rd Edition
book

Python for Data Analysis, 3rd Edition

by Wes McKinney
August 2022
Beginner to intermediate
582 pages
13h 6m
English
O'Reilly Media, Inc.
Book available
Content preview from Python for Data Analysis, 3rd Edition

Chapter 3. Built-In Data Structures, Functions, and Files

This chapter discusses capabilities built into the Python language that will be used ubiquitously throughout the book. While add-on libraries like pandas and NumPy add advanced computational functionality for larger datasets, they are designed to be used together with Python’s built-in data manipulation tools.

We’ll start with Python’s workhorse data structures: tuples, lists, dictionaries, and sets. Then, we’ll discuss creating your own reusable Python functions. Finally, we’ll look at the mechanics of Python file objects and interacting with your local hard drive.

3.1 Data Structures and Sequences

Python’s data structures are simple but powerful. Mastering their use is a critical part of becoming a proficient Python programmer. We start with tuple, list, and dictionary, which are some of the most frequently used sequence types.

Tuple

A tuple is a fixed-length, immutable sequence of Python objects which, once assigned, cannot be changed. The easiest way to create one is with a comma-separated sequence of values wrapped in parentheses:

In [2]: tup = (4, 5, 6)

In [3]: tup
Out[3]: (4, 5, 6)

In many contexts, the parentheses can be omitted, so here we could also have written:

In [4]: tup = 4, 5, 6

In [5]: tup
Out[5]: (4, 5, 6)

You can convert any sequence or iterator to a tuple by invoking tuple:

In [6]: tuple([4, 0, 2])
Out[6]: (4, 0, 2)

In [7]: tup = tuple('string')

In [8]: tup
Out[8]: ('s', 't', 'r', 'i', 'n', 'g')

Elements can be accessed ...

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

Python for Data Analysis: Step-By-Step with Projects

Python for Data Analysis: Step-By-Step with Projects

Just Into Data

Publisher Resources

ISBN: 9781098104023Errata PageSupplemental Content