What are the New features in Python 3.9
New Features in Python 3.9
Union Operator in the dictionary.
The dictionary union will return a new dictionary. The operands on the left-hand side are summed up with operand on the right-hand side. The required condition for performing union operation is that both must be a dict.
Example:
dict1 = { 1:’Rakesh’, 2:’Shyam’, ‘Ram’:’Varun’}
dict2 = { ‘Ram’:’Student’, ‘Priya’:’Architecture’}
dict1|dict2
{ 1:’Rakesh’, 2:’Shyam’, ‘Ram’:’Student’, ‘Priya’:’Architecture’}
dict2| dict1
{ ‘Priya’:’Architecture’, 1:’Rakesh’, 2:’Shyam’, ‘Ram’:’Varun’}
Type Hinting in Standard Collections
Generic can be parameterized by using a container. We can specify the data type of the excepted output.
Example:
def dictionary(r: dict) -> int:
return sum(r[key] for key in r)
test:dict = {‘test’:2, ‘demo’: 3}
dictionary(test)
From this way of specifying, the compiler is exactly knowing the data type of the variable they need to return.
Flexible variables and function annotation
Annotated always contains at least two parameters. The first argument is always data type. The other argument is the list of python values.
Syntax:
Annotated[int, ValueRange(2,10)]
Operation on string prefixes and suffixes
It becomes very easy to remove prefix and suffix in the string. Early, we go through each character of the string. Prefix and suffix can be removed only with the help of slicing. But now we have a separate function to remove prefixes and suffixes. Now, we don’t need to find the length of the string. It improves the performance of the code and also more descriptive.
“Hello”.removeprefix(“He”)
Output:
“llo”
It doesn’t make a major difference but helps in the performance of code.
New Parser
This is the most important feature which is used in the future released of the python. Python uses a predominant LL-based grammar which parses code left-right, top-down only with the help of one token. This is introduced to handle the unnecessary complexity in the program.
Garbage Collection
This feature helps in restoring the dead blocks again. Now, memory is utilized in an optimum way. Earlier, also python has the most effective memory management system.
To know more in-depth about the new features of Python 3.9. You can visit the link