Python 3.9 is a general-purpose programming language. Without any doubt, it has been the best companion for Data Science. It has a wide range of libraries that allow core data analysis activities, presently it has been commanding the Analytics and Machine Learning Kingdom.
The programming language is being updated regularly with new features and supports, hence Python has finally released its 3.9 variant.
Python 3.9 new features bring several unique features like, position-only parameter, assignment expression – which assigns values to variables as part of a larger expression, f-strings support, and many more.
It also introduces some significant new syntax and standard library features.
Every update of Python has performance improvements over the earlier version. Python 3.9 has some significant improvements that enhance performance without doing any modifications to existing code.
Open-source enthusiasts from all over the world have been working on these new, enhanced the new feature in Python 3.9.
Most importantly, in this new version, you will get a new parser that is based on Parsing Expression Grammar (PEG). Furthermore, merge | and update |= Union Operators are added.
Python 3.9 New Features and Improvements
Some of the python 3.9 new features are amazingly impressive. let’s look into the top features that are noteworthy in this new release:
1) New Parser Based on PEG
Unlike the previous parser, this parser has some key differences that make it more flexible and future-proof.
It is not like the previous parser i.e. LL (1) based which was a top-down parser, this new parser parses the inputs from left to right.
The main difference between context-free-grammar-based parsers (older ones) and PEG is that in PEG the choice operator is ordered.
PEG parser is finely tuned to execute an instruction with only 10% memory usage and it quickly produces the AST (Abstract Syntax Tree) nodes for a rule via grammar actions.
The new grammar is context-free and as a result, the contexts of the tokens are not considered. Therefore, the generation of intermediate steps is avoided.
Also, Read – Python 3.9 Installation on Ubuntu 20.04 LTS
2) Support for the IANA Time Zone
In real-world applications, users normally need just three types of time zones: UTC, The local time zone of the system, and IANA time zones.
If are already familiar with earlier versions of Python then you might remember that Python 3.2 added a class datetime.timezone with the purpose to provide support for UTC.
Python 3.9 adds support for the IANA time zone database. Using the module zoneinfo, you can generate the IANA time zone database and this support has been added to the standard library now.
You can use the datetime library to add a datetime object and define its timezone by setting the tzinfo property. This makes you end up producing complex timezone rules while using datetime.tzinfo baseline.
3) Added Union Operators
In the earlier versions of Python, merging or updating two dicts was not very efficient. That’s why Python 3.9 introduced Union Operators like | for merging and |= for updating the dicts.
Consider this example to understand how to use this function, Dictionary union (|) will return a new dictionary that consists of the left operand merged with the right operand.
If a key appears in both operands, then the last-seen value (i.e. that from the right-hand operand) is chosen. Update (|=) operation will return the left operand merged with the right operand.
4) Type Hinting for Built-in Generic Types
Removing the parallel type hierarchy in Python has made it easy to interpret programs. The support for the generic syntax has been enabled in this release in all standard collections in the typing module.
Rather than using typing.Dict or typing.List in the signature of a function, you can now utilize this built-in dict or list collection types as generic types.
This results in cleaner code and it becomes easy to comprehend and explain the code.
The major plus point of this Python 3.9 new feature is that now user can easily interpret their code. It also helps the instructors to teach Python in a better way.
5) Ability to Cancel Concurrent Futures
Python 3.9 includes a new parameter cancel_futures in the concurrent.futures.Executor.shutdown(). This new parameter is included in both ThreadPoolExecutor and ProcessPoolExecutor.
It helps in canceling all of the pending futures that have not started. In the earlier versions, the process would wait for the pending futures to complete before closing the executor.
6) Random Bytes Generation
Another new feature added in the Python 3.9 version is random.Random.randbytes() function which is used to generate random bytes.
Before Python 3.9 you need to get creative to build these random bytes. Though you can use os.getrandom(), secrets.token_bytes() or os.urandom(), you still can’t generate pseudo-random patterns.
As an example, to assure that the random numbers are generated with normal behavior and the process is reproducible, we usually use this seed with random.Random module.
7) String Replace Function Fix
Text manipulation constitutes a major portion of the data science lifecycle, particularly during NLP.
There are several functions to perform various types of string operations such as concatenation, splitting, replacement, and many more. Two new operations have been added which are removesuffix() and removeprefix().
As the name implies, these are used to remove prefixes and suffixes from a string. They are accessible by using the string object.
Also, Read – 9 Best Linux Distros for Beginners & Programmers
Modules Enhanced in Python 3.9
There has been a major effort that took place while enhancing the existing modules. You can decide this with the fact that a large list of 35 modules has been updated to optimize the Python programming language.
Some of the most notable Python 3.9 new features occurred inside gc, http, imaplib, ipaddress, math, os, pydoc, random, signal, socket, time, and sys modules.
- The distutils bdist_msi command has now been depreciated.
- This function has now been deprecated, math.factorial() accepts float instances with non-negative integer values and raises a ValueError for non-integral and negative floats.
- Deprecated parser module.
- The utilization of NotImplemented in a boolean context has been depreciated.
- Deprecated hexbin4 and binhex4 standards.
- Idiom optimization for assigning a temporary variable in comprehensions.
- Optimized signal handling in multithreaded applications.
- Optimizing the subprocess module on FreeBSD using closefrom().
Python 3.9 is Faster Than Python 3.8
1) It Builds Faster
One of the biggest changes made to this version of Python is that Python 3.9 parser has been changed from an LL (1) type to a PEG type.
Changing the parser with a PEG version guarantees to get relieved of all of the nastiness and make adding language features easier.
The main points that you need to understand about the new parser are:
- It’s faster
- It uses up to 10% more memory
2) It Runs Faster
A Python 3.9 new feature uses optimizations in the background to decrease the number of temporary objects that get created and passed around. This resulted in the code running faster.
Python 3.9 is faster and uses much less memory than its previous version 3.8. Despite this, there’s always a place for speed improvements in Python, and they’re always appreciated.
Whenever a new version of the software is released, it brings a lot of new changes. We have discussed above some of the major and highlighted changes in Python 3.9 new features that include new operator, added features, and improved parser.
There are a lot of other changes in different modules like ast, asyncio, concurrent, futures, multiprocessing and XML. Some functions have also been depreciated.