Blog_Banner_Asset
    Homebreadcumb forward arrow iconBlogbreadcumb forward arrow iconData Sciencebreadcumb forward arrow iconPython 3.9 : What’s New Now? Cool Features To Checkout

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

Last updated:
18th Jun, 2023
Views
Read Time
8 Mins
share image icon
In this article
Chevron in toc
View All
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. 

Check out all trending Python tutorial concepts in 2024.

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

Performance Enhancements

Python 3.9 includes a number of optimizations that improve performance. An important advancement is the addition of a new string representation dubbed “compact unicode.” With this encoding, strings take up less memory and perform better when there are many strings involved. Additionally, a number of built-in procedures and routines have been accelerated to run more quickly, improving program performance overall.

Dictionary Key Insertion Order Guaranteed

Dictionary Key Insertion Order is guaranteed to start with Python 3.7. This behavior is extended to dictionary comprehension in Python 3.9, ensuring that the dictionaries produced by comprehension retain the keys’ original order. With this modification, developers can depend on the dictionary components’ intended sequence while also simplifying their code.

New Syntax for Type Hinting Variables

In Python 3.9, a new syntax is added that allows you to explicitly declare a variable’s type without initializing it. Developers can use this functionality to specify type hints for variables that will receive values later in the code. For instance:

x: int

# Type hint for variable x without initialization

# Later in the code: x = 42

Type Hints in a Union

In Python 3.9, the Union type hint allows for the specification of numerous types inside of a single union. Using this, developers can specify that a variable or function parameter accepts values of various types. For example:

From typing import Union

def process_data(data: Union[str, bytes]):

    # Function code here

Error Messages

Python 3.9 improves error messages to deliver more thorough and educational feedback. Developers may now identify and fix problems more quickly thanks to the inclusion of more context in error messages, such as line numbers and pertinent code fragments.

New Syntax Warning

Python 3.9 alerts programmers to the use of deprecated or discouraged syntax. This warning is intended to draw attention to potentially dangerous code structures and to persuade programmers to use more preferable and up-to-date substitutes. Developers can design code that is future-proof and keep current with best practices by being aware of these warnings.

Updates to Standard Library Modules

Python 3.9 includes updates to “heapq,” “collections,” “statistics,” and “type,” among other standard library modules. These upgrades boost usability and speed across all aspects of Python development while adding new features and improving current ones. By keeping up with these upgrades, developers can use their applications’ newest features and advancements.

Better Debugging and Troubleshooting

The “pdb” debugger has been improved in Python 3.9, making debugging easier to understand and use. The debugger now has features like syntax highlighting and “sticky” mode, which remembers the most recently issued commands for easier code reading during debugging sessions. These enhancements enable quicker problem-solving and more effective bug-fixing.

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.

Profile

Rohit Sharma

Blog Author
Rohit Sharma is the Program Director for the UpGrad-IIIT Bangalore, PG Diploma Data Analytics Program.

Frequently Asked Questions (FAQs)

1Why 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.

2 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.

3 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.

Explore Free Courses

Suggested Blogs

Most Common PySpark Interview Questions & Answers [For Freshers & Experienced]
20826
Attending a PySpark interview and wondering what are all the questions and discussions you will go through? Before attending a PySpark interview, it’s
Read More

by Rohit Sharma

05 Mar 2024

Data Science for Beginners: A Comprehensive Guide
5062
Data science is an important part of many industries today. Having worked as a data scientist for several years, I have witnessed the massive amounts
Read More

by Harish K

28 Feb 2024

6 Best Data Science Institutes in 2024 (Detailed Guide)
5149
Data science training is one of the most hyped skills in today’s world. Based on my experience as a data scientist, it’s evident that we are in
Read More

by Harish K

28 Feb 2024

Data Science Course Fees: The Roadmap to Your Analytics Career
5074
A data science course syllabus covers several basic and advanced concepts of statistics, data analytics, machine learning, and programming languages.
Read More

by Harish K

28 Feb 2024

Inheritance in Python | Python Inheritance [With Example]
17581
Python is one of the most popular programming languages. Despite a transition full of ups and downs from the Python 2 version to Python 3, the Object-
Read More

by Rohan Vats

27 Feb 2024

Data Mining Architecture: Components, Types & Techniques
10763
Introduction Data mining is the process in which information that was previously unknown, which could be potentially very useful, is extracted from a
Read More

by Rohit Sharma

27 Feb 2024

6 Phases of Data Analytics Lifecycle Every Data Analyst Should Know About
80573
What is a Data Analytics Lifecycle? Data is crucial in today’s digital world. As it gets created, consumed, tested, processed, and reused, data goes
Read More

by Rohit Sharma

19 Feb 2024

Sorting in Data Structure: Categories & Types [With Examples]
138957
The arrangement of data in a preferred order is called sorting in the data structure. By sorting data, it is easier to search through it quickly and e
Read More

by Rohit Sharma

19 Feb 2024

Data Science Vs Data Analytics: Difference Between Data Science and Data Analytics
68952
Summary: In this article, you will learn, Difference between Data Science and Data Analytics Job roles Skills Career perspectives Which one is right
Read More

by Rohit Sharma

19 Feb 2024

Schedule 1:1 free counsellingTalk to Career Expert
icon
footer sticky close icon