From the course: Advanced Python
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
The assignment expression - Python Tutorial
From the course: Advanced Python
The assignment expression
- [Instructor] Back in the Python 3.8 release, a new operator became available that helps simplify variable assignments that can make your code more concise and easier to understand. So in order to understand how this new operator makes things easier, let's take a look at how assignment statements work. So I'm going to open up the assignment.py file here. Okay, so here in my code, I have a statement that assigns the value of 5 to the variable x, and just prints it out. And this is pretty straightforward and you've probably seen it many times. The new assignment operator helps address some common coding scenarios. So in other languages, like C for example, it's actually legal to do something like this. So what's happening here is you're making the assignment at the same time you're testing the value. So it basically combines the assignment along with the conditional expression. And as you might have guessed, this leads to a lot of very hard-to-find bugs because usually the developer…