What’s new in Python 3.9

python_3.9_features
0
3 min read

Some days back, a stable version of Python 3.9 is officially released with some new features and cool features. Although versions of Python 3.9 were released from time to time, the stable version is released now.

Here are some cool features in this version:

Dictionary Updation

There were many method for merging and updating dictionaries but the syntax was not that good or a bit cryptic.

With new merge (|) and update(|=) operators it becomes easy how we merge dictionaries. The merge and update operators work similarly to the concatenation(+) and in place concatenation(+=) in lists.

>>> a = {'1' : 'apple', '2' : 'mango'}
>>> b = {'3' : 'grapes' ,'4' : 'bananas'}
>>> a|b
{'1' : 'apple', '2' : 'mango', '3': 'grapes', '4': 'bananas'}

One thing to note here is that if there are some conflicts or key errors, then the value from right will be given preference.

Note :- This pipe(or merge) operator should not be used in series as this operator is in-efficient.

Type Hinting

Now, you don’t need to import types from typing library. You can use built-in types too.

For example, we can check type of variables and return type during run time of the code.

>>> def iseven(name: int) -> bool:
. . .      if number%2:
. . .         return False
. . .      else:
. . .         return True 

The above function will throw TypeError is the type defined don’t match.

String Suffix and Prefix

New methods, removeprefix and removesuffix methods are added in built in ‘str’ class to remove unneeded suffix and prefix.

>>> s='python is better'
>>> s.removeprefix('python is ')
'better'
>>> s.removesuffix(' is better')
'python'

Typological Sorting

A new module, graphlib is added to perform topological sorting in graph. It can help in scheduling tasks based on dependencies like installing python libraries that depends on each other.

GCD and LCM

Till now, math module supports HCF of two numbers like this:

>>> from math import gcd
>>> gcd(10,15)
5

As LCM is similar to GCD, there was no method to calculate LCM directly. But with python 3.9, you don’t need to define your own LCM function.

Also, Python below version 3.9 supports gcd of two numbers only and you have to iterate over the list or use reduce to find gcd. With python 3.9, it’s a lot easier.

>>> from math import gcd, lcm
>>> math.gcd(33, 45, 81)
3
>>> math.lcm(33, 45, 81)
4455

NOTE : – These functions can’t calculate based on lists. However, you can unpack it in comma separated values.

Randbytes

Random module has a new function randbytes to generate random bytes.

>>> from random import randbytes
>>> randbytes(6)
b'k\xcf\x07\x0c?\x95'

New HTTP Status Code

In python 3.9, new http status codes EARLY_HINTS(103), IM_A_TEAPOT(418), TOO_EARLY(425) are introduced.

>>> from http import HTTPStatus
>>> HTTPStatus.EARLY_HINTS
<HTTPStatus.EARLY_HINTS: 103>
>>> HTTPStatus.IM_A_TEAPOT.value
418
>>> HTTPStatus.TOO_EARLY.phrase
Too Early

Although there are some more features that are introduces, but only some of them are listed above. For more detailed explanation, visit official documentation here.

Choose your Reaction!
Leave a Comment