Top 6 New Features in Python 3.8



This blog post will describe some of the top features added to Python 3.8. Are you one of those Python people like me that are resistant to updating your Python version? Well.. let’s see if this blog post will change your mind to update your Python to version 3.8 – maybe you will like the cool python f string change.

1. Better f-strings

Some people like the “printf-style”. Python 3.8 now supports “=”

On Python 3 before 3.8, the f-string would be such as:

>>> foo = 1
>>> bar = 2
>>> print(f"foo={foo} bar={bar}")
>>> foo=1 bar=2

Python 3.8 now allows using the string type “foo”, and then the expression “foo”.

>>> foo = 1
>>> bar = 2
>>> print(f"{foo=} {bar=}")
>>> foo=1 bar=2

2. Euclidean Distance

Remember when you need to write some python code to compute the euclidean distance? Python 3.8 now includes it in the math module now includes a method where you can use to compute the euclidean distance between numbers in two lists.

>>> import math
>>> p = range(10)
>>> q = range(10)
>>> math.dist(p,q)
>>> 0.0

3. Create Normal Distribution

Now you don’t need to use numpy to generate a normal distribution sequence. Python 3.8′ statistics.NormalDist(mu=0.0, sigma=1.0) does the job for you an object where mu represents the arithmetic mean and sigma represents the standard deviation.

4. New syntax warnings when missing commas (,)

Before Python 3.8 was not so obvious when listing items in a list and a comma was missed. Python 3.6, for example, gives the following SyntaxError:

>>> my_list = [1,2,3,4 5]
>>> my_list
    File "<input>", line 1
    my_list = [1,2,3,4 5]
SyntaxError: invalid syntax

Python 3.8 has improved the SyntaxError message which makes so much easier to debug:

>>> my_list = [1,2,3,4 5]
  File "<stdin>", line 1
    my_list = [1,2,3,4 5]
SyntaxError: invalid syntax

The same should probably be true on other structures such as tuples and dict.

5. ‘Continue’ is now legal in ‘finally:’ blocks

You can now use a continue in a finally block as you can see below and not throw a `SyntaxError: ‘continue’ not supported inside ‘finally’ clause` error message.

>>> try:
>>>    print(random_variable)
>>> except:
>>>    print(1)
>>> finally:
>>>    continue 

6. dict takes reversed()

Now reserved can be used in python dictionaries behaving like OrderedDict(), and we can reverse the order of keys entry in the dict, and not get a TypeError: ‘dict_items’ object is not reversible error.

>>> tmp_dict = {}
>>> tmp_dict[2] = 1
>>> tmp_dict[1] = 1
>>> tmp_dict[3] = 1

>>> list(reversed(tmp_dict.items()))
>>> [(3, 1), (1, 1), (2, 1)]

So? Are you doing to update your Python version to Python 3.8? Did you like the new Python 3.8 new features? What was your favorite new feature? Please comment below!

More Resources

Here are three of my favorite Python Books in case you want to learn more about it.

Related Posts