Learn to Code by Solving Problems: A Python Programming Primer Homepage for the book Learn to Code by Solving Problems: A Python Programming Primer

What Beginning Coders Might Care About in Python 3.11

Hello, Python learners,

My Learn to Code by Solving Problems book uses Python to teach you how to program. About once a year, a new Python version is released with new features and improvements. And here we are with the latest version of Python, version 3.11!

As always with Python, you can continue to program as you have before. But I make a habit of checking what’s new each year because often I find a tidbit or two that I can benefit from in my code.

Let’s see what’s new with Python 3.11, from the perspective of the beginning programmer. At the time of writing, Python 3.11 is on beta 4. It’s generally not advised to work with beta versions of Python for serious work, but do read on if you’re curious about what’s coming down the pike.

(We did this last year, too, when Python version 3.10 came out. Check out my summary of those Python 3.10 changes , too, if you haven’t done so yet!)

Note: the version of Python before Python 3.11 is Python 3.10. The book assumes that you’re using at least Python 3.6, and everything will work as expected in later versions of Python as well, including Python 3.11.

Faster Python

One of the most exciting advances in Python 3.11 is in Python’s efficiency. What’s amazing here is that your same code, with no changes, is likely to run faster in Python 3.11 compared to prior versions of Python. The Python developers note that you can expect/hope for an average 25% speed increase in your code. Now, this isn’t going to be true for all programs. Some programs will gain way more than 25% here, and some will gain less. The take-home message though is that your code is very likely going to be faster.

How did they do it? Who knows. :) But here is the gist of two of the optimizations:

I ran timing experiments on a lot of the book code in order to compare Python 3.10 and Python 3.11 runtime. One of the biggest increases I found was for the Lifeguards code in Chapter 9, where depending on the test case I was able to observe up to a 20% speed increase.

Now, as you know from working through Chapters 8-10 of the book, you still need to choose the right data types and make your code efficient. No Python version is ever going to free you from that :)

But a 20% increase for free? And all I have to do is update to Python 3.11? Yes, please!

Better Error Messages

Last year, when we talked about the new features in Python 3.10, we learned about the improvements that the Python developers made to error messages.

They’re at it again!

Take a look at this traceback from Python 3.10:

Traceback (most recent call last):
  File "example.py", line 4, in <module>
    print(func('x', 'y') + func(2, 3))
  File "example.py", line 2, in func
    return 54 + 23 + first + second
TypeError: unsupported operand type(s) for +: 'int' and 'str'

The error is caused by line 2, and the function call that led there was at line 4. But line 4 has two function calls–which one broke this? And which part of line 2 is failing?

The error message for the same program from Python 3.11 answers those questions:

Traceback (most recent call last):
  File "example.py", line 4, in <module>
    print(func('x', 'y') + func(2, 3))
          ^^^^^^^^^^^^^^
  File "example.py", line 2, in func
    return 54 + 23 + first + second
           ~~~~~~~~^~~~~~~
TypeError: unsupported operand type(s) for +: 'int' and 'str'

The error for line 2 now tells us specifically that 23 + first is a problem. And the error for line 4 tells us that it’s the func('x', 'y') function call that’s failing.

You can expect to see these enhanced error messages in your own errors as well. You may be able to use them to help pinpoint where exactly an error is coming from, down to a piece of a line of code rather than the whole line.

Wrapping Up

That’s my summary of what I think will apply to everyone. There’s more, though. For the full list of what’s new, check out the Python 3.11 Release Notes. Some of the new features may not make sense to you right now… but if you keep going, and you want to learn more about them, I’m sure that you can!

A personal thank-you to the Python developers for an incredible focus on making the language better for everyone, including beginning programmers. My introductory CS courses are changing each year based directly on improvements that come down from Python developers.