Conditional Logic
- 04:48
How conditional logic is used in Python, specifically focusing on how to use if statements, else statements, and elif statements.
Downloads
No associated resources to download.
Transcript
Conditional logic allows you to write more complex and useful applications that evaluate and respond to conditions that you define. You can create conditional logic using if statements, if statements, begin with the if keyword and have two parts. First, the condition to evaluate, which must be either true or false. And second, a code block to execute if the condition is true, which must be indented without conditional logic. You could use a for loop like we have right here to print all of the objects in a list by adding an if statement. You could incorporate conditional logic here. I have another for loop that iterates through all of the objects in list one, but inside of that for loop, I also have an if statement. Now you have this percentage sign, which we haven't talked about before. That's the modulo operator, and it checks to see that when this number is divided by 2, what the remainder is. So my if statement evaluates if the number when divided by 2 has a remainder of 0 and then a colon tells it in this indented code block, what to do if that, if that condition is true? So if the number divided by 2 has a remainder of 0, then print the number, which is really the same as saying print all of the even numbers only in list one. When I execute that code cell, it's gonna apply our conditional logic and it's gonna print only the even numbers 2 and 4. You can also add code to run if the condition is false by adding an else statement. For example, if we wanted to, we could use an if statement and an else statement to split list one into a list of even numbers and a list of odd numbers. So here I've initialized two lists, even numbers and odd numbers. Now I wanna write a for loop that cycles through each object in list one evaluates whether it's even or whether it's odd, and then puts even numbers into my list, even numbers and odd numbers into my list. Odd numbers. So I'm gonna start by saying four number in list one if that number, and then I'm gonna use that modulo symbol. If when divided by 2, the remainder is equal to 0, meaning that it's an even number, then I want to add it to my list, even numbers. So I'm gonna say even numbers, append that number. Else, if that condition is not met, I want to append to my list odd numbers. Once that's done, I want to print both of my lists and now we can check to see if our numbers were split correctly. There you go. It looks like even numbers does contain the even numbers 2 and 4 and odd numbers contains 1, 3, and 5. Your conditional logic is not limited to two options. If and else, you can add intermediate steps using elif statements, which is a combination of else. And if so, you can have, if this first condition is true, then do A, elif else if the second condition is true, then do B, else finally do C If both of those are not true, for example, let's say we wanted to split list two here into positive numbers and negative numbers. Here I've created the list, I've initialized two other lists for positive numbers and negative numbers, and then I've written a quick for loop containing an if statement and an else statement. And it says, for each number in list two, if that number is greater than 0, then append it to our positive list else, append it to our negative list. And when I print those out, we're gonna see that 0 did not get put in the right list because we only have that one condition. We're saying if the number is greater than 0, which is one and two, then append it to positive list list, otherwise append it to negative list. Well, technically it's true that 0 is not greater than 0, so it goes into our negative list, even though it's not a negative number. If we want to fix that, we'll need to add another condition with an elif statement. So first I'm going to initialize a list called 0, and that'll give us a place to store 0. After we add this elif statement, I'm gonna change the second statement to an elif statement, and it's gonna say else if that number is less than 0, then we want to append it to our negative list.
And finally, so we've sorted out the numbers that are greater than 0. We've sorted out the numbers that are less than 0, so else all we have left is 0. Then we will append that to our list called 0.
And now I wanna print my positive list, my negative list, and I also wanna print my list called 0. And then you can see that all of the numbers have been sorted into the correct list.