View All
View All
View All
View All
View All
View All
View All
    View All
    View All
    View All
    View All
    View All

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

    By Rohit Sharma

    Updated on Jan 08, 2024 | 8 min read | 5.9k views

    Share:

    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

    upGrad’s Exclusive Data Science Webinar for you –

    ODE Thought Leadership Presentation

    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.

    background

    Liverpool John Moores University

    MS in Data Science

    Dual Credentials

    Master's Degree17 Months

    Placement Assistance

    Certification6 Months

    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.

    Frequently Asked Questions (FAQs)

    1. Why is Python preferred over other coding languages?

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

    3. What is Parser in Python?

    Rohit Sharma

    761 articles published

    Get Free Consultation

    +91

    By submitting, I accept the T&C and
    Privacy Policy

    Start Your Career in Data Science Today

    Top Resources

    Recommended Programs

    upGrad Logo

    Certification

    3 Months

    Liverpool John Moores University Logo
    bestseller

    Liverpool John Moores University

    MS in Data Science

    Dual Credentials

    Master's Degree

    17 Months

    IIIT Bangalore logo
    bestseller

    The International Institute of Information Technology, Bangalore

    Executive Diploma in Data Science & AI

    Placement Assistance

    Executive PG Program

    12 Months