Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Assignment Expressions

For more information on concepts covered in this lesson, you can check out:

00:00 Here’s a feature introduced in version 3.8 of Python, which can simplify the function we’re currently working on. It’s called an assignment expression, and it allows you to save the return value of a function to a variable while at the same time doing something else with the return value. C and C++ programmers are very familiar with this concept, although we often do it by accident instead of on purpose. With Python’s notation, often referred to as the walrus operator, you have to be deliberate when using it, meaning you won’t make a lot of the mistakes I made when accidentally performing this type of operation.

00:38 In this case, the tryParse() function is still going to be evaluated and used in the condition for the if statement, but at the same time, its value will also be saved to the variable number.

00:50 This version of the program also takes advantage of the fact that tryParse() will return None for the numeric part of the return value tuple if the attempted conversion failed. You can now use this to test if the tryParse() function worked, so you don’t even need to return the Boolean value anymore.

01:09 Just have it return a proper numeric value or None and let the test be whether or not the value return was None. So here, you see the script to test this new version with the same strings you’ve seen tested before.

01:25 tryParse() now only returns the numeric value, if it worked, or None, if it didn’t. The testing script checks to see if the value returned is not None and at same time saves that return value to the variable number.

01:47 If it is indeed not None, that means it returned a value which can be used—in this case, displayed. If the return value was None, then the value should be ignored and the program should take some corrective action or at least display an error.

02:05 I can run this for you.

02:14 And you see, we get the same output here as you did in the last lesson. And if you don’t believe me, let me show you the first run again.

02:28 See? Same output. The function is behaving the way it did all the way back at the beginning of this lesson. We’ve just changed its implementation to make it more useful.

02:39 This version actually has some advantages over the original. I’ll let ptpython run the script again as I import it.

02:52 By the way, there’s a way to write a script so that it won’t run when you import it into a REPL. I’ve just chosen not to add that to my code examples. You can look up Defining Main Functions in Python here on Real Python for more information on that. Anyway, since this function only returns a numeric value and not a tuple, you can use its return value in other expressions.

03:15 So I can multiply 10 by the result of parsing the string "10", and this is even written to take advantage of the int() function’s keyword parameter base, so I could write a string in hexadecimal and it will be parsed correctly and used as well.

03:37 If you’re a bit hesitant to use it like this, in case it might fail, then use it in Python’s conditional statement.

03:53 So, we’ll use the assignment operator here and attempt to parse the string "123", saving it to n, and if it indeed is not None, we’ll display n.

04:09 We need some default value to use if the string can’t be parsed properly. In the tutorial, he uses 1, so I’ll use that as well. And I forgot the equal sign (=) in the walrus operator.

04:28 There we go. And here’s an example where it fails and it’ll use the default value of 1 from the else part of the conditional statement.

04:41 Give it a string it can’t parse.

04:46 This will be None, so the condition will be False, and this will return the default value 1 at the end of this expression. Again, the assignment expression was introduced in Python version 3.8, so if you’re using a code base requiring an earlier version of Python, you’ll have to use the techniques you saw in the last lesson.

05:09 Next, you’ll start taking a closer look at how assignment works in Python.

Become a Member to join the conversation.