Hello, everyone. Welcome back to intrude a python here on Cyber Eri on demand. If you're watching this video, you're watching lesson two
four and else in Python.
Now we're gonna have two main objectives in this. We're gonna learn how to use for looks and python. We're going to learn about the very special and very unique relationship between four and else in Python, which is something a little bit different. That's not really something that you'll have seen in a lot of other languages.
So we're gonna just demonstrate that as before with our if statements were going to open up our interpreter and we're gonna work from that at first,
I have I have it written in the script that we're gonna I'm gonna show you in a little bit
for now, we're just gonna try it out of the way.
So the way if you'll recall we constructed an if statement was it
before looking into something pretty similar is one before condition
So, for example, one of those commonly used types of python Luke
four eye in range, 10
now, what's gonna happen here is gonna increment zero through 10 but and this is important. I want to address it. Range in Python
Which is to say, it's going to be the number 012345678 and nine, but not 10. The reason for that is because we're giving it the argument of 10. The assumption is that you want 10 numbers. Not that you want to actually get to the specific number of 10.
So we're gonna print, I hear
Now you'll recall from our lesson on four loops in the abstract when we're not looking at Python, all that's happening here is that it's setting one. I equal to zero, and then it's just running through that series of numbers until it reaches the condition.
So here the condition is going to be 10. And of course it doesn't pretend because the actual four loop, if you were to write it into two code would be constructed.
That's sort of the pseudo code implementation is happening here, the general idea of it. So what we're doing is we're just going through all of those numbers and printing.
That's the first type, of course. But there is another type that we addressed in our for video in our in our four video in module one.
And we're gonna clear a screen here just a little bit and clean that up. And the best way to demonstrate that is by creating a list we're gonna say L one
and then we're going to perform a four loop against that. So four items in L one
Now I want to note that item right here is actually not a mandatory word. You can use pretty much any word right there as long as it's not.
Ah, variable. That already exists in your coat or a python keyword.
So it could be four.
Anything like that, Some ridiculous variable name. Generally speaking, you're gonna want to use a simple one just for the sake of
easy code readability and and understanding what's happening. So usually you'll see, for example, for letter in sentence is a common one
number in list. Whatever the idea here, we're just gonna do for item in one
Now, this is very similar for two are four I in range. Blink.
What's happening here is that we're just saying, OK, this list right here exists. It is some kind of irritable object of list a dictionary. What have you
and I just want you to go through all of the characters and all the values in that list and perform some logic, which in this case, is just printing that value.
So we're gonna run that list. We're gonna run that for Look, we're going to see we print A, B, C, D, E, f and G.
That's the basics of how a four loop works in Python. It's pretty straightforward. It's exactly like what we talked about when we're talking about four loops in the abstract and module one.
I mentioned, however, that our other objective is learning how to apply else tau four loops and python, and that one's a little bit trickier. For that to work, you need to understand what's called a break statement.
So again, we're gonna be using L one, which will remember is a B C D E f g.
We're gonna say four items
well say for this one d
are you single course just to keep from getting confusing? If item is deep
sure enough, you can see that it's gonna go through our lists. Going to say is a d know is be No, it's see No is D It sure is. It's worth noting that this key word in Python is works very similarly to the equal sign. But it's not identical so that Luke could also have been constructed as l one
If item equals equals D
exactly the same way. We're not really gonna talk about the main difference between is and a double equal sign in this video. It's a slightly more advanced concept in Python, so we're not gonna spend any time on it. Anyway, The point is that it will ran through this list
until it found the character was looking for.
However, there might be a condition in which all you want to do is find the character you're looking for
and stop execution. A soon as you found it.
Four items in l one.
And what break does is kind of what it sounds like. It breaks out of the loop. If you reach a break statement in the loop, it's going to say OK, we're done executing. We have reached our objective. You don't need to examine the rest of this list. You don't need to examine the rest of the possible options.
So here you can see it returns seat. Now,
this list isn't large enough for me to demonstrate to you exactly how useful that can be. But understand, For example, if you had a list of items that was, ah, 1000 items long or 10,000 items long, which is actually possible,
using this break statement would make your code to substantially faster and more efficient.
Because in fact, you stop execution once you reach what you're looking,
what you're looking for.
It's another. We understand what a break Sam it is and what it's used for. We can start to apply, and else
that's interesting for item
else, you'll note here, that else is on the same level of indentation is four in the same way that when you use elf what else with an if statement. It goes on that level of indentation.
So if we were to tab this in,
it would now be in else that is, evaluating whatever happened in this if statement
since it, since it is where it is, it's going to evaluate against the four loop.
Remember that pipe Python is white space delineated
now what's happening here? What else actually does when you use it with a four loop instead of with an If where it would evaluate, Did that condition evaluate? Too true.
It's that instead, it's going to say was a break statement reached. Did At some point in the four loop did the code reach a break statement? So note that this code code will only reach a break if it finds the letter Z in the list, which, of course, it isn't there.
So it's going to activate our else,
and sure enough knows that was found. So for item in a one, it goes a B C D E f g. None of those recede. Therefore, we never break. Therefore, we run our l statement.
That's all there is to it. That's four loops in python. You can see the relationship between four and else You can see how for looks were used. I also just for real quick demonstration. Do have that written out in this? Let me check the yes
And you can see here exactly what I just demonstrated that this is actually the letters A through H.
we do have some exercises for this. If you look over the side, you'll see a list of four loops that you could try and implement yourself. Additionally, if you have access to our insider pro program, you will be able to perform some of these four loops in our next tech lab. If you don't have that, we'll still have these. Simon's there for you to do on your own,
But I do highly, highly recommend you make use of that lab because it's just
absolutely spectacular and very, very easy to use. That's gonna be all there is for this video. As always. I'm your instructor, Joe Perry. You've been watching intrude a python here on Cyber Eri on demand