It’s that time again, a new version of Python is imminent. Now in beta (3.9.0b3), we will soon be seeing the full release of Python 3.9.

Some of the newest features are incredibly exciting, and it will be amazing to see them used after release. We’ll cover the following:

  • Dictionary Union Operators
  • Type Hinting
  • Two New String Methods
  • New Python Parser

— this is very cool

Let’s take a first look at these new features and how we use them.

Dictionary Unions

One of my favorite new features with a sleek syntax. If we have two dictionaries a and b that we need to merge, we now use the union operators.

We have the **merge **operator |:

a = {1: 'a', 2: 'b', 3: 'c'}
b = {4: 'd', 5: 'e'}
c = a | b
print(c)

[Out]: {1: ‘a’, 2: ‘b’, 3: ‘c’, 4: ‘d’, 5: ‘e’}

And the **update **operator |=, which updates the original dictionary:

a = {1: 'a', 2: 'b', 3: 'c'}
b = {4: 'd', 5: 'e'}
a |= b
print(a)

[Out]: {1: ‘a’, 2: ‘b’, 3: ‘c’, 4: ‘d’, 5: ‘e’}

If our dictionaries share a common key, the key-value pair in the second dictionary will be used:

a = {1: 'a', 2: 'b', 3: 'c', 6: 'in both'}
b = {4: 'd', 5: 'e', 6: 'but different'}
print(a | b)

**[Out]: **{1: ‘a’, 2: ‘b’, 3: ‘c’, 6: ‘but different’, 4: ‘d’, 5: ‘e’}

Dictionary Update with Iterables

Another cool behavior of the |= operator is the ability to **update **the dictionary with new key-value pairs using an iterable object — like a list or generator:

a = {'a': 'one', 'b': 'two'}
b = ((i, i**2) for i in range(3))
a |= b
print(a)

#python #python-programming #latest-tech-stories #python-top-story #python3 #learn-python #programming #python-tips

What Are The New Features in Python 3.9?
2.80 GEEK