What’s new in Python 3.9?

A new version of Python is coming! Soon, we’ll be using it in our Python projects. As of 20/07/2020 Python 3.9 is in beta version(3.9.0b4) and will release the full version of Python 3.9 pretty soon.

Enough of introduction. Let’s get started and learn about the new features.

1. Dictionary merge and update operators : Python 3.9 introduces merge(|) and update(|=) operators in the dict class. If you have two dictionaries a and b, you can now use these operators to do merge and update them.

filter_none

edit
close

play_arrow

link
brightness_4
code

a = {1: "Sam", 2: "Alex"}
b = {3: "Daniel", 4: "Tom", 5: "Jimmy"}

chevron_right


You can use | to merge these both dictionaries.

filter_none

edit
close

play_arrow

link
brightness_4
code

c = a|b
print(c)

chevron_right


Output :



{1: "Sam", 2: "Alex", 3: "Daniel", 4: "Tom", 5: "Jimmy"}

If both the dictionaries have a common key, then the output will display the second key-value pair.

filter_none

edit
close

play_arrow

link
brightness_4
code

a = {1: "Sam", 2: "Alex", 3: "James"}
b = {3: "Daniel", 4: "Tom", 5: "Jimmy"}
  
c = a|b
print(c)

chevron_right


Output :

{1: "Sam", 2: "Alex", 3: "Daniel", 4: "Tom", 5: "Jimmy"}

For updating the dictionary, you can use the following operator :

filter_none

edit
close

play_arrow

link
brightness_4
code

a = {1: "Sam", 2: "Alex"}
b = {2: "Daniel"}
a |= b
print(a)

chevron_right


Output :

{1: "Sam", 2: "Daniel"}

2. removeprefix() and removesuffix() string methods : In Python’s str class, the new update introduces new removeprefix() and removesuffix() methods.

You can remove the prefix from any string using the removeprefix() method :

filter_none

edit
close

play_arrow

link
brightness_4
code

print('HelloWorld'.removeprefix('Hello'))

chevron_right


Output :

'World'

If the string doesn’t start with the input prefix, the copy of the original string will be returned.



filter_none

edit
close

play_arrow

link
brightness_4
code

print('BaseTestCase'.removeprefix('Test'))

chevron_right


Output :

'BaseTestCase'

If the string ends with input suffix, the output will look like string [:-len(suffix)] .

filter_none

edit
close

play_arrow

link
brightness_4
code

print('MiscTests'.removesuffix('Tests'))

chevron_right


Output :

'Misc'

If the input suffix is empty or the string doesn’t end with it, the output will be a copy of the original string.

filter_none

edit
close

play_arrow

link
brightness_4
code

print('BadTestCase'.removesuffix('Tmp'))

chevron_right


Output :

'BadTestCase'

3. New Parser : Python 3.9 uses a new parser which is a PEG-based parser. Previously, Python used LL(1). PEG is more flexible than LL(1) when it comes to building new features in the language. The official documentation says that this flexibility will be seen in Python 3.10 and later.

4. Type Hinting : Python dynamically specifies datatypes to a variable. For static allocation of data types, type hinting is used. This was introduced in Python 3.5. You can now use built-in collection types (list and dict ) as generic types. Earlier, you had to import the capital types (List or Dict) from typing.

filter_none

edit
close

play_arrow

link
brightness_4
code

def greet_all(names: list[str]) -> None:
    for name in names:
        print("Hello", name)

chevron_right


Now there is no need to call List from typing.List.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.