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.13!
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.13, from the perspective of the beginning programmer. Python is extremely stable at this point, especially for those just learning to program, so there isn’t much new to be aware of. Just keep using Python as you always have! There are some new benefits, though.
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.13.
In the past, when we talked about the new features in Python 3.10 and new features in Python 3.11, we learned about the improvements that the Python developers made to error messages.
One big improvement this time is that errors are displayed with color now.
Another improvement – I love this one as an educator (it’s going to help a lot!) – is that Python warns you now if you create your own Python filename with a conflicting name. For example, you never want to create a Python file called random.py
, because there’s already a built-in random module in Python. (Remember Chapter 9 from the book!) And you don’t want to create a Python file called bisect.py
for the same reason. Basically you always want to avoid naming your file using a name that Python already uses for a module.
In the past, you’d get really confusing error messages from Python if you did this.
For example, suppose that I (bad!) created my own Python file called bisect.py
. In past versions of Python, here’s what would happen if we tried to import that module:
Traceback (most recent call last):
File "test.py", line 3, in <module>
bisect.bisect_left([], 5)
^^^^^^^^^^^^^^^^^^
AttributeError: module 'bisect' has no attribute 'bisect_left'
Huh? Of course the bisect
module has a bisect_left
function! (We used it in Chapter 9. :) )
Now compare that to the new error message in Python 3.13:
Traceback (most recent call last):
File "C:\Windows\Temp\a.py", line 3, in <module>
bisect.bisect_left([], 5)
^^^^^^^^^^^^^^^^^^
AttributeError: module 'bisect' has no attribute 'bisect_left'
(consider renaming 'bisect.py' since it has the same name
as the standard library module named 'bisect'
and the import system gives it precedence)
You know the >>>
Python prompt that you use to type Python code? That interpreter has some new features now.
In addition to the color that I mentionned above, there’s also a new paste mode that you can start and stop by pressing F3.
What does paste mode do? It allows you to more easily paste code from the clipboard!
If you’ve ever tried to paste code with blank lines in it, you may have experienced errors because the interactive Python interpreter treats blank lines as the end of blocks of statements. For example, there’s nothing wrong with this code, but try pasting it into earlier versions of Python:
if 4 > 2:
print('Yes!')
print('Of course!')
Yep, that causes an error. The reason is that the blank lines after print('Yes!')
cause Python to terminate the body of the if statement. But we weren’t done! The print('Of course!')
is part of the if statement, too. Python gives an error because this is an indented statement after Python thought that the if statement was already over.
But today, try pressing F3 to get into paste mode, pasting the code, and pressing F3 again to get out of paste mode. It will work! The blank lines don’t cause problems in this mode.
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.13 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.
Written on October 17th, 2024 by Daniel Zingaro