Conditionals and Loops Recorded Transcript This transcript was created with a speech to text software. Please excuse any typos or mismatches with the video. Hi there. Welcome back in this video. We're continuing our Python prep series of videos and we're gonna talk about conditionals and loops. So, so far, we've talked about Jupiter notebooks, uh basic Python data types, strings and more complicated Python data types. And we're now ready to start learning more computer science concepts uh that are conditionals and loops, which will use a little bit of everything we've learned so far. So I'm gonna go ahead and share the Jupiter notebook conditionals and loops. Uh So we're going to learn a couple new things in this video. For one, we'll talk about white space in Python and why that's important, which we'll see in a little bit uh conditional statements which are, which are if, if else or if else, if statements four and while loops and along the way, we'll learn about range and list comprehensions. So conditional statements or where we, where we will start uh they are chunks of code that will run depending upon the truth value of some sort of logical statement. So a silly joke that makes the round every once in a while when teaching conditional statements is uh a woman sends her partner who is a computer programmer to the grocery store. And she tells that person I need butter sugar and cooking oil. Also get a loaf of bread. If they have eggs, get six. The partner returns with the butter sugar and cooking oil as well as six loaves of bread. And the woman asks why the heck did you get six loaves of bread to which the partner replies, they had eggs. So this is a dumb example, but it really shows what the conditional statement is. So the conditional statement that the programmer checked to see was true was if they have eggs and since it, it was true, they did have eggs, they got six. And so here, it's really more of a joke about uh the phrasing of words. Uh So here, when they said get six, they meant uh they thought the uh woman meant six loaves of bread as opposed to six eggs. But regardless uh the point of this is we have a conditional statement which is phrased by an if so if they have eggs. So if this statement is true, get six of whatever. And so this is the whole idea behind a conditional statement is uh if statements as well as statements. OK? So an if statement in Python is gonna check whether or not whatever logical condition you type up is true. And then if it is true, it's going to execute whatever code that follows. So typically, these are going to use standard math operations. So it'll do something like is a equal to B uh is A, not equal to B, is a, less than B is a, less than, or equal to B and so on. And so we're gonna see an example here that really also illustrates that Python is dependent upon white space uh to denote um syntax. So we're gonna write an if statement, we'll see what the why white space is important. Uh And we'll also learn about if statements. So first we write the word if and we can see in the Jupiter notebook that it is green denoting that it's an important word in Python. Uh If two is greater than one, which we know is true. And so here's where the white space comes in. Uh notice that we have this colon. So we say if this is true, the two greater than one colon which tells Python that we want to do something. Uh Now we have to do an indent. So typically this is done in Jupiter, it's done for you. So you watch, if I hit, enter after this colon, it's already indented for me because it knows I'm typing Python. Uh but it can be any number of white spaces so long as it's the same for each indent in the chunk. So you'll have your if statements followed by an indent where in the indented code chunks, that's where you write the code that you'd like to see executed. Uh If that statement is true. So if two is greater than one, I'm going to print the string two is greater than one. So I'll run this and we can see that it's true. Uh As a demonstration, we'll see why. What happens if I don't have this indent. So we're gonna try this. So if three is greater than one colon, I want to print these three, uh these two things. And so we're gonna see that we're gonna have an error because this is not indented. So the error we get is an indentation error. Uh very aptly named. Uh And because the Python interpreter expected an indented block. So after it sees this colon, it expects that the next line is going to be indented. So we could fix this by putting an indent in ... and now it will run just fine. And so we can see here that as soon as we think we're done with whatever the if statement is, we can go back to the original indentation level, which for us is zero here. And then Python will be like, OK, there was at least one indent in line with code on it. Uh And now I can go back to not being in the if statement. OK? So what if we show, we'll show an example where uh negative one, what would happen if this was false? Well, we're still OK, we're just not going to print the little bit of code that I've now highlighted. Uh that is part of the execution block of the if statement. OK. So let me go back to what it was originally. Here we go. OK? So you can put a conditional in addition to, you know, having a single conditional, maybe you have to check two things if two things are true. Uh But you want it to be incremental. So we can first check if two is greater than one and then we can have a conditional within that conditional as long as it follows the indentation rules. So our second conditional is check to see if two is less than three. And now we'll have a second level of indentation for the code that should be executed if this statement is true, that if two is less than three, so you can put conditionals within other conditional statements as long as you follow the rules of the indentation. So for each new uh little conditional statement that you'd like to write the code that you want executed has to be at an additional level of indentation. So here we have one tab for these two. Now that we're gonna enter a possible next conditional, a second one, we're gonna have two tabs. So each of these would be like two pressing the tab key twice. OK? And so we can run this and we see that it runs. So just remem remember from before uh we can combine logical statements with, if we want to see if more than one logical statement is true. Uh We could use the word and, or the symbol and, and in this little exercise which you're going to try. And, um, well, I guess it's not really an exercise now, I'm looking at it. So just choose your favorite numerical value for me. I'll choose, let's say 71. OK. And then, uh when I run this, it doesn't have to be 71 for you. But for me, it's 71. Uh when you run this, now we'll store 71 in a variable. And we can see an example of how and works. So we're gonna check if 71 is greater than two and less than 100 ... In two different ways. So we can do it with either the Ampersand symbol or the word. And so now this is an exercise. I want you to try and code up uh how uh to check uh code up a conditional statement to check uh if the absolute value of X same X from before uh is greater than 50. And so try to use or remembering that you can use either the word or, or the vertical line. So pause the video and try this and I'll code it up right now. So we would write if, and then we need to put just like I said, when we had above with an N statement, I don't think I said it out loud but I wrote it. Uh You need to put all your conditional, unique conditional statements in parentheses. So we would type so if the absolute value is greater than 50 that means either X is greater than 50 or X is less than -50. ... And then we'll print uh the string and absolute value of X, It's greater than 50. And then just so you can see what the alternative could be. We could do if X is Greater than 50 vertical line, ... X, less than negative 50 colon prince, Absolute value of x greater than 50. OK? So both of those work. So maybe you have something that you want to run, maybe you have two things. Maybe you want to check is this true? Then run this. If it is not true, run something else. So that's what an if statement is. So an if statement will first check if your condition is true and if it is not true, it will run something else. OK? And so how do you write that? Well, first you write your normal if statement that we saw above and then when you're finally done with whatever code you want executed, if it's true, you write the keyword, which we can notice is a keyword because it's green, you can write the keyword colon and then write whatever code you want executed if uh uh if the conditional is not true. So for us, we know that X is 71. So here we'll run this and we should get X is greater than two, But we can change it to see what would happen. So what if I choose a number that I know is less than two, I'll choose X is equal to a -999. And then we can see that negative 999 is not greater than two. And so the code must execute. Uh So we just print X is less than or equal to two. OK? We can keep doing this even further with an additional chain. So we can be like if something is true, do this other check, which is, if so essentially you're saying if this is true, do this, if this is true, do that. So this allows you to chain conditionals to have more than one possibility. So you can sort of think of this as um having a nice little graph where you're say, like you're trying to check uh is X for instance, greater than two. If it's not greater than two, you can say, well, is it at least greater than negative one? If it's not greater than negative one, you can say, well, is it at least greater than negative four? And if it's not greater than negative four, you can do like some final checks. So maybe you're trying to do some sort of binning process where you need to see where X falls on some number line uh into say here in this example, four specific bins. And then uh this would be something you could do for that. So the syntax is the same, you have your, if statement with a conditional, followed by the indented, whatever code you want, run for us. It's just a simple print. And now for L if you can use the keyword, L if, which is just a combination of the words, if so you say L if you write your next conditional statement, colon indented line with the code and you can just keep doing it as much as you need to. Uh And then finally, if you want to, you don't always, you don't have to end with anything else. But if you wanted to end with an else, you can do that. OK? So we'll run this and because we now now have an X which is negative 999, we were to go through and say, all right, negative 999, not greater than two, not greater than negative one, not greater than negative four. So we end in the last, which is negative four is greater than X. We could change it again to get another option. So what if I say X is negative three? Well, where should we end up? We go through? Well, it's not greater than two, it's not greater than negative one, but it is greater than -4. So we should print two greater than or equal to X greater than like er uh Greater than here we go. We should print this uh negative one greater than, or equal to X greater than -4. OK. So now we're gonna have another exercise where you're going to code, there's some modular division and remember this turns the remainder after you do a, like a regular division, like back in, you know, maybe grade school or whenever you learned it. Uh So we'll say uh you're gonna set Y as any integer you'd like. And then I want you to try to write an if statement that shows whether or not Y is divisible by two. So feel free to pause the video, I'll do it right now. Uh But you can come back and check when you've written the code yourself. So I'm gonna say uh 17, which I know is not divisible by two. So we would just say if uh Y percent symbol two. So remember this is the remainder. So if a number is divisible by two, it should have a remainder of zero Will say that print y is divisible by two ... and then else print Y is not Divisible by two. And so here I'm using the definition of divisible where I, I want a remainder of zero. So here I should end up in the And if I chose a different one, say 16, I should end up with the 1st 1. OK? ... So we've got conditional statements now, let's learn about loops. And so the idea behind loops is maybe you have a specific chunk of code that you'd like to execute a certain number of times or until something uh is not true. So that's the idea of loops. So we're gonna learn about two types of loops. One is called a four loop and we'll get to the next one a little bit later in this notebook. So a four loop is a chunk of code that you're going to run for a certain number of times or in Python, you're going to run it through something called an iterable. And so we'll see what that means in just a second. So before we can really get through this properly, uh we're gonna learn about what a range object is. So a range object is going to give you evenly spaced integers. Uh Typically it's gonna be spaced by one, but you can specify it to be spaced by anything. So to make this, you're going to put range and then the default is that you just put in a single integer. So for me, it's 10 and when you call a range of 10, you're going to go from 0 to 9 in a uh values of one. And so it comes out as range zero comma 10. And that's because this is just the way a range object is denoted in Python. But what you're gonna see is that this will be 0123456789. So we stop at nine because remember we go to N minus one, you can specify both a starting and a stopping point. So maybe I don't wanna go all the way from zero. I wanna go from something greater than zero. So I can go from 71 say to 1 42. I could also go from a negative number. Again, we're at intervals of one. Here, you can also go at intervals, integer intervals that are bigger than one. So you can do that by saying, I want to go from the starting point to N minus one and intervals of three. So the, the third argument are the number of steps between. So here, for instance, we have 71 1 42 in intervals of three. So this would give me 71 74 77 so forth. So now that we have an idea of what a range object is, let's see what we, why we need them. So a range object is something that we're going to iterate through in a four loop. So where a four loop is gonna say for every I in some iterable object, do the following code chunk. So here we're gonna say four, I in the range from 0 to 10. So from I equals zero up to nine, um we're gonna want to do the following. So again, we have to indent and don't forget the colon when you write these, I didn't point it out, but don't forget the colon. Uh Again, I'm gonna indent and then whatever code I want executed every time through the loop I'm gonna have on this indent. So every time I do this, I'm gonna go through and say for each value of I in this range, just print I ... OK? And so here we can see we have the zero all the way up through the nine. And then this all done was just saying once we're done with our four loop, we go back to the original indentation level. So remember I said you can loop through any iterable object. So for instance, a list is an iterable object. And so you can say for whatever in this list uh and then it will just go through and say OK, well apple is in the list, bananas in the list, pears in the list and then it will be done iterating it. So any sort of like sequence like object. So you can iterate through a string, you can iterate through uh a list, you can iterate through a tuple. And so here we can see we've got apple banana pear just like we would expect. ... So let's see what happens. Uh Remember I said iterable, that's what you can loop through. So what if I tried to loop through an integer four I N five? So let's see what happens. I'm just gonna say print I ... and so here I can see I get an error, a type error. And that's because integers are not iterable objects. So they just represent a single number, whereas a range represents a range of numbers that are evenly spaced by one or some other specified value or a list, which is an iterable object which says go through all the items in this list. OK. Uh A nice use of four loops is something called the list comprehension which allows you to create a list using a four loop in a very clean manner. And so a list comprehension uh works sort of like the following. So you use your list notation with the square brackets on either side and then within those square brackets, you're gonna say I want item and you can use whatever names you want. This is just to demonstrate, you say item four item in whatever iterable you want for some logical condition. OK? And so let's see an example. So here I'm gonna create a list, notice the square brackets And I'm gonna say I for I in range. So remember this part should look familiar, this is like a four loop. So I for I in the range from 1 to 72, and then my logical condition is I'm checking to see if that I is divisible by two. And so when I run this, I should say for all the numbers from one up to 71, if it's divisible by two. Keep it. And so I should only get the evens from 1-71. OK. So 2, 4, 6, 8, 10, 12, 14 all the way up to 70. So let's go ahead and you're gonna try this now. So pause the video and try and do this exercise where you try and make a list comprehension of all the numbers from 1 to 100 and 11 that are divisible by seven. So we would do, uh, here's me doing the answer right now. So square brackets, I for I in range And I wasn't too specific in the wording. So I'm gonna say that I want to include 111. So I'm gonna go 1-1 12. OK. So this will give me as it is, this will just give me all the numbers from 1-1 12 as we can see. So now I have to add the conditional if I module seven is equal to zero. And so now I can see that I have only the ones that are divisible by 7, 7, 14, 21, et cetera. Ok. So that's a list comprehension. Uh, it may seem weird that we're doing this. It's an odd looking notation, but it actually saves you a lot of typing when it comes time to write your own code. And, uh, I just, I like them now a lot more than I did when I first started Python. They're just very handy. Uh And uh con uh succinct way to write something that we might write a full four loop for uh in an earlier, you know, without list comprehensions, uh we can combine conditionals with four loops naturally. So we can say for every I in some range, I can just check the conditional. And the only thing that you need to check for while you're writing the code is that the indentations are consistent. So the four loop, this is the code chunk for the four loop, it's at a single level of indentation. And then once you get into these conditional statements, these if statements, you now have two levels of indentation. So what this code does is for every I from one to up to 100, I check if it's divisible by seven and then I print it Or if it's divisible by 11 and then I print it. So this isn't if else or if these, these are just checking, are these numbers divisible by seven? Are they divisible by 11? OK. So now, um we're gonna code, this is probably one of the harder, not necessarily hard but time consuming exercises that we've looked at so far. So we're gonna make a nicer version of this that prints out more informative information for each individual number. So you can notice there are some Like 77 that are divisible by both seven and 11. Uh where maybe we could write a more concise sentence. So try and do a four loop where you loop through these numbers and print out whether something is divisible by just seven by just 11 or by both. So here we go, you can pause the video and try. I'm gonna start right now. So four I and range from 1-101. Right. Yep. ... Uh So we're gonna say if 5% seven equals to zero ... and I percent 11, Not equal to zero Print I is divisible by seven ... but not 11 else. If I% 7 equal uh not equal to zero And percent I% 11 equals to zero, ... we're gonna say print ... eye is divisible and this should be in quotations And then I'm gonna add but not seven. And again, if I% 7 equals to zero ... And percent I% 11 is equal to zero Will print that it is divisible by both seven and 11. ... And then finally, the only option left is the one where they're not divisible. So we can just say else And then we'll print eyes not divisible by seven or 11. Ok? And then now we can see we have options for every single integer and then you know what, I'm gonna add the actual integer itself. So that way we can see ... and then maybe I'll add a dash here to make it more readable. OK. ... Well, you know what, let's uh this is maybe overkill. Let's do it like this. We'll get rid of this and we'll just get rid of the eyes, whatever. We'll keep this and then we'll just keep it like this. This will look better. So one is not divisible. There we go see. All right. So that is one way to complete this. I'm sure there are others, but that's one way. Uh And then we'll end with this final loop type called a while loop. So the way a while loop works is you're gonna have a code chunk and a logical condition. So whenever this logical condition is true, you run the code chunk and when it's not true, you exit. So I'm gonna make a caution here. When you're testing out a wild loop, you need to be careful. Uh Because for a while loops, it's possible to make what's known as an infinite loop. So this is just a loop that keeps running forever because the statement is always true. And so let's see what we mean here. So here's gonna be I'm gonna set I equal to 10 and then I'm gonna decrease I each time through the loop and I'm gonna print I. So what I, the way the wild loop works is you say while you put your logical condition here, so as long as I is greater than zero, I'm gonna do the following, I'm gonna print I and then we're gonna decrease I by one. So then the next time through. So the first time through the loop I is 10, I'll print 10. Then I update I, I equals I minus one. So that will be nine, then I is nine, which is greater than zero. I print nine and so forth. OK. So that's how the wild loop works. And you can see if I were to say, forget that line, I'd have an infinite loop because I is always going to be 10, which means it will always be greater than zero, which can be bad because you could have code running for forever. Uh And maybe you don't notice it until after a long time has passed. And you've spent wasted a lot of compute time and compute resources. So always be careful when you're writing while loops in Python or any computing language because they can be infinite. OK? So countdown time 10 987654321. So that's really it for while loops, I don't expect that we'll use them too much. Uh My personal preference is for four loops. Um Yeah. And so there are some other flow control techniques that maybe you'd be interested in learning. I suggest that you look at the Python documentation for those sorts of things. Um We're not gonna be using them too much in the boot camp uh proper. Uh But you may need them in your own personal project. So if you find that there's some sort of flow control, which is what these things are called conditionals. And loops are known as flow control. Uh If you find that you have something maybe you want to do and you think, well, this is common enough that somebody else has surely written some code for it or it's in Python already, I encourage you to do a web search, uh and try and find it on your own. We're just not gonna cover it. OK? So that's it for this video. Uh You've learned about if statements, if else and then if else, if you learned about four loops, you learned about range, you learned about list comprehensions and you learned about wild loops. Uh It's been great spending some time with you and I'll see you in the next video. Have a great rest of your day.