Python 2.x
The legacy version you may still meet in old codebases.
Python 2.x is taught here in 11 lessons, running from The print statement and integer division through to Where Python 2 still appears and what to do about it. Each lesson takes one topic, shows the working code, and links onward to the next, so the course can be read straight through in order.
| Track | Python & Data Science |
| Lessons | 11 |
| Level | Beginner to intermediate |
| Reading time | about 2 hours |
| Prerequisites | Helpful, but not required: Pillow |
Lessons
- The print statement and integer divisionTwo Python 2 behaviours that change results without a warning: print as a statement, and division that floors between integers.
- unicode and str are different thingsPython 2 has a bytes type called str and a text type called unicode, plus hidden ASCII conversions that fail on any non-ASCII input.
- Migrating to Python 3The concrete changes between the versions, the automated tools, and the semantic diffs a compiler will not catch.
- Running Python 2 today: interpreter, pip and virtualenvGet a working 2.7 environment, install packages with the right pip, and understand exactly what you are accepting by keeping it alive.
- Lists, dicts and the Python 2-only APIsThe methods that vanished in Python 3, what they actually did, and the mechanical replacement for each one.
- Old-style vs new-style classes and metaclassesWhy class Foo: and class Foo(object): behaved differently, how metaclasses were set, and what Python 3 unified.
- Exceptions and the Python 2 except syntaxThe comma syntax, string exceptions, sys.exc_info, and a mechanical rewrite to the Python 3 form that a compiler would catch.
- Files, the codecs module and text vs binary I/ORead and write non-ASCII text without corrupting it, handle byte-order marks, and know why the Python 2 open() is always binary.
- Standard library differences from Python 3The renamed and reorganised modules you will meet in a 2.7 codebase, and the import line each one becomes in Python 3.
- Testing legacy Python 2 code before you change itPin a reproducible 2.7 test environment, write characterisation tests around behaviour you do not understand, and prove that unicode round-trips survive your edits.
- Where Python 2 still appears and what to do about itThe places 2.7 survives, a checklist for deciding between converting and containing it, and how to keep the blast radius small.
More in Python & Data Science
Python 3 NumPy pandas Matplotlib Jupyter Notebook Flask FastAPI Django SciPy Pillow R Julia
FAQ
How long does the Python 2.x course take?
It has 11 lessons, about 2 hours of reading. Expect roughly twice that if you type out and run every example.
Do I need prior experience for Python 2.x?
Not strictly. It helps to have read Pillow first, because some lessons build on it, but every lesson explains its own assumptions.
What should I read after Python 2.x?
Continue with R (12 lessons), the next course in Python & Data Science.