Python 3.13: Cool New Features for You to Try

2 days ago 3
Club.noww.in

Python 3.13 will be published on October 1, 2024. This new version is a major step forward for the language, although several of the biggest changes are happening under the hood and won’t be immediately visible to you.

In a sense, Python 3.13 is laying the groundwork for some future improvements, especially to the language’s performance. As you read on, you’ll learn more about the background for this and dive into some new features that are fully available now.

In this tutorial, you’ll learn about some of the improvements in the new version, including:

  • Improvements made to the interactive interpreter (REPL)
  • Clearer error messages that can help you fix common mistakes
  • Advancements done in removing the global interpreter lock (GIL) and making Python free-threaded
  • The implementation of an experimental Just-In-Time (JIT) compiler
  • A host of minor upgrades to Python’s static type system

If you want to try any of the examples in this tutorial, then you’ll need to use Python 3.13. The tutorials Python 3 Installation & Setup Guide and How Can You Install a Pre-Release Version of Python? walk you through several options for adding a new version of Python to your system.

In addition to learning more about the new features coming to the language, you’ll also get some advice about what to consider before upgrading to the new version. Click the link below to download code examples demonstrating the new capabilities of Python 3.13:

Take the Quiz: Test your knowledge with our interactive “Python 3.13: Cool New Features for You to Try” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

Python 3.13: Cool New Features for You to Try

In this quiz, you'll test your understanding of the new features introduced in Python 3.13. By working through this quiz, you'll review the key updates and improvements in this version of Python.

An Improved Interactive Interpreter (REPL)

If you run Python without specifying any script or code, you’ll find yourself inside Python’s interactive interpreter. This interpreter is informally called the REPL because it’s based on a read-evaluate-print loop. The REPL reads your input, evaluates it, and prints the result before looping back and doing the same thing again.

The Python REPL has been around for decades, and it supports an explorative workflow that makes Python a beginner-friendly language. Unfortunately, the interpreter has been missing several features you may have come to expect, including multiline editing and efficient pasting of code.

Begin by starting the REPL. You can do this by typing python in your terminal. Depending on your setup, you may have to write py, python3, or even python3.13 instead. One way to recognize that you’re using the new interpreter shipping with Python 3.13 is that the prompt consisting of three chevrons (>>>) is subtly colored:

The new REPL in Python 3.13 shows a colored prompt

One improvement is that you can now use REPL-specific commands without calling them with parentheses as if they are Python functions. Here are some of the commands and keyboard shortcuts you can use:

  • exit or quit: Exit the interpreter
  • clear: Clear the screen
  • help or F1: Access the help system
  • F2: Open the history browser
  • F3: Enter paste mode

You can learn more about these options in Python 3.13 Preview: A Modern REPL.

Recalling code you’ve written earlier has been cumbersome in the REPL before Python 3.13, especially if you’re working with a block of code spanning several lines. Traditionally, you’ve had to bring back each line one by one by repeatedly pressing Up. Now in 3.13, you can bring back the whole block of code with a single Up keystroke.

To try this for yourself, enter the following code in your REPL:

Python
>>> numbers = range(3, 13) >>> [ ... (number - 3)**3 for number in numbers ... if number % 2 == 1 ... ] [0, 8, 64, 216, 512]
Copied!

You’re creating a somewhat complex list comprehension that calculates an offset cube of a range of numbers, but only if the numbers are odd. The important part is that for readability, you split the list comprehension over several lines. Now try hitting that Up key! The interpreter recalls all four lines at once, and you can continue to use your arrow keys to move around inside of the expression.

You can make changes to your code and run it again. To execute the updated code, you need to move your cursor to the end of the last line in the code block. If you press Enter inside the expression, you’ll create a new empty line instead:

The ability to recall and edit multiline statements is a huge time-saver and will make you more efficient when working with the REPL.

Another convenience coming in Python 3.13 is proper support for pasting code. In Python 3.12 and earlier, you’d need to make sure that your code doesn’t contain any blank lines before you could copy and paste it. In the new version, pasted code is treated as a unit and executes just as it would inside a script.

Read the full article at https://realpython.com/python313-new-features/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

Read Entire Article