New features in Python 3.8
Python’s latest version at the time of writing this article is 3.8.1. It was released on December 18, 2019. In this post, we list some of the new features in this version as compared to Python 3.7
- Walrus Assignment Operator
- Position only parameter
- Using = specifier with f-strings
- Syntaxwarning
- Improved math and statics function
Walrus Assignment Operator
- One of the biggest change in this version is the introduction of assignment operator. They are represented using the symbol
:= - They are also known as walrus operator due to their resembelance to the eyes and tusks of a walrus.
- They are generally used to assign and return values in the same expression.
For example in Python 3.7
foo = "something"
print (something)In Python 3.8 we could write this as
print (foo:="something")- The walrus operator can also be used inside
iforwhileblocks that calculate a value to test a particular condition.
# Check whether the first element of the list is 24
while (foo:= a[0]) == 24:
process (foo)Position Only Parameter
- The new function parameter syntax
/which indicates for some function parameters to be specified positionally. They can't be used as keyword assignments.
# a, b are positional only arguments
def foo(a, b, /, c, d):
print (a, b, c, d)
# You can use these as function calls
foo (8, 9, 10, d=11)
# These are invalid calls
foo(a=8, 9, 10, 11)
foo(8, b=9, 10, 11)- One possible use case of positional arguments would be to disallow use of keyword argument when argument name is not helpful. Consider the following example
# The definition of the built-in python function len
len(obj, /)
Return the number of items in a container.
>>> len(foo="something")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: len() takes no keyword arguments- Another benefit of marking an argument as position only is that it allows change of argument name in the future, without breaking the code
Using = specifier with f-strings
- One of my favourite addition is the support of
=operator tof-stringsspecifiers. This can be really helpful in debugging and self - documenting expressions.
In Python 3.7 we would use
#python 3.7
>>> num = 8
>>> print ("num=",num)
num= 8In Python 3.8, we would simple write this as
# python 3.8
>>> num = 8
>>> print(f'{num=}')
num=8Syntax Warning
Syntax Warning helps in better debugging and coding purposes. Rather than just displaying a SyntaxError, in this example the compiler displays SyntaxWarning as well
>>> [(10, 20) (30, 40)]
<stdin>:1: SyntaxWarning: 'tuple' object is not callable; perhaps you missed a comma?
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object is not callableImproved Math and Statistics Function
A couple of functions have been added to the math module. Some of them are
- math.prod
A function that analogous to the sum function returns the product of a ‘start’ value(default: 1) times an iterable of numbers.
>>> import math
>>> lst = [1, 2, 3, 4, 5]
>>> math.prod(lst)
120- math.isqrt
math.isqrt() computes accurate integer square roots without conversion to floating point. It supports arbitrarily large integers. According to the documentation
It is faster than
floor(sqrt(n))but slower thanmath.sqrt()
>>> math.isqrt(9)
3
>>> math.isqrt(15)
3
>>> math.sqrt(15)
3.872983346207417A lot more changes have been incorporated in Python 3.8. For a more comphrehensive list of changes you can have a look at the official documentation.
Get Hackadda’s stories in your inbox
Join Medium for free to get updates from this writer.
This was originally posted on HackAdda.
‘;







