New features in Python 3.8

New features in Python 3.8

Editorial Team
Editorial Team

Python 3.8 has been released recently with many new awesome features.

This article will describe some of the new features of Python 3.8 which I believe every Python developer should be familiar with.

#1 Assignment expressions

This is one of the major changes in Python 3.8 is the introduction of Assignment expressions. It is written using := and is known as “the walrus operator”.

key = ''
while(key != 'q'):
    key = input("Enter a number or q to exit ")
    if key != 'q':

In Python 3.8, you can write the code as:

while((key:= input("Enter a number or q to exit ")) != 'q'):

#2 Positional-only arguments

Now we can use the new function parameter syntax / to indicate that some function parameters must be specified positionally and cannot be used as keyword arguments.

Take a look at this example.

def add(a, b):

add(2, 5)
add(b=5, a=2)

Here, the Add() function takes two mandatory parameters a and b. Therefore we can call the function by passing parameters in any of the following ways.

  • add( 2, 5 ) - Pass the arguments in the same order.
  • add( b=5, a=2 ) - Pass arguments as keyworded arguments.

Starting with Python 3.8, we can use / parameter to specify positional-only arguments.

The / parameter indicates that the arguments should be passed in the exact order and should not be passed as keyworded arguments.

See the example shown below.

def add(a, b, /):

add(b=5, a=2)

Note that we've added / as a parameter to the add() function. This restricts passing arguments as keyworded arguments. This code when executed will produce an error similar to the one shown below.

Traceback (most recent call last):
  File "", line 4, in <module>
    add(b=5, a=2)
TypeError: add() got some positional-only arguments passed as keyword arguments: 'a, b'

#3 Debugging made easier with f-strings

An f-string is a formatted string literal. It was introduced in Python 3.6.

name = "Python"
print(f'Hi from {name}')

This will display Hi from Python when executed.

In Python 3.8, we can add an = to the expression to evaluate the expression and display the output as well.

name = "Python"
print(f'Hi from {name=}')

This would produce the following output.

Hi from name='Python'

Here's another example.

a, b = 5, 6
print(f'The value of a = {a} and b = {b}')

# New in Python 3.8
print(f'The value of {a = } and {b = }')

The output will be:

The value of a = 5 and b = 6
The value of a = 5 and b = 6