Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
There are two different types of conditionals in Python. There is conditional execution, and there is conditional values. Let's take a look at what these look like. We'll make a working copy of syntax.py, call it syntax-conditionals.py. Go ahead and open our working copy, and we'll start in here by defining a couple of variables. We'll just call them a and b, and give them values of 0 and 1.
And then we'll say if a < b, and we'll print a is less than b. We can get rid of this line here. So what we have here - I'm going to save that - is an assignment for assigning two values, 0 and 1, two variables, a and b. And then we're testing if a is less than b, and so this is a conditional statement.
And it has colon here, and that colon means there is going to be a suite, or a block of code, that is indented under that level. And so here we have just a print statement, so that's the only statement in that suite, and it will only print if a is actually less than b. So if I save this and run it, we get a is less than b in our result. If I change this 0 here to a 1, then we will get nothing in our result.
It won't print anything, you see. There's nothing there. Now, I can handle that exceptional case, and I can say else: print(a is not less than b), and save that and run it, and now we see a is not less than b because, in fact a is equal to b. So this is the common if and else in Python. You also have an else if, which in Python is spelled like this, elif.
And you need another condition, else if a > b, and we'll print a is greater than b, and then our else. For else, we know that if it's not less than and if it's not greater than then it is equal, a = b. So, if we save this and run it, of course we get a = b, and if we make the a a larger number than the b, and save that and run it, then we get a > b.
So, this is the conditional execution form of conditionals in Python. We have the if clause, we have the else if, and you can have a string of else ifs, if you want to. And we have the else, for if the case where none of the ifs and else ifs are evaluated to true. There is another form of conditional in Python, which is the conditional expression, or the conditional value. So we'll start with our same assignment (a, b = 0, 1). We'll say (s = "less than" if a < b else "not less than) and then we'll go ahead and print(s).
So what this does is this is a conditional expression. We have a value that's getting assigned only if this condition is true; otherwise, this other value would be assigned. You may have seen this in other languages done with question marks and colons, and the designers of Python felt that that was too obscure and too difficult to read and decided instead to use syntax that's much easier to read, and in fact, this is much easier to read.
It's very clear from just looking at this that (s = "less than" if a < b else "not less than), and otherwise, we'll say "not less than". Let's go ahead and save it and run it, and we see that we get the word less than, and if I make this a 1, now it's now less that anymore, and I'll run that, and it says "not less than". So that is the conditional value, or the conditional expression in Python. So Python has two different types of conditionals. It's got conditional Execution, which is with the traditional if, elif, and else, and it has conditional expressions, which don't use the question mark and colon syntax of some other C-based languages, which instead use the same keywords if and else. It is actually a whole lot easier to read.
Get unlimited access to all courses for just $25/month.Become a member