O'Reilly logo
live online training icon Live Online training

Modern Python Tips and Tricks

What features to start using after upgrading

Topic: Software Development
Trey Hunner

So you’ve upgraded to Python 3. Now you just need to figure out which new features you should embrace—and which old habits you should leave behind.

If you feel like you’re writing Python 3 code while still thinking in terms of Python 2, this course is for you. Expert Trey Hunner will help you reconfigure your Python 2-focused brain to think like Python 3 does. Over three hands-on hours, you’ll refactor more than a dozen examples of working Python 2 code to embrace new Python 3 features that you may have overlooked after upgrading.

What you'll learn-and how you can apply it

By the end of this live online course, you’ll understand:

  • The Python 2 features that are now obsolete or redundant (but that you’re probably still using)
  • The many important syntax and behavior changes between Python 2 and Python 3 that aren’t obvious at first glance
  • The new Python 3 features you should be using

And you’ll be able to:

  • Remove unnecessary and outdated Python 2-isms from your code
  • Refactor your code for improved readability and performance in Python 3

This training course is for you because...

  • You’re a Python developer who needs to get up to speed on Python 3.
  • You’re already using Python 3 but want to embrace its features more wholeheartedly to improve the readability of your code.
  • You’re familiar with Python 2 and want to see what’s changed in Python 3.

Prerequisites

  • A computer with Python 3.7+ and pip installed
  • A working knowledge of Python, including loops, functions, and classes
  • Familiarity with using pip to install Python packages

Course Set-up

During this workshop, you'll be using a test framework to verify correctness of your code. Here's what you need to know to get the test framework setup in your environment. Please try to work through these instructions before we begin.

  1. Make sure you've installed Python 3.6 or greater on your computer and that you have access to a code editor on your computer (see https://trey.io/install for instructions).

  2. Create a directory to use for this workshop in your home directory or desktop (or wherever you want to save your work). I'll refer to this folder as "python_class", but you can name it whatever you'd like. But don't forget where you put it as you will need it!

  3. Download the ZIP file at https://trey.io/3tips-olt-exercises and extract it into the python_class directory you just made.

  4. When unzipped, that ZIP file will create a new folder called "exercises". It contains the test framework we will be using, so you will be working from this folder. It also contains the files you will need to modify for most of the exercises in the workshop.

  5. Open up your system command-prompt or terminal (not a Python prompt, your system command prompt) and change directories to that exercises directory. On Windows you may need to type "C:" or another drive letter to change drives first. There are a couple shortcuts (see https://trey.io/cmd1 and https://trey.io/cmd2 for examples) in Windows for opening a command-prompt in the correct directory quickly.

  6. Once in the exercises directory in a command-prompt or terminal, run "py -3 test.py is_ok" on Windows or "python3 test.py is_ok" on Linux/Mac. You should see a success message that includes "Congrats and welcome to the Test Framework!" and ends with "OK".

  7. That's it! You're all setup for the workshop.

Recommended follow-up:

About your instructor

  • Trey Hunner helps software teams level up their Python skills through on-site Python training workshops and courses. Trey speaks at Python conferences, blogs about Python frequently, and runs Python Morsels, an online Python education service for individuals and small teams.

    Trey was formerly a director at the Python Software Foundation.

Schedule

The timeframes are only estimates and may vary according to how the class is progressing

Numbers and strings (30 minutes)

  • Presentation: Division; thousands separator; string formatting; unicode versus bytes
  • Hands-on exercises: Use string formatting, bytes, and unicode
  • Q&A

Asterisks (25 minutes)

  • Presentation: Merging iterables and mappings, keyword-only arguments, and more
  • Hands-on exercises: Practice with the many newer uses of * and **
  • Q&A

Break (5 minutes)

Iterators and laziness (30 minutes)

  • Presentation: Using the new built-in iterators, views, and lazy sequences
  • Hands-on exercises: Use the zip, reversed, and iteration utilities that changed in Python 3
  • Q&A

Classes (35 minutes)

  • Presentation: Inheritance; operator overloading; new class creation helpers
  • Hands-on exercises: Use dataclasses, changed dunder methods, and more
  • Q&A

Break (5 minutes)

Working with files (30 minutes)

  • Presentation: Navigating the filesystem with pathlib
  • Hands-on exercises: Use pathlib
  • Q&A

Miscellaneous improvements and next steps (20 minutes)

  • Presentation: Exception changes; type annotations; minor improvements and fixes
  • Q&A