Appendix B: Python Quick Reference
How to use this appendix: This is a condensed reference card for every Python concept covered in this book. It is organized by topic so you can find what you need quickly. This is not a tutorial --- if you need explanations and examples, refer back to the relevant chapter. If you need a quick reminder of syntax, this is your page.
B.1 Data Types
| Type | Example | Notes |
|---|---|---|
int |
42, -7, 0 |
Whole numbers, unlimited precision |
float |
3.14, -0.001, 1e6 |
Decimal numbers, ~15 digits of precision |
str |
"hello", 'world', """multi""" |
Immutable text sequences |
bool |
True, False |
Logical values; subtype of int (True == 1, False == 0) |
NoneType |
None |
Represents absence of a value |
list |
[1, 2, 3] |
Mutable ordered sequence |
tuple |
(1, 2, 3) |
Immutable ordered sequence |
dict |
{"a": 1, "b": 2} |
Mutable key-value mapping |
set |
{1, 2, 3} |
Mutable unordered collection of unique values |
Type checking:
type(x) # Returns the type of x
isinstance(x, int) # True if x is an int (or subclass)
isinstance(x, (int, float)) # True if x is int or float
Type conversion:
int("42") # 42 (string to int)
float("3.14") # 3.14 (string to float)
str(42) # "42" (number to string)
bool(0) # False (falsy value)
bool("hello") # True (truthy: non-empty string)
list((1,2,3)) # [1, 2, 3] (tuple to list)
tuple([1,2,3]) # (1, 2, 3) (list to tuple)
set([1,2,2,3]) # {1, 2, 3} (removes duplicates)
Falsy values: 0, 0.0, "", [], {}, set(), None, False. Everything else is truthy.
B.2 Operators
Arithmetic Operators
| Operator | Operation | Example | Result |
|---|---|---|---|
+ |
Addition | 7 + 3 |
10 |
- |
Subtraction | 7 - 3 |
4 |
* |
Multiplication | 7 * 3 |
21 |
/ |
Division (float) | 7 / 3 |
2.333... |
// |
Floor division | 7 // 3 |
2 |
% |
Modulo (remainder) | 7 % 3 |
1 |
** |
Exponentiation | 2 ** 10 |
1024 |
Precedence (highest to lowest): ** then *, /, //, % then +, -. Use parentheses when in doubt.
Comparison Operators
| Operator | Meaning | Example |
|---|---|---|
== |
Equal to | x == 5 |
!= |
Not equal to | x != 5 |
< |
Less than | x < 5 |
> |
Greater than | x > 5 |
<= |
Less than or equal | x <= 5 |
>= |
Greater than or equal | x >= 5 |
Logical Operators
| Operator | Meaning | Example |
|---|---|---|
and |
Both must be True | x > 0 and x < 10 |
or |
At least one must be True | x < 0 or x > 100 |
not |
Negation | not x > 5 |
Assignment Operators
x = 10 # Assignment
x += 5 # x = x + 5 (now 15)
x -= 3 # x = x - 3 (now 12)
x *= 2 # x = x * 2 (now 24)
x /= 4 # x = x / 4 (now 6.0)
x //= 2 # x = x // 2 (now 3.0)
x %= 2 # x = x % 2 (now 1.0)
x **= 3 # x = x ** 3 (now 1.0)
Membership and Identity
"a" in "apple" # True
3 in [1, 2, 3] # True
"key" in my_dict # True if "key" is a key in the dictionary
x is None # True if x is None (use 'is' for None, not ==)
x is not None # True if x is not None
B.3 String Methods
s = " Hello, World! "
# Case conversion
s.lower() # " hello, world! "
s.upper() # " HELLO, WORLD! "
s.title() # " Hello, World! "
s.capitalize() # " hello, world! "
s.swapcase() # " hELLO, wORLD! "
# Whitespace handling
s.strip() # "Hello, World!"
s.lstrip() # "Hello, World! "
s.rstrip() # " Hello, World!"
# Search and test
s.find("World") # 9 (index of first occurrence, -1 if not found)
s.index("World") # 9 (like find, but raises ValueError if not found)
s.count("l") # 3
s.startswith(" He") # True
s.endswith("! ") # True
# Checking content
"abc".isalpha() # True (all letters)
"123".isdigit() # True (all digits)
"abc123".isalnum() # True (all letters or digits)
" ".isspace() # True (all whitespace)
# Modification (returns new string --- strings are immutable)
s.replace("World", "Python") # " Hello, Python! "
"a,b,c".split(",") # ["a", "b", "c"]
", ".join(["a", "b", "c"]) # "a, b, c"
"hello".center(20) # " hello "
"hello".ljust(20) # "hello "
"hello".rjust(20) # " hello"
"42".zfill(5) # "00042"
# Slicing
s = "Hello"
s[0] # "H"
s[-1] # "o"
s[1:4] # "ell"
s[:3] # "Hel"
s[2:] # "llo"
s[::-1] # "olleH" (reverse)
B.4 List Methods
fruits = ["apple", "banana", "cherry"]
# Adding elements
fruits.append("date") # ["apple", "banana", "cherry", "date"]
fruits.insert(1, "avocado") # ["apple", "avocado", "banana", "cherry", "date"]
fruits.extend(["elderberry", "fig"]) # Adds multiple items to end
# Removing elements
fruits.remove("banana") # Removes first occurrence
popped = fruits.pop() # Removes and returns last item
popped = fruits.pop(0) # Removes and returns item at index 0
fruits.clear() # Removes all items
# Searching and counting
fruits = ["apple", "banana", "cherry", "banana"]
fruits.index("banana") # 1 (index of first occurrence)
fruits.count("banana") # 2
# Sorting
fruits.sort() # Sorts in place (ascending)
fruits.sort(reverse=True) # Sorts in place (descending)
fruits.sort(key=len) # Sorts by string length
sorted_fruits = sorted(fruits) # Returns new sorted list (does not modify original)
# Other
fruits.reverse() # Reverses in place
fruits_copy = fruits.copy() # Shallow copy
len(fruits) # Number of items
# List comprehension
squares = [x**2 for x in range(10)] # [0, 1, 4, 9, ...]
evens = [x for x in range(20) if x % 2 == 0] # [0, 2, 4, 6, ...]
cleaned = [s.strip().lower() for s in dirty_list] # Clean a list of strings
B.5 Dictionary Methods
student = {"name": "Jordan", "gpa": 3.7, "major": "Data Science"}
# Accessing values
student["name"] # "Jordan" (raises KeyError if key missing)
student.get("name") # "Jordan"
student.get("age", "N/A") # "N/A" (default if key missing)
# Adding / updating
student["age"] = 21 # Add new key-value pair
student["gpa"] = 3.8 # Update existing value
student.update({"age": 22, "minor": "Stats"}) # Update multiple
# Removing
del student["minor"] # Remove key-value pair
removed = student.pop("age") # Remove and return value
# Iteration
student.keys() # dict_keys(["name", "gpa", "major"])
student.values() # dict_values(["Jordan", 3.8, "Data Science"])
student.items() # dict_items([("name", "Jordan"), ...])
for key, value in student.items():
print(f"{key}: {value}")
# Testing membership (checks keys, not values)
"name" in student # True
"address" in student # False
# Dictionary comprehension
word_lengths = {word: len(word) for word in ["apple", "banana", "cherry"]}
# {"apple": 5, "banana": 6, "cherry": 6}
B.6 Set Methods
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
# Set operations
A | B # Union: {1, 2, 3, 4, 5, 6}
A & B # Intersection: {3, 4}
A - B # Difference: {1, 2}
A ^ B # Symmetric difference: {1, 2, 5, 6}
# Method equivalents
A.union(B)
A.intersection(B)
A.difference(B)
A.symmetric_difference(B)
# Modification
A.add(5) # Add single element
A.discard(5) # Remove element (no error if absent)
A.remove(5) # Remove element (KeyError if absent)
# Testing
A.issubset(B) # True if every element of A is in B
A.issuperset(B) # True if A contains every element of B
A.isdisjoint(B) # True if no common elements
B.7 Control Flow
if / elif / else
if condition:
# runs if condition is True
elif other_condition:
# runs if first was False and this is True
else:
# runs if all above were False
for loops
for item in iterable:
print(item)
for i in range(5): # 0, 1, 2, 3, 4
print(i)
for i in range(2, 10, 3): # 2, 5, 8
print(i)
for i, item in enumerate(my_list): # Get index and item
print(f"{i}: {item}")
for key, value in my_dict.items(): # Iterate over dict
print(f"{key} = {value}")
while loops
count = 0
while count < 10:
print(count)
count += 1
Loop control
for x in range(100):
if x == 5:
continue # Skip rest of this iteration, go to next
if x == 10:
break # Exit the loop entirely
Ternary (conditional) expression
status = "pass" if score >= 60 else "fail"
B.8 Functions
Defining functions
def greet(name):
"""Return a greeting string."""
return f"Hello, {name}!"
# With default argument
def greet(name, greeting="Hello"):
return f"{greeting}, {name}!"
# With type hints (optional but recommended)
def calculate_bmi(weight_kg: float, height_m: float) -> float:
"""Calculate Body Mass Index."""
return weight_kg / (height_m ** 2)
# Multiple return values (returned as tuple)
def min_max(numbers):
return min(numbers), max(numbers)
lo, hi = min_max([3, 1, 4, 1, 5]) # Tuple unpacking
# *args and **kwargs
def flexible(*args, **kwargs):
print(f"Positional: {args}")
print(f"Keyword: {kwargs}")
flexible(1, 2, 3, name="test", value=42)
Lambda functions
square = lambda x: x ** 2
square(5) # 25
# Common use: as argument to sort/filter/map
sorted(names, key=lambda n: n.lower())
filtered = list(filter(lambda x: x > 0, numbers))
mapped = list(map(lambda x: x * 2, numbers))
B.9 Common Built-in Functions
| Function | Purpose | Example |
|---|---|---|
print() |
Display output | print("hello", end="") |
len() |
Length of sequence | len([1,2,3]) returns 3 |
type() |
Type of object | type(42) returns <class 'int'> |
range() |
Sequence of integers | range(5) gives 0,1,2,3,4 |
int() |
Convert to integer | int("42") returns 42 |
float() |
Convert to float | float("3.14") returns 3.14 |
str() |
Convert to string | str(42) returns "42" |
bool() |
Convert to boolean | bool(0) returns False |
abs() |
Absolute value | abs(-5) returns 5 |
round() |
Round a number | round(3.14159, 2) returns 3.14 |
min() |
Minimum value | min([3, 1, 4]) returns 1 |
max() |
Maximum value | max([3, 1, 4]) returns 4 |
sum() |
Sum of iterable | sum([1, 2, 3]) returns 6 |
sorted() |
Return sorted copy | sorted([3,1,2]) returns [1,2,3] |
reversed() |
Return reversed iterator | list(reversed([1,2,3])) |
enumerate() |
Index + value pairs | list(enumerate(["a","b"])) |
zip() |
Pair items from iterables | list(zip([1,2], ["a","b"])) |
map() |
Apply function to each item | list(map(str, [1,2,3])) |
filter() |
Keep items where function is True | list(filter(bool, [0,1,2])) |
any() |
True if any element is True | any([False, True, False]) |
all() |
True if all elements are True | all([True, True, False]) |
isinstance() |
Check type | isinstance(42, int) |
input() |
Read user input | name = input("Name: ") |
open() |
Open a file | f = open("data.txt") |
help() |
Show documentation | help(len) |
dir() |
List attributes | dir(str) |
B.10 f-String Formatting
name = "Elena"
age = 28
gpa = 3.14159
population = 8045311
# Basic insertion
f"Name: {name}" # "Name: Elena"
f"Age: {age}" # "Age: 28"
# Expressions inside braces
f"Next year: {age + 1}" # "Next year: 29"
f"Name upper: {name.upper()}" # "Name upper: ELENA"
# Number formatting
f"{gpa:.2f}" # "3.14" (2 decimal places)
f"{gpa:.4f}" # "3.1416" (4 decimal places)
f"{population:,}" # "8,045,311" (comma separator)
f"{population:>15,}" # " 8,045,311" (right-aligned, width 15)
f"{0.85:.1%}" # "85.0%" (percentage format)
f"{42:05d}" # "00042" (zero-padded, width 5)
f"{42:<10d}" # "42 " (left-aligned, width 10)
f"{42:>10d}" # " 42" (right-aligned, width 10)
f"{42:^10d}" # " 42 " (centered, width 10)
# Multiline f-strings
message = (
f"Student: {name}\n"
f"GPA: {gpa:.2f}\n"
f"Status: {'honors' if gpa >= 3.5 else 'regular'}"
)
B.11 File I/O Patterns
Reading files
# Read entire file
with open("data.txt", "r") as f:
content = f.read()
# Read line by line
with open("data.txt", "r") as f:
for line in f:
print(line.strip())
# Read all lines into a list
with open("data.txt", "r") as f:
lines = f.readlines()
# Read CSV with pandas (the preferred way for data science)
import pandas as pd
df = pd.read_csv("data.csv")
df = pd.read_csv("data.csv", encoding="utf-8", na_values=["", "NA", "N/A"])
Writing files
# Write text
with open("output.txt", "w") as f:
f.write("Hello, world!\n")
f.write("Second line.\n")
# Append to existing file
with open("log.txt", "a") as f:
f.write("New entry\n")
# Write CSV with pandas
df.to_csv("output.csv", index=False)
df.to_excel("output.xlsx", index=False)
Path handling
from pathlib import Path
data_dir = Path("data")
file_path = data_dir / "survey_results.csv" # Joins paths correctly
file_path.exists() # True/False
file_path.is_file() # True if it's a file
file_path.is_dir() # True if it's a directory
file_path.suffix # ".csv"
file_path.stem # "survey_results"
file_path.parent # Path("data")
# List all CSV files in a directory
csv_files = list(data_dir.glob("*.csv"))
B.12 Common Error Types and Fixes
| Error | Common Cause | Fix |
|---|---|---|
SyntaxError |
Missing colon, parenthesis, or quote | Check for missing : after if/for/def, unmatched ( or " |
IndentationError |
Inconsistent spaces/tabs | Use 4 spaces consistently; never mix tabs and spaces |
NameError |
Variable not defined or misspelled | Check spelling and case; ensure variable is defined before use |
TypeError |
Wrong type for operation | Check types with type(); convert with int(), str(), etc. |
ValueError |
Right type but wrong value | int("hello") fails; validate input before converting |
IndexError |
Index out of range | List of length 5 has indices 0--4; check len() first |
KeyError |
Dict key not found | Use .get(key, default) instead of dict[key] |
AttributeError |
Object has no such method | Check type() of the object; consult dir(obj) |
FileNotFoundError |
File path is wrong | Check path with Path.exists(); check working directory |
ZeroDivisionError |
Division by zero | Add a guard: if denominator != 0: |
ImportError |
Module not installed | Run pip install module_name or conda install module_name |
ModuleNotFoundError |
Module not found | Check spelling; ensure correct environment is active |
Debugging strategies
# 1. Read the error message --- bottom line first, then trace up
# 2. Add print statements to inspect values
print(f"DEBUG: x = {x}, type = {type(x)}")
# 3. Check types explicitly
assert isinstance(x, int), f"Expected int, got {type(x)}"
# 4. Use try/except for graceful error handling
try:
result = int(user_input)
except ValueError:
print("Please enter a valid number.")
# 5. In Jupyter, use %debug after an error to enter the debugger
# 6. Break complex expressions into smaller steps
B.13 Importing Modules
# Standard library
import math
import os
import json
import csv
import datetime
from pathlib import Path
from collections import Counter, defaultdict
# Data science stack
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, mean_squared_error
# Import conventions (follow these for consistency)
# numpy -> np
# pandas -> pd
# matplotlib -> plt
# seaborn -> sns
B.14 Useful Python Patterns for Data Science
Counting occurrences
from collections import Counter
words = ["cat", "dog", "cat", "bird", "dog", "cat"]
Counter(words) # Counter({"cat": 3, "dog": 2, "bird": 1})
Counter(words).most_common(2) # [("cat", 3), ("dog", 2)]
Flattening nested lists
nested = [[1, 2], [3, 4], [5, 6]]
flat = [item for sublist in nested for item in sublist] # [1, 2, 3, 4, 5, 6]
Creating dictionaries from parallel lists
keys = ["name", "age", "city"]
values = ["Elena", 28, "Seattle"]
record = dict(zip(keys, values)) # {"name": "Elena", "age": 28, "city": "Seattle"}
Safe division
def safe_divide(a, b, default=0):
return a / b if b != 0 else default
Reading JSON
import json
with open("config.json", "r") as f:
data = json.load(f)
Timing code
import time
start = time.time()
# ... your code ...
elapsed = time.time() - start
print(f"Took {elapsed:.2f} seconds")
# In Jupyter:
# %timeit my_function() # Average over many runs
# %%timeit # Time an entire cell
This reference covers Python as used in Chapters 1--36 of this book. For the complete Python documentation, visit docs.python.org.