Python Programming for Beginners Part -11 (What’s new in Python 3.10?)
I am back with new article on what’s new in Python 3.10. It’s beta version is now available. I am excited to share some new and prominent features of python 3.10 and I am sure you will find this stuff useful to you.
Let’s begin the discussion…
Python 3.10, beta version is released recently. We will explore some of the most important features included in Python 3.10. The main moto of new python beta version to make programming pythonic and more convenient and make easier to exception handling etc.
Let’s see some of the prominent features of python 3.10 beta version:
Better error messages
The new version of python can better deal with error messages that helps programmers to debug codes in easier, faster manner. Syntax error is very common error committed while coding and precise syntax error messages will certainly help python coders.
Let’s see some of the notable new SyntaxError exceptions are introduced in the new beta version. These are given below:
- Missing commas (,) in collection literals and between expressions
- Missing colon (:) before blocks
- Missing colon (:) and values in dictionary literals
- Un-parenthesized tuples in comprehensions targets
- Try blocks without except or finally blocks
- Multiple Exception types without parentheses
- Assignment operator (=) instead of comparison operator (==) in comparisons
- Usage of * in f-strings
Some specific indentationError exceptions are introduced in the context regarding what kind of an indentation was expected by the block that indicates the location of the statement.
Structural Pattern Matching
It is one of the most expected features of Python 3.10. The new version of python introduces a switch/case-like feature that is available in most of the programming languages like C, C++, and java and so on to Python. Moreover, Python 3.10 Structural Pattern Matching gives a variety of added features, creating it even more powerful than the typical switch conditional statement in other programming languages. Python uses the match keyword instead of switch with the case keyword defining each different scenario. The generic syntax of pattern matching is:
Here last case _: is the wildcard_ expression and if an exact match is not found, it will be applied as the matching case. If an exact match is not found and the code does not have a wildcard case, the entire match block is a no-op. For example,
return “Pass with honours”
return “No comment!!!”
We can also combine several literals in a single case pattern using | (or). For example.
case 40 | 45 | 50:
Structural Pattern Matching with Guards
Guards allow us to follow the top pattern, an if statement that evaluates the subsequent case statement if the guard evaluates to true. For example:
case [b, r] if b > NUM_OF_BALLS and r > NUM_OF_RUNS:
print(“Scored runs in number of balls”)
case r if r > NUM_OF_BALLS:
print(“Strike rate is more than 100%”)
case [b, r] if b == r:
print(“Scored run a ball”)
In Python 3.10, other pattern matching features includes classes, literals and variables, and nested patterns that allow us to produce complex conditional statements.
The bit_count is a new method introduced in Python 3.10. It returns the number of 1’s present in the binary number of an integer. It is also called population count (popcount). It provides us a faster means for counting non-zero bits in an integer number
New Type Union Operator
Python 3.10 offers an improved way of type union checking syntax. It supports to | (or) operator with type annotations and new version also supports functions like isinstance() and issubclass(). For example,
def func(num1: int | float) -> int | float:
We can see another example to demonstrate use of with isinstance() function.
isinstance(“Welcome to Python 3.10”, int | str)
In nutshell, we can say that the new version has included some of the interesting features that will empower python programmers to write code in pythonic way. It will make ease of exception handling. The new version also introduced much awaited way of structural pattern matching with match/case feature.
On wrapping up notes, feel free to share your comments. Your claps and comments will surely help me to present contents in a better way. See you next week.