Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
One other type of conditional structure in Python is the conditional expression. Let's make a working copy of conditionals.py, conditionals-working.py and we'll open up that working copy, and we'll go ahead and assign a couple of variables. Now, a lot of times, all you're doing with a conditional is something like this. if a is less than b, then V equals this is true, else, V equals this is not true and print v.
You'll see this kind of a thing a lot in a lot of different languages. We save that and run it, say this is true, and if the condition is otherwise, save and run. It says this is not true. So, in Python, there's a much, much easier way to do this. You can simply say v equals this is true if a is less than b, else, this is not true, And so that takes all of this, and it basically puts it in an expression.
So delete that, and I save and run, and we get exactly the same results. Make that a 0 and it'll say this is true, and this is true. So, all we've done here is we've taken that whole if-else structure and we put it in an expression. And Python has several of these, and we'll cover these throughout the course, but it's a convenient shorthand, and you'll notice that it even reads well. v equals this is true if a is less than b, else, this is not true And so this part here is what makes it a conditional expression.
You have if, and you have the expression that evaluates true or false, and else, and then a second value. So, without all of this, you would simply have v equals this is true, and with all of this, you have a conditional expression, this is true or this is not true, depending on the evaluation of this conditional expression. So, that's a conditional expression in Python, and it's a very convenient shorthand for a lot of cases where you would otherwise use four lines of if and else.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 96758 Viewers
56 Video lessons · 110462 Viewers
71 Video lessons · 79290 Viewers
131 Video lessons · 38036 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.