This article would be interesting for you because we are discussing the new functionalities of the newly released python version which is Python 3.9.
It has more capabilities than the older versions. Let’s start.
Firstly python 3.9 was begun to develop from 4th June 2019. This article is about python Beta 4.0 and the version is scheduled as below
3.9.0 beta 5: Monday, 2020-07-20
3.9.0 candidate 1: Monday, 2020-08-10
3.9.0 candidate 2: Monday, 2020-09-14
3.9.0 final: Monday, 2020-10-05
so on 5th October 2020, we can use the final version of python 3.9.0. Let’s see some of the newly added features in short. So let’s get started.
String methods to remove prefixes and suffixes:
Two new functions added 1)removeprefix() 2)removesuffix().
1)removeprefix()
This function is added to remove the prefix from a string if it exists in the string. Otherwise, return the whole string as it is.
Example :
#with the current version:
if test_function_name.startswith(“test_”):
print(test_function_name[5:])
else:
print(test_function_name)
#with py3.9 version
print(test_function_name.removeprefix(“test_”))
2)removesuffix()
This function is added to remove the suffix from a string if it exists in the string. Otherwise, return the whole string as it is.
#with the current version:
def strip_quotes(text):
if text.startswith('"'): text = text[1:] if text.endswith('"'): text = text[:-1] return text
#with py3.9 version
def strip_quotes(text): return text.removeprefix('"').removesuffix('"')
why these functions are added:-
1)The code will not depend on the user to count the length of a literal.
2)The code does not require a call to the Python built-in len function nor to the more expensive str.replace() method. so performance will be increased.
3)he methods give a higher-level API for code readability as opposed to the traditional method of string slicing.
Add Union Operators To dict:
The PSF(Python Software Foundation) which is responsible for python development has added to more operators for dictionary data-structure for merging and updating the dictionary. These operators are added to the dict class. Lest’s see.
Dictionary Merge or Union operator (|) and Update operator (|=)
- older versions:
dic1 = {0:1, 1:1}
dic2 = {1:2, 2:3}
d3 = d1.copy()
d3.update(d2)
#Second Way to merge
dic1 = {0:1, 1:1}
dic2 = {1:2, 2:3}
dic3 = {**d1, **d2}
in both snippets, the values of d1 with equal keys as in d2 are updated by the values of d2. That’s the standard behavior of dictionaries: The last added value for a key wins.
- With Python 3.9 version you can write as below
dic1 = {0:1, 1:1}
dic2 = {1:2, 2:3}
dic3 = {2:4, 0:4}
#merge dic1 and dic2
merge = dic1 | dic2
#update dic1 with values of dic3
dic1 |= dic3
Here, Be Careful dic1 |= dic3 is different from dic3 |= dic1. since keys that appear in both operands will be updated with the values of the second operand.
A lot of changes are there in the newer version but most of the time or daily routine we use the above things. But a little bit of information I give about other things as below.
Built-in Generic types:-
- In type annotations you can now use built-in collection types such as list and dict as generic types instead of importing the corresponding capitalized types(e.g List or Dict)from typing. Some other types in the standard library are also now generic, for example, queue.Queue.
New Modules: zoneinfo and graphilb
- The zoneinfo module brings support for the IANA time zone database to the standard library. It adds zoneinfo.Zoneinfo, a concrete datetime.tzinfo implementation backed at the system’s time zone data
- Add the graphlib that contains the graphlib.TopolicalSorter class to offer functionality to perform topological sorting of graphs. Here topological sorting is nothing but Directed Acyclic Graph(DAG).
A deep understanding of the newly python version can be read from python.org site.
Thank you for reading the article and stay tuned for new python articles.
An enthusiastic python based full-stack web-developer and possessing vast knowledge
in python programming language with django,the highly scalable web-framework and good debugging skill in projects as well as possessing knowledge in new technologies like docker ,ngingx, implementing payment gateways and with highly recommended skills which increases productivity of a developer like public-speaking, leadership and teamwork with socializing also like to increase knowledge by solving different python based problems in different online platforms.