 Welcome back everyone. This is Brian in this episode. We're going to talk about iterators They make counting easy and we've actually worked with these before but we're gonna really dive under the hood and see what makes them work So for example, if you're really confused about what an iterator is, let's just say One two three four. We've got a simple tuple here and we're gonna say four x in T Go ahead print Run this Voila one two three four, but how does that actually work? How does Python know how to go through each and every single item and how to treat each item? That's what we're really gonna dive into in this video Let's pop open the hood and see what's going on here. So we're talking about Iter basics Well list tuples dictionary sets even strings and vastly huge amounts of other objects are Editable we use a function called Iter to get the iterable object. Let's take a look here So I'm going to say people equals and we're just gonna make a simple list here Brian comma Tammy and our family dog little lazy stinky dog Rango And we want to Iter through that So I'm gonna say I equals Iter And we're going to Use that list right there. So if we go ahead and print this out, let's take a quick peek at what I actually is It is a list iterator object at so this is a separate object from our list It's not the list. It's an iterator which tells Python how to maneuver through that list Okay, great. Now. Let's actually go through this I'm gonna say print. I'm gonna call the next function, which is going to tell the iterator. Hey, move to the next position Assuming that This started at the very beginning the next position is well Brian. So let's go ahead and run this And sure enough, there's Brian and now instead of knowing some sort of index we can just say next next next See Brian Tammy Rango, and it's just gonna keep moving or iterating through that list Now an interesting little bit here is if we try to go beyond the scope of that list Notice how it throws an error. It's a stop iteration This is what Python's using under the hood to tell Itself it needs to stop moving through this because it's simply hit the end So we can continue on I'm gonna comment that out But it's very important you understand what a stop iteration is and why it's there It simply tells Python. Hey, we've hit the end stop processing this Now that we understand the basics, we're going to make our own class So we're going to say 4x in our custom class iterate through it and under the hood It's going to call next and all that fun stuff And we're not going to play around the stop iteration But I wanted you to understand that exists So if you're trying to push through next and you get some sort of area, you know what it's doing Older tutorials, you would actually have to raise that yourself. So it gets a little confusing We're going to do it the easy way or I should say the Python three way We're going to say import random and we're going to make a random number generator or a lottery class A lot of people want to win the lottery. I know I do Man the things I would do if I won the lotto So we're going to say def I'm going to do init self This is just simply our constructor and we're going to say self dot underscore max Equals five if you have no idea why there's an underscore again. Watch the previous videos. All right, so Def and then we want to call iter underscore underscore so and this is where we're really deviating from other Tutorials out there because other would have the iter plus you'd have a nest next function And you have to track where you are in some internal list We're not going to do any of that because we simply don't need to So in the iter function, we're going to use what's called yield And yield is incredibly cool. I'm going to pay some notes here The yield statement suspends the function's execution and sends a value back A lot like how a return would return a value. However Instead of returning out we stay right here where we are and we retain enough state to continue on where we left off This is extremely powerful. So instead of returning Which is going to break out and then we call this function over and over again. We're going to say for underscores, I don't need a variable in range And we're simply going to say self dot underscore max We're going to go ahead and call our yield And we're going to create a random And we want a rand range Between zero and whatever number the lottery commission would want as a maximum All right, now we want to allow them to tweak this if they wanted to so I'm going to say just because I'm a nice guy def set max self value that way Whoever's using our little lottery class can set the maximum if they want to From here, it becomes particularly simple to use. You're going to see other tutorials out there There where they got all these functions and you're tracking some internal counter. Yeah, we're not going to play around with any of that But print Just want to separate this out on the screen Let's go and create an instance of our lotto class And let's go ahead and say lotto dot set max And I want maximum of 10 values Now comes the fun bit. We're going to say for x in lotto right x So let's go ahead and run this see what happens. Ta-da. We have got our random numbers And we can even if we wanted to say we want say 50 of these so it's going to kick out a lot of numbers here Let's go ahead and clear that and bang. There's our 50 random numbers Extremely cool the way this works so quick quick recap What we're doing here is we're just simply making a class Calling the constructor setting some internal values, and then we have this Iter function Other tutorials, you're going to see something similar to this, but it's also going to be followed up with Something like Next And then you would have some code. We don't need that Because we're using yield And unlike return yield will return the value, but then stay right here in this current context And when the execution pops back it will just continue on its merry way Very cool very powerful feature makes life very very simple compared to how it used to be Gotta love python 3 I hope you enjoyed this video you can find the source code out on github.com If you need additional help myself and thousands of other developers are hanging out in the void realm's facebook group This is a large group with lots of developers and we talk about everything technology related Not just the technology that you just watched and if you want official training I do develop courses out on udemy.com. This is official classroom style training If you go out there and the course you're looking for is just simply not there drop me a note I'm either working on it or I will actually develop it I will put a link down below for all three of those and as always Help me help you smash that like and subscribe button the more popular these videos become the moral create and publish out on youtube Thank you for watching