New good features in Python 3.9
- Python 3.9 is still in development and expected to release final version on October 2020
- Currently, 3.9.0a6 version is available for download as of April 29, 2020
- Note: Here, I have covered some of the major features of Python 3.9 and a complete list of of all features can found here
Merge two dictionaries (dicts) using union operators
# I am using interactive python interpreter (Python 3.9.0a6)
# create two dicts
>>> d1 = {1: 'one', 2: 'two', 3: 'three'}
>>> d2 = {4: 'four', 5: 'five', 6: 'six'}
# merge two dicts in python 3.9
>>> d1 | d2
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six'}
# merge two dicts in python (3.5 to 3.8)
>>> {**d1, **d2}
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six'}
Update dictionaries (dicts) union operators
# update current dict with key-values from other dict (like appending the dict)
>>> d1 = {1: 'one', 2: 'two', 3: 'three'}
>>> d2 = {4: 'four', 5: 'five', 6: 'six'}
# update d1 in python 3.9
>>> d1 |= d2
>>> d1
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six'}
# in python 3.8
>>> d1 = {1: 'one', 2: 'two', 3: 'three'}
>>> d2 = {4: 'four', 5: 'five', 6: 'six'}
>>> d1.update(d2)
>>> d1
{1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six'}
String methods (removeprefix()
and removesuffix()
)
# in python 3.9
>>> s = 'python_390a6'
# apply removeprefix()
>>> s.removeprefix('python_')
'390a6'
# apply removesuffix()
>>> s = 'python.exe'
>>> s.removesuffix('.exe')
'python'
# in python 3.8 or before
>>> s = 'python_390a6'
>>> s.lstrip('python_')
'390a6'
>>> s = 'python.exe'
>>> s.rstrip('.exe')
'python'
Note: removeprefix()
and removesuffix()
string methods added in Python 3.9 due to
issues associated with lstrip
and rstrip
interpretation of parameters passed to them.
Read PEP 616 for more details
math
functions
# in python 3.9
math.gcd() function can take more than two arguments
math.lcm(*integers), math.ulp(x), and math.nextafter(x, y) functions added
Check detailed usage
readlink()
function added to Path
module
# in python 3.9
>>> from pathlib import Path
>>> p = Path('/some/path/for/symlink_file')
>>> p.symlink_to('/some/path/for/existing_file')
# get the path for existing_file to which symlink_file points to
>>> p.readlink()
# should get PosixPath('/some/path/for/existing_file')
References
- https://docs.python.org/3.9/whatsnew/3.9.html
- https://www.python.org/dev/peps/pep-0596/
- https://www.python.org/dev/peps/pep-0584
- https://www.python.org/dev/peps/pep-0616
- https://docs.python.org/3.9/library/math.html
Last updated: May 1, 2020