Brand New Python Features In 2021 🚀

Mr. Unity Buddy's photo
Mr. Unity Buddy
·May 30, 2021·

4 min read

Brand New Python Features In 2021 🚀

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Hello, buddies! We know, When technology updates everyday, Developers must be updated. So, today we are going to see the brand new features of Python, you may not know yet.

Assignment Expressions

The biggest change in Python 3.9 is the introduction of assignment expressions. They are written using a new notation (:=). This operator is often called the walrus operator as it resembles the eyes and tusks of a walrus on its side.

Assignment expressions allow you to assign and return a value in the same expression. For example, if you want to assign to a variable and print its value, then you typically do something like this:

walrus = False
>> False

In Python 3.9, you’re allowed to combine these two statements into one, using the walrus operator:

 print(walrus := True)

The assignment expression allows you to assign True to walrus, and immediately print the value. But keep in mind that the walrus operator does not do anything that isn’t possible without it. It only makes certain constructs more convenient, and can sometimes communicate the intent of your code more clearly.

Positional-Only Arguments

There is a new function parameter syntax “/” which indicates that some function parameters must be specified positionally and can’t be used as keyword arguments. The addition of “/” improves the language’s consistency and allows a robust API design.

# Arguments before / are considered
# as positional arguments only
def add(x, y, /, z = 0):
    a = x + y + z
    return a

# Driver's code
print(add(2, 5))
print(add(2, 5, 7))
print(add(x = 2, y = 5))

So The marker “/” means that passing values for x and y can only be done positionally, but not using keyword arguments.

f-strings now support =

This string formatting mechanism is known as Literal String Interpolation or more commonly as F-strings (because of the leading f character preceding the string literal). The idea behind f-strings is to make string interpolation simpler. Python 3.9 allows the use of the above-discussed assignment operator and equal sign (=) inside the f-strings. For example, say we have two variables “a” and “b”, and we want to print “a + b” along with the result. Here we can use f-strings=.

a = 5
b = 10

# Using = at the end of
# f-strings
print(f'{a + b = }')

# Using assignment operators
# inside f-strings
print(f'{(c := a + b)}')

print("The value of c:", c)

Output will be,

a + b = 15
The value of c: 15

This is very useful during debugging, especially for everyone who uses print() statements for debugging.

reversed() works with a dictionary

Unlike Python 3.7, now in Python 3.9, the built-in method reversed() can be used for accessing the elements in the reverse order of insertion.

# Declaring a dictionary
my_dict = dict(x = 1, y = 2, z = 3)

# Prints only keys

# Prints the key-value pair
# as a list of tuples


['z', 'y', 'x']
[('z', 3), ('y', 2), ('x', 1)]

No parentheses for return and yield statements

yield and return statements do not require parentheses to return multiple values.

For example:

def parse(family):
  lastname, *members = family.split()
  return lastname.upper(), *members

print(parse('Charles David John Sam'))

Will give us,

('CHARLES', 'David', 'John', 'Sam')

pow() function

In the three-argument form of pow(), when the exponent is -1, it calculates the modular multiplicative inverse of the given value.

For example, to compute the modular multiplicative inverse of 38 modulo 137:

print(pow(38, -1, 137))



Dictionary comprehension

Dict comprehensions have been modified so that the key is computed first and the value second:

>>> data = {input('Name: '): input('Age: ')}
Name: Charles
Age: 40

csv module

The csv.DictReader now returns instances of dictionary instead of a ‘collections.OrderedDict’.

Of course, Performance!

  • In Python 3.9, the multiprocessing module offers a SharedMemory class that allows regions of memory to be created and shared between different Python processes. Shared memory provides a much faster path for passing data between process, which eventually allows Python to use multiple processors and multiple cores more efficiently.

  • Many built-in methods and functions have been sped up by 20% to 50%.

  • Newly created lists are now on average 12% smaller than before
  • Writes to class variables are much faster in Python 3.9.

So buddies, this is how Python is updated. Happy Coding with new Python!

Share this