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 ...
Get Python for Data Analysis, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.