From the course: Python Essential Training

If and else

- If you want to learn about conditional statements in Python, you're in the right place. Else? Well, you should learn about conditional statements in Python. They're critical to writing most programs. And although we covered if and else previously, now we're going to show you a new statement you can add on to these if and else blocks, as well as a new syntax for using the if statement. If you've done any other programming languages, you've probably encountered the switch statement. You evaluate a series of values and run the code instructions that correspond to the first true value you find. And if you've never heard of a switch statement and don't know what I'm talking about, that's fine. Obviously, other programming languages are inferior to Python anyway. Let's go to the code. Here's a classic problem in programming. Iterate through the numbers one through 100. If the number is divisible by three, print Fizz. If the number is divisible by five, print Buzz. If the number is divisible by 15, print FizzBuzz. Otherwise, just print the number. So the sequence looks like this. 1, 2, Fizz, 4, Buzz, Fizz, et cetera. So let's write this in Python. For n in range, 1, 101, if n % 15 == 0, print 'FizzBuzz', else if n % 3 == 0, print 'Fizz', else if n % 5 == 0, print 'Buzz', else just print n. Well, this works, but it's a little difficult to read. There's a lot of indenting going on. So let's rewrite this with an elif statement, which stands for else if, and I'm actually going to copy this code and move it down here and let's just rewrite this, elif, elif and we can keep that else. And out-dent a little bit. Great. So this is the same output, but it's so much cleaner. The else if statement must always be proceeded by an if statement. So if I remove this if statement here, we get a syntax error. The else statement at the end is optional. So we can actually run without this and we just get all the Fizzes and Buzzes printed out. So the rule is you write a single if statement, any number of elif statements and then, optionally, a single else statement at the very end, to provide some sort of a default value if nothing above it matches. Of course, if you want another if statement after that, you can also do that. So if n % 2 == 0, print 'It is even!'. Get rid of that extra quote, there we go. And so we get those all printing out. And that will actually start a completely new block of if and elif and else statements. So one issue with if else statements is they can often drag on for too many lines. Sometimes, you want to evaluate something in a one-liner. So let's set up an example here, n == 3, print 'Fizz' if n % 3 == 0 else n and we get Fizz printed out. Of course, if we set n to 5, we get 5. You can also store this in a variable. So we could take this and set variable fizzBuzz equal to this output. In programming, this is what's known as a ternary operator. So a ternary operator takes in some Boolean condition, in this case, N % 3 == 0, evaluates it and returns one value if the condition is true and then another value if the condition is false. As Python programmers, our goal is always to write clean, readable code. Although ternary operators easily accomplish this when used correctly, you have to be careful with them, so they can actually be strung together. Let's take this and add, else 'Buzz' if n % 5 == 0 else n, and we get 'Buzz'. We can even do more stringing, so we could add onto the beginning of this, 'FizzBuzz' if n % 15 == 0 else, okay, and we get 'Buzz' again. We can maybe even put these in a list. So let's take a list, for n in range 1 through 101. So while I'm not advocating that you definitely do this for your next job interview, if you're asked to solve FizzBuzz, I am saying that you could.

Contents