Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
So, continuing with our Python Quick Start, let's take a look at how conditionals work in Python. I am going ahead and make a copy of this conditionals.py, and it's a good idea to work with copies. I am just going to name this conditionals -working. We'll go ahead and open that. There we have an example of a conditional, a very simple conditional. It's if and else.
And you'll notice that the assignment - it's really cool. In Python, you can assign multiple values at a time, so this assigns a 0 to a and assigns 1 to b. So that makes a nice little test for the conditional. If a is less than b, which it is, then it will print this. You'll also notice, in Python, you do the replacement, and this is Python 3; Python 2 works differently. In Python 3, these curly braces will be replaced by the values in the format, and we'll get more into how this works exactly, and some of the details of it, later on when we talk about strings.
But in fact this format is a method of the string object. Any place where you have a string, it allows you to do these kinds of replacements; it's very useful. So, when we run this - and we'll get this one because a is less than b, and unless we were to change these values and then maybe we could get this one. So let's go ahead and run it, and I'll run it here in Eclipse, and I'll select Python Run, and there we go. a is less than b, and see there is the value 0 and 1.
If I change this, if I make this say a 5, and I save that and run it, a is not less than b anymore. So the syntax of the conditional is, if, and then you have the condition, and you have a colon. This is how blocks work in Python. You'll see this same type of syntax and loops and functions and classes, where you have the colon and then you have code that's indented. There aren't any braces signifying a block of code.
Blocks are actually called suites in the Python documentation, and they simply consist of code that is indented under the level of whatever it is that's controlling it. So here we are controlling it with a conditional, and we have the colon, and then we have this code indented. Now four spaces of indenting is considered standard in Python. It's actually not required. I can make this one space and do the same down here and it would still work, save and run, but four is traditional.
So we are just going to go ahead and go with the tradition here and save that and run it, and there we have it. Python also has conditional expressions. If you are familiar with any C-based languages, you'll be familiar with something that might look like this, where you would say print, and you would have a condition a < b and a question mark, and then one value, and a colon, and then another value. If the condition were true, you would get the first one; if the condition were false, you would get the second one.
Python has something like this, but it works very differently. Here, you would say "foo" if a < b else "bar". If I save that and run it, then we get bar because a is not less than b. If I put this back to a 0, like we had it originally, and save and run, now we get foo because a is less than b. So this is how conditional expressions work in Python. A lot of the syntax that you will see here is a little bit different than in other C-based languages.
And that's because the designers of Python, they didn't consider themselves be beholden to tradition, and instead they wanted to do things that seem to flow better or make sense in some way. And actually, this does have its own set of reason in that you have print "foo" just like you would have if you didn't have a condition. You only really care about having the condition if you have another value. So you have this whole if-else all in one place, and it's spelled out; it's not punctuation marks that might just be hard to remember or hard to read.
It's very clear: "foo" if a < b else "bar"; it's kind of readable. So that is how conditions work in Python 3. You have the traditional if-else, and you have the conditional expressions.
Get unlimited access to all courses for just $25/month.Become a member