- [Instructor] One of the fairly common scenarios you'll run into in programming is making decisions. Your program needs to compare values and execute code based upon some logical outcome. And that's where conditional logic comes into play. In Python, conditionals are handled by the "if" statement and that's what we're going to look at now. So in your editor, let's open up the conditionals_start.py. And you can see here in the main function of this example, I've got two variables, x and y, and they are ten and 100 to start with.
Let's add our first conditional; alright, if x is less than y st equals x is less than y. And then down here, I'll write print st. Alright, this code is comparing x to y, and if x is less than y, then the value of the st variable will be the strain, x is less than y, then we print the result. So let's go ahead and run this.
And you can see, sure enough, the program prints x is less than y. So let's see what happens if I change the value of x to be 1000. I'll save it, and I'll run it again. And you can see I'm getting an error because the st variable was referenced before it got assigned, which means that this line of code right here never got executed. Let's go ahead and fix this. The assignment didn't happen because x is not less than y anymore.
Now it's greater than y. Let's go ahead and make that change. So I'll add else st is equal to x is greater than y. So now we have the if condition and we have the else condition. When the if condition is true, then this block executes, and the other block executes in all other cases. So let's run this again, and sure enough, now we can see that x is greater than y. So now let's try making these two variables exactly equal to each other.
So we'll save and I'll run, and sure enough, it says, well, x is greater than y. That's not really true though, right? They're the same. The problem is that the condition here is evaluating to false, therefore this condition is evaluating to true, even though it doesn't check to make sure that x is actually greater than y. So let's make one more change to fix that problem. What I'll do is, in here I'll write elif, and the elif will be x is exactly equal to y, then st will be x is the same as y.
Now you might be wondering if there is the equivalent of a switch case in Python. Some other languages have this notion of a compound conditional known as switch and case. And the answer is no. Python, in it's efforts to be simple, sticks to the if else construct, and uses elif as a substitute for writing a switch case block. So if you're familiar with other programming languages and wondering if there's the equivalent of that, then in Python, as of right now, there's not.
So there's one more thing to look at before we move on, and that's an additional way of writing conditional statements. Sometimes you'll run into situations where you have just an if and an else, so you've got four lines or whatever intended to do a simple comparison. Now Python has a construct called a conditional statement. The conditional statement lets you write a common else if construct all in one line, and it's a more concise way of writing the comparison logic. Let's add the statement and you'll see what I mean.
Let's go ahead and put the print statement, we'll copy that and put that down here as well. So I'll run this, and you can see that the result is x is greater than or the same as y, and if I change it to ten and run it again, there we're getting the x is less than y function. So it's a nice concise way of writing a conditional statement instead of having to write out a more verbose if else block.
- Installing Python
- Choosing an editor or IDE
- Working with variables and expressions
- Writing loops
- Using the date, time, and datetime classes
- Reading and writing files
- Fetching internet data
- Parsing and processing HTML