Python 3.10 Released — Top 5 New Features You should Know

Image Source Canva — Edited By Varun Singh

1. Improved Error Messages

# I am coding in Python and this is first line ;)
my_list = ["Hello", "Python!"
print(my_list)
File "my_precious.py", line 3
print(my_list)
^
SyntaxError: invalid syntax
File "my_precious.py", line 2
news = ["Hello", "Python!"
^
SyntaxError: '[' was never closed
# missing_comma.py

dc_characters = {
1: "Superman" # Comma missing
2: "Batman",
3: "Joker"
}
....
Output:
File "dc_characters.py", line 4
10: "October"
^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

2. Easier Syntax for Type Union

# Before Python 3.10 Release
from typing import Union
def f(list: List[Union[int, str]], param: Optional[int]):
pass
# In Python 3.10 Release
def f(list: List[int | str], param: int | None):
pass
# Calling the function
f([1, "abc"], None)
int | str == typing.Union[int, str]

typing.Union[int, int] == int
int | int == int

3. Using multiple `with` statements across multiple lines

# Before Python 3.10 Releasewith (open("a_really_long_foo") as foo,
open("a_really_long_bar") as bar):
pass

Traceback (most recent call last):
File "<input>", line 1, in <module>
File "demo.py", line 19
with (open("a_really_long_foo") as foo,
^
SyntaxError: invalid syntax
# After Python 3.10 Releasefrom contextlib import contextmanager

@contextmanager
def f(x):
try:
yield x
finally:
pass
# Example 1
with f('c') as a,
f('a') as b:
pass
# Example 2
with f('c') as a,
f('a') as b,
f('a') as c:
pass

4. Better Type Aliases

# Before Python 3.10
UserInfo = tuple[str, int]
# In Python 3.10from typing import TypeAlias

Card: TypeAlias = tuple[str, str]
Deck: TypeAlias = list[Card]

5. Stricter Zipping of Sequences

# Before Python 3.10
names = ["Tom", "Harry", "Jessica", "Robert", "Kevin"]
numbers = ["21024", "75978", "92176", "75192", "34323"]
list(zip(names, numbers)).....
Output:
[(Tom, 21024), (Harry, 75978), (Jessica, 92176), (Robert, 75192), (Kevin, 34323)]
# Before Python 3.10
names = ["Tom", "Harry", "Jessica", "Robert"] # Kevin is missing
numbers = ["21024", "75978", "92176", "75192", "34323"]
# Zipping using zip()
list(zip(names, numbers))
......
Output
[(Tom, 21024), (Harry, 75978), (Jessica, 92176), (Robert, 75192)]
# In Python 3.10
names = ["Tom", "Harry", "Jessica", "Robert"] # Kevin is missing
numbers = ["21024", "75978", "92176", "75192", "34323"]
# Zipping using zip() with additional Parameter strict=True
list(zip(names, numbers, strict=True))
....
Output:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: zip() argument 2 is shorter than argument 1

Other Updates in Python 3.10

More About The Author😄

Data Analyst turned Software Developer and Blogger. Enjoys a hot cup of tea and nature’s spell.