The latest beta preview python 3.9.0b5 is released on July 20th, 2020. Yes, you heard it right. This is the last of the five planned beta release previews. This beta release gives an opportunity to the community to test the new features.

If you are planning to go for a python certification, make sure that you check out what functionalities are deprecated and what features are newly added in this release. Its always good to get yourself updated with the latest information. Without further ado, let’s go through the new features of Python 3.9.0b5.

Dictionary Merge and Update operators

In this release, the merge (|) and update (|=) operators are added to the built-in ‘dict’ class.

If we have data in two different dictionaries, we can combine them using the merge operator.

#Dictionary declaration
x = {1: 'Welcome', 2: 'to', 3: 'the'}
y = {4: 'HKR', 5: 'training'}

#Merging the dictionaries
z = x | y
print("The merged dictionaries are:", z)
The merged dictionaries are: {1: 'Welcome', 2: 'to', 3: 'the', 4: 'HKR', 5: 'training'}

The update operator is used to update an existing dictionary. Let’s see how that can be done.

#Dictionary declaration
x = {1: 'Welcome', 2: 'to', 3: 'the'}
y = {4: 'HKR', 5: 'training'}

#Updating the dictionaries
x |= y
print("The updated dictionary is:", x)
The updated dictionary is: {1: 'Welcome', 2: 'to', 3: 'the', 4: 'HKR', 5: 'training'}

You can see that all the elements of y dictionary are added to the x dictionary. If both the dictionaries contain a common key, then the second dictionary value will be updated. Let’s look at an example of that too.

#Dictionary declaration
x = {1: 'Welcome', 2: 'to', 3: 'the', 5: 'World'}
y = {4: 'HKR', 5: 'training'}

#Updating the dictionaries
x |= y
print("The updated dictionary is:", x)
The updated dictionary is: {1: 'Welcome', 2: 'to', 3: 'the', 4: 'HKR', 5: 'training'}

You can observe that the ‘5’ key exists in both x and y. So when the x dictionary is updated with y dictionary, the value ‘World’ is replaced with ‘training’.

Builtin Generic Types

Instead of importing the capitalized types like List and Dict, we can now use the built-in collection types list and dict as generic types.

New Parser

Python now has a new parser in this release based on PEG instead of LL(1). It parses code from top to down, left to right, with a lookahead of just one token. We can say that it is the most significant change for this release. The PEG-based parser allows python to be more flexible, especially when designing new language features.

We can start using this parser from Python 3.10 and later. The old parser along with the functionalities depending on it will be deprecated in python 3.10.

New string methods

Two new string methods – removeprefix() and removesuffix() have been introduced in this release.

If we want to remove an unwanted prefix, we can use removeprefix() method. The syntax for this method is StringName.removeprefix(prefix).

Let’s look at an example.

#String declaration
NewString = "##Hello World"
Result = NewString.removeprefix("##")
print("The result string is:", Result)
The result string is: Hello World

If we want to remove an unwanted suffix, we can use removesuffix() method. The syntax for this method is StringName.removesuffix(suffix). Here is a sample program that shows how the method works.

#String declaration
NewString = "Hello World##"
Result = NewString.removesuffix("##")
print("The result string is:", Result)
The result string is: Hello World

Type hinting

When coding in python, we don’t have to specify data types for the variables, since python is dynamically typed. But when we perform some operations on the variables, it is good to know what type of variable is expected in the expression.

Python 3.9 includes type hinting, which specifies type based warnings as hints. This is especially for the linters and code checkers. This ensures consistency in large codebases.

Other language changes

These are some other language changes that the python 3.9 includes.

1. If a relative import goes past its top-level package, we get ValueError. From this release, we will get ImportError instead of ValueError.

2. Python now gets the absolute path of the file given on the command line. Instead of being a relative path, the __file__ attribute of the __main__ module is now an absolute path.

3. We can use valid expressions as decorators.

4. The help is improved for the typing module.

Conclusion

Python has improved drastically over the last few releases. It is now focusing on improvements to speed up the execution. Now that you know about all the new features of python 3.9, start preparing your existing code projects to support the new feature release. Try and run your tests in the Python Development Mode that helps in preparing your code to be compatible with the next python version.