Programs

Python 3.9 : What’s New Now? Cool Features To Checkout

Python is an all-time favorite and most favored language among developers. According to The Tiobe index which is a programming community that ranks programming languages according to their popularity, Python has been ranked second overtaking Java. Python has excellent community support and due to its simplicity and easy to use nature, every beginner aspires to master at least this language.

While this language has grasped a lot of eyeballs in recent years finding its usage in the field of data analytics, machine learning, web development, computer application, IOT or other development areas, its latest version Python 3.9 stable has been released and it brings a lot of changes from the parser to additional functionalities to change in the feature releases timeline. Let’s look at these changes in detail:

New Operator

While dealing with dictionaries, it is very often that we need to merge or update them with entries from other dictionaries. Earlier this could be done using the update attribute of dictionary object: first_dict.update(second_dict) but the problem with this approach is that the changes are done in-place, and original values are modified.

To make it a temporary change, another variable is required to store the previous value. This new version has introduced a new operator known as a merge operator (|) which can be used to merge dictionaries. Here is a code example to understand this:

A = { “name” : “Author”, “company” : “upGrad”}

B = { “status” : “instructor”}

print (A|B)

Output:  { “name” : “Author”, “company” : “upGrad”,  “status” : “instructor”}

| is like a concatenation operator but if you want to update the original value then you can simply use the |= operator. Continuing the previous example, A |=B will update the value of A dictionary. 

Also read: Python Developer Salary in India

String Operations

Text manipulation forms a major part of the data science lifecycle, especially during NLP. There are different functions to perform different types of string operations such as splitting, concatenation, replacement, and many more. Joining this legacy, two new operations have been added which are removesuffix() and removeprefix().

As the name suggests, these are used to remove prefixes and suffixes from a string. They are accessible by using the string object. This removal could be easily done using regular expressions and slicing of the strings but now we have this functionality in built-in libraries. Here is a simple example for illustration purpose:

‘@something_new’.removeprefix(‘@’)

‘something_231’.removesufix(‘231’)

Output:

something_new

something_

Modified Timezone Module

This version has also introduced a new module named zone info supporting the IANA database in the standard library. We can specify the timezone in the DateTime object from the standard library but the user can end up creating complex rules for the time zones. As per PEP 615, the userbase is more inclined towards major time zones such as UTC, IANA, or system local time zone. Now you can create a zone info object by specifying the config search path which is of the form “continent/city”. For instance,

from zoneinfo import ZoneInfo

from datetime import datetime

dt = datetime(2020, 11, 15, 01, tzinfo=ZoneInfo(“Asia/Kolkata”))

dt.tzname()

Output: ‘IST’

Learn about: Top Python tools

Top Data Science Skills to Learn

upGrad’s Exclusive Data Science Webinar for you –

ODE Thought Leadership Presentation

Explore our Popular Data Science Certifications

New Math Functions

The math module consists of all numerical operations that are used in general mathematics. Whether it’s sine, cosine functions, rounding off numbers to specific places, or universal constants such as pi or exponential, the math module covers almost all types of functions.

Three new functions have been added to this list and there is math.lcm(), math.nextafter(), math.ulp(). While the LCM (Least Common Multiple) is a very commonly used function, the other two are not used as much. Earlier, we had to explicitly create a function for LCM operation but now starting this new version, we have a built-in functionality in the standard library. To use this function simply call:

import math

math.lcm(2,3) 

Output: 6

If no arguments are passed in this function then it returns 1 and if one of them is zero then it returns 0.

Our learners also read: Top Python Free Courses

High-Performance Parser

Apart from all the new additions on the feature level, the Python language parser is also updated to new, faster technology known as PEG parser. This effect will not reflect in this version but the upcoming Python 3.10. From the day Python was created, it has been using LL(1) parser which parses the code from top to bottom and left to right. This parser is based on context-free grammar and therefore it is ambiguous meaning that the strings can have more than one parse tree and increasing the number of intermediate steps. The new PEG parser generates only one valid AST (Abstract Syntax Trees) removing the ambiguity of the previous parser. 

New Random Function and Change in Release Cycle

In the random.random module, new method randbytes(n) has been added which returns random n bytes. It is very similar to other random functions. Also previously, Python releases a new version every 18 months which has been reduced to 12 months.

Get data science certification from the World’s top Universities. Learn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Read our popular Data Science Articles

Conclusion

Whenever a new version of software releases, it accompanies a lot of new changes. Here we have discussed some of the major and highlighted changes in Python which include new operator, added features, and improved parser.

There are a lot of new changes in other modules also such as ast, asyncio, concurrent.futures, multiprocessing, XML. Some functions have been depreciated and the list goes on. You can check the official documentation for a full list of updates in the new Python 3.9. 

If you are curious to learn about python, data science, check out IIIT-B & upGrad’s PG Diploma in Data Science which is created for working professionals and offers 10+ case studies & projects, practical hands-on workshops, mentorship with industry experts, 1-on-1 with industry mentors, 400+ hours of learning and job assistance with top firms.

Why is Python preferred over other coding languages?

There are numerous reasons why Python is preferred over other languages. Firstly, Python's codes are legible and understood even by developers who have never written them before. Python is common in the scientific community, and numerous researchers and professors are among Python's users. Codes made on Python can be shared, copied, and changed by the developers. Also, Python has a strong community and is rapidly evolving since it is a free and open programming language. Python is compatible with a wide range of systems. It helps developers as they are not confronted with difficulties that are prevalent in other languages.

What is the use of math.nextafter() and math.ulp() in Python?

The nextafter() and ulp() are two new functions added to the math library in Python 3.9.0. The nextafter() function returns the next float value after x in the direction of y. If x equals y, the value y is returned. Its syntax is math.nextafter(x, y), where x and y are two integers/floating-point values. ULP is an abbreviation meaning Unit in the Last Place. ULP is the space between floating-point values in numerical analysis and computer science. The math.ulp() function returns the value of the float x's least significant bit. Its syntax is mathematical ulp(x), and the argument to be input, x, must be a float, whose ulp must be returned.

What is Parser in Python?

During the syntax analysis step, a compiler examines whether or not the tokens created by the lexical analyzer are organized according to the language's syntactic rules. A parser is responsible for this. The parser gets a string of tokens from the lexical analyzer and validates that the string corresponds to the grammar of the source language. Any syntax problems are detected and reported, and a parse tree is produced from which developers can generate intermediate code. Also, the parser module communicates with Python's internal parser and byte-code compiler. This interface's main purpose is to allow Python codes to alter the parse trees of a Python expression and generate working codes from it.

Want to share this article?

Prepare for a Career of the Future

Leave a comment

Your email address will not be published. Required fields are marked *

Leave a comment

Your email address will not be published. Required fields are marked *

×
Get Free career counselling from upGrad experts!
Book a session with an industry professional today!
No Thanks
Let's do it
Get Free career counselling from upGrad experts!
Book a Session with an industry professional today!
Let's do it
No Thanks