Verify Certificate
Verify Certificate

Cool New Features in Python 3.8 is a brief course from Real Python that covers new features introduced in Python 3.8. I completed this course to become familiar with some of the newer elements introduced in recent version of Python including the assignment expression, (walrus operator,) and new techniques to print variables with f-strings.

Walrus Operator

Learning the walrus operator was particularly useful to me as it made reading other peoples’ code much easier. I have periodically seen the walrus operator used to set the conditions for a while loop within the loop itself, and it was always a mystery to me how this code worked.

while (var := expression()) == 'condition':
    # loop actions

Now I understand that this is functionally equivalent to defining the variable outside the loop and iterating/changing the variable’s contents within the loop. It’s a much more elegant way of expressing this common pattern, so I see why it’s become common in Python scripts since Python 3.8.

Debugging with F-Strings

I frequently use print() statements to check the value of certain variables when debugging my scripts. When you are working with multiple variables, it can be useful to print the variable name followed by the value: print(f'variable = {variable}'). Python 3.8 adds a shorthand for this kind of variable='value' print statement: print(f'{variable=}). If an expression has a trailing =, Python will print that expression as well as the result of that expression. This makes inserting temporary print() statements for debugging much easier.

This course covered other updates as well, including upgraded type hints, but the walrus operator and upgraded f-strings are the updates I find myself using most often. Follow my continued exercises in Python here .