Table of Contents
Preface vii
CHAPTER 1: 11
Why Read This Book? Why Learn These Skills? 11
Who Is This Book For? 12
Why Windows 13
Why Python 14
Base Python and Pandas 15
Anaconda Python 16
To install Anaconda Python (Windows or Mac) 17
Text Editors 18
Download Book Materials 19
Base Python and Pandas 19
Overview of Chapters 20
CHAPTER 2: Python Basics 25
Running Python in the Shell 25
How To Create a Python Script 25
How to Run a Python Script 27
A few more hints for interacting with the command line 29
Up Arrow for Previous Command 29
Ctrl+c to Stop a Script 29
Read and Search for Solutions to Error Messages 29
iiiAdd More Code to first_script.py 30
Python’s Basic Building Blocks 32
Numbers 32
Integers 32
Floating-point numbers 33
Strings 34
Split 36
Join 37
Strip 38
Replace 39
Lower, Upper, Capitalize 39
Regular Expressions and Pattern Matching 40
Dates 44
Lists 47
Create a list 47
Index values 48
List slices 49
Copy a list 49
IN and NOT IN 50
Append, Remove, Pop 50
Reverse 51
Sorting 52
Tuples 54
Create a tuple 54
>Unpack tuples 54
Convert tuple to list, list to tuple 55
Dictionaries 55
Create a dictionary 56
Keys 56
Copy 57
IN, NOT IN, and GET 57
Sort 59
Control Flow 60
If-Else 60
Table of Contents
ivIf-Elif-Else 61
For Loops 61
Compact For Loops: List, Set, and Dictionary Comprehensions 63
While Loops 64
Functions 65
Exceptions 66
Try-Except 66
Try-Except-Else-Finally 67
Reading a Text File 67
Create a text file 68
Script and Input File in Same Location 70
Modern File Reading Syntax 70
Reading Multiple Text Files with Glob 71
Create another text file 72
Writing to a Text File 74
Writing to a Comma Separated Values “CSV” File 76
Print Statements 77
Chapter Exercises: 78