Python 3.9: What's New

Python 3.9: What's New

Python 3.9 doesn't bring any major new features to the table, but there are still some significant changes especially to how the language is developed and released. In Python 3.10, the old parser will be deleted and so will all functionality that depends on it (primarily the parser module, which has long been deprecated).

Python 3.9, which was released on October 5th 2020, doesn't bring any major new features to the table, but there are still some significant changes especially to how the language is developed and released.

Development Cycle

This is the first Python release coming from the new release philosophy, where major releases happen every 12-months (in October) rather than every 18-months. With releases being more frequent, changes should be smaller -- which is exactly what we're seeing in Python 3.9.

All new versions have 1.5 years of full support along with 3.5 years of security fixes.

Python release calendar

More info: https://www.python.org/dev/peps/pep-0602/

Dictionary Unions

Merge dictionaries

We now have a merge operator for performing dictionary unions: |. It works the same as a.update(b) or {**a, **b}, with one difference: It works for any instance of the dict subclass.

If you have Docker, you can quickly spin up a Python 3.9 shell to play around with the examples in this post via: docker run -it --rm python:3.9.

You can merge two dictionaries like so:

user = {'name': 'John', 'surname': 'Doe'}
address = {'street': 'Awesome street 42', 'city': 'Huge city', 'post': '420000'}

user_with_address = user | address

print(user_with_address)
## {'name': 'John', 'surname': 'Doe', 'street': 'Awesome street 42', 'city': 'Huge city', 'post': '420000'}

Now, we have a new dictionary called user_with_address, which is a union of user and address.

If there are duplicate keys in the dictionaries, then the output will display the second (rightmost) key-value pair:

user_1 = {'name': 'John', 'surname': 'Doe'}
user_2 = {'name': 'Joe', 'surname': 'Doe'}

users = user_1 | user_2
print(users)
## {'name': 'Joe', 'surname': 'Doe'}

| operator means union, not or. It doesn't do any bitwise work nor does it serve as logical operator. It's used to create a union of two dictionaries. Since it looks similar to an or condition in other languages, you may want to double check how it's being used during code reviews.

Updating dictionaries

As for updating, you now have the operator |=. This works in place.

You can update the first dictionary with keys and values from the second like so:

grades = {'John': 'A', 'Marry': 'B+'}
grades_second_try = {'Marry': 'A', 'Jane': 'C-', 'James': 'B'}

grades |= grades_second_try

print(grades)
## {'John': 'A', 'Marry': 'A', 'Jane': 'C-', 'James': 'B'}

It works for anything with keys and __getitem__ or iterables with key-value pairs:

## example 1
grades = {'John': 'A', 'Marry': 'B+'}
grades_second_try = [('Marry', 'A'), ('Jane', 'C-'), ('James', 'B')]
grades |= grades_second_try
print(grades)
## {'John': 'A', 'Marry': 'A', 'Jane': 'C-', 'James': 'B'}

## example 2
x = {0: 0, 1: 1}
y = ((i, i**2) for i in range(2,6))
x |= y
print(x)
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

## example 3
x | y
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'dict' and 'generator'

More info: https://www.python.org/dev/peps/pep-0584/

python programming developer web-development machine-learning

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Python Tutorial - Learn Python for Machine Learning and Web Development

Learn Python for Machine Learning and Web Development. Can Python be used for machine learning? Python is widely considered as the preferred language for teaching and learning ML (Machine Learning). Can I use Python for web development? Python can be used to build server-side web applications. Why Python is suitable for machine learning? How Python is used in AI? What language is best for machine learning?

Hire Python Developers

Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, our full-stack Python development services...

How To Plot A Decision Boundary For Machine Learning Algorithms in Python

How To Plot A Decision Boundary For Machine Learning Algorithms in Python, you will discover how to plot a decision surface for a classification machine learning algorithm.

Hire Python Developers India

Looking to build robust, scalable, and dynamic responsive websites and applications in Python? At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, we constantly endeavor to give you exactly what you need. If you need to...

Python Programs for Practice | Learn Python Programming

Python Programs for Practice will help you understand how to write various basic programs in python and real-time uses of some of the Python libraries as well.