Discussion on the difference between Python3.10 and Python3.9

  • 2021-11-29 07:37:06
  • OfStack

Directory introduction: Understanding Python and its use cases: The difference between Python 3.9 V/s Python 3.10 was analyzed Python 3.9: IANA Time Zone Database
Functions for merging and updating dictionaries
Delete prefixes and suffixes
Use type hints for built-in generic types in Python 3.9
Python 3.10: Improved Syntax Error Messages
Better type hints

Introduction:

Over the past decade, Python has created a name for itself in the programming or scripting language arena. The main reason why python is highly favored is its extreme user-friendliness. Python is also used to deal with complex programming or coding challenges. Emerging fields such as machine learning (ML), artificial intelligence (AI) and data science also meet the high demand for learning this language. Compared with traditional languages such as Java, C #, and others, Python is a powerful programming language that is rapidly becoming a favorite among developers, data scientists, and AI/ML enthusiasts.

As a programming language, Python has many use cases that attract learners and experts in the IT industry. At a basic level, Python can be used as a programming language to practice data structures and algorithms or to develop simple projects or games. The versatility of Python as a language allows its users to easily extend their projects and create websites, software, or prediction models. Automation is taking over most of the IT industry, and Python is in the leading position as the preferred language for automating data analysis or data science tasks. In addition, Python has a large library and a strong community of programmers who continue to add more value to Python as a language.

Understanding Python and its use cases:

One of the many reasons beginners are attracted to Python is its user-friendliness. Python discards the formidable semicolon and uses a simple indentation structure as its syntax. Python also found a use case as an extension of an application that requires a programmable interface. One of the other benefits of Python includes its most coveted feature, its library. The Python library is a huge resource that can be used for many key code writing, such as:

Code based on regular expressions String processing Internet protocols such as HTTP, FTP, SMTP, XML-RPC, POP, IMAP Unified 1 code Differences between file systems and calculated files CGI programming Mathematical modeling Database query Data analysis Data visualization Automation code

All of these functions can be performed on many Unix, Linux, macOS, and Windows systems.

The difference between Python 3.9 V/s Python 3.10 was analyzed

Over the years, Python has undergone a number of upgrades, and many features have been added in the new version. Here, let's pay attention to the two latest versions added by Python. Exploring the updated functionality can help you use it smoothly, and of course, you can find smarter ways to work with the update library. All of the code attached below is for educational purposes only and is taken from the original Python documentation released with new releases (e.g. Python 3.9 and Python 3.10) 1

Python 3.9:

IANA Time Zone Database

A new module named zoneinfo was created in Python 3.9. With this module, you can access the IANA or the Internet Authority for Assigned Numbers Time Zone database. By default, this module uses the local time zone data of the system.

Code:


print(datetime(2021, 7, 2, 12, 0).astimezone())
print(datetime(2021, 7, 2, 12, 0).astimezone().strftime("%Y-%m-%d %H:%M:%S %Z"))
print(datetime(2021, 7, 2, 12, 0).astimezone(timezone.utc))

Output:
2020-07-2 12:00:00-05:00
2020-07-2 12:00:00 EST
2020-07-2 17:00:00+00:00

Functions for merging and updating dictionaries

Python 3.9 added another cool feature that attracted a lot of attention. Python 3.9 can now merge or update dictionaries using operators. The new operators ie () and (=) have been added to the Python 3.9 built-in dict class. You can access these operators to merge or update dictionaries using code similar to the markup below.
Code:


>>> a = { ' v': 1, 'art': 2, 'py': 3}
>>> b = {'v': 'd', 'topic': 'python3.9'}

Merge code:


>>> a | b
{'art': 2, 'py': 3, 'v':'d',  'topic': 'python3.9'}
>>> b | a
{'v': 1,'art': 2, 'py': 3, 'topic':'python3.9' }

Update code:


>>> a |= b
>>> a
{'art': 2, 'py': 3,'v':'d'}

Delete prefixes and suffixes

String processing issues can be solved more easily using new features added in Python 3.9. The code marked below is used to remove prefixes and suffixes from the sample string. The new method used in the following sample code is:

removeprefix ()-This method is properly named for its function, which is to remove any prefixes that exist in a given sample string. removesuffix ()-This method removes the existing suffix from the sample string passed to it.

These new methods were created to replace the old strip () method due to the programmer's negative evaluation of its defect nature. Marked below is a sample code that can help you understand the implementation of these two new methods.
Code:


print(" The sea is playing outside ".removeprefix(" Sea embrace "))

Output:
'Playing outside'

Use type hints for built-in generic types in Python 3.9

Python version 3.9 enables support for common syntax for all standard collections, which are currently available in the input module. Generic types are usually defined as one container, such as one list. It is a type that can be easily parameterized. Typically, generic types have parameters of one or more types, while parameterized generics are specific instances of generic data types with container elements, for example, list or dictionary built-in collection types are supported types, not specifically supported types using Typing. Dict or typing. List

Code:


def print_value(input: str): #  Specifies that the passed value will be of type string 

By using the following method, we will be able to find out whether the following input is a string

Python 3.10:

Matching using structural patterns
A new feature called Structural Pattern Matching is introduced in the new Python 3.10. This matching process runs with the same matching case logic 1, but it also compares with comparison objects to track a given pattern.

Code for Python 3.9:


http_code = "419"
if http_code == "200":
    print("OK")
elif http_code == "404":
    print("Not Found Here")
elif http_code == "419":
    print("Value Found")
else:
    print("Code not found")

Code for Python 3.10:


http_code = "419"
match http_code:
    case "200":
        print("Hi")
    case "404":
        print("Not Found")
    case "419":
        print("You Found Me")
    case _:  #Default Case
        print("Code not found")

Improved Syntax Error Messages

A large number of programmers face difficulties in mismatching or debugging code. Python 3.10 adds a very user-friendly feature called Association Recommendations, which is marked with syntax error messages. This helps you quickly find fixes for code with errors or errors.
Code:


named_car = 77
print(new_car)

Output:
NameError: name 'new_car' is not defined. Did you mean: named_car?

Better type hints

Upgrading from Python 3.9, we can allocate multiple input types of parameters using only OR symbols instead of union keywords. Defining multiple input types for the same variable is a simpler method

Code for Python 3.9:


def add(a: Union[int, float], b: Union[int, float]):

Code for Python 3.10:


>>> a = { ' v': 1, 'art': 2, 'py': 3}
>>> b = {'v': 'd', 'topic': 'python3.9'}
0

Improved context manager

Context managers help with resources such as files. You can now use multiple contexts in a single block. This will greatly enhance your code, because you no longer need multiple blocks or statements.
Previous syntax:


>>> a = { ' v': 1, 'art': 2, 'py': 3}
>>> b = {'v': 'd', 'topic': 'python3.9'}
1

Latest syntax:


>>> a = { ' v': 1, 'art': 2, 'py': 3}
>>> b = {'v': 'd', 'topic': 'python3.9'}
2

Related articles: