20. Iterators [Python 3 Programming Tutorials]

codebasics · Beginner ·🛠️ AI Tools & Apps ·7y ago

Key Takeaways

Explains iterators in Python 3 using the codebasics tutorial

Full Transcript

today's topic is I traitors in Python and we are going to cover basically the introduction on I traitor and then we will implement our own I Twitter using a class so first let's begin with what is an AI traitor I have Python idols shall open here and I'm going to first show you how you typically use for loop to iterate to an array let's say I have an array of string this is hey bro you are awesome okay now if you want to write it through this array you will just say this okay and it works you might have tried this before but have you ever wondered how it works internally this loop right here is going through these elements one by one and that process of going through elements one by one is called I trading through a loop internally it uses the built-in function called ITER so if I just say dir of a it will show you the list of methods now among these there is this item method which is a built-in method so if you use that let's see what happens I will say I equal to or I will say I TR equal to ITER okay now what ITER gives me here is a list I trade or object now using the hydrater object you can go to a next element in this list so if I call this next method on I TR it will print me the first element okay if I call it again by the way in idle if you want to repeat the old command you can use alt p shortcut okay so I am just using alt P or P all P and it is now at the last element so let's see what happens if I call it one more okay so now it's calling it's raising the stop iteration exception so this is how for loop works basically it has an eye traitor and each time when you either through it is internally calling next method on the idea object and you can see the next method on idea object here so you can see that matter here okay so this is a basic introduction on what I traitor is there are few other examples of I traitor that you can see in this screenshot the first one is I trading through a list the second one is I treating through a tuple the third one is I trading through the dictionary Keys the fourth one is basically a training through all the characters in the string and the fifth example is I trading through every line in a file all of these for loops internally are using iterators okay now there is also a reverse I treat are available which allows you to I to it through a list in reverse and the way you get that is by calling this reversed method this is a built-in method so now on either if I call next you notice that it is giving me the last element if you call it one more time it's gonna be last second element so this is basically iterating in a reverse order and this reversed method is a built-in method which you can see here so when you say dir a you see reverse method right always these majority of these built-in methods has underscore underscore which you can ignore basically and just type call this method directly so now let's go ahead and implement our own I traitor class let's say you want to design our remote control class when you praise a next button it will give you the next channel on a TV so in order to implement that class will first write as simple class called remote control okay now if you have already taken my tutorial on how to define classes then you already should have some idea on how we can define the classes okay I'm just assuming here that you already know how to write classes so here I created a remote control class and what I'm doing here is I'm just initializing a list of channels as a static list variable and then I will have an indexed variable so index variable is where like what channel you are on right now so minus 1 is your TV is off and you don't have any channel coming in okay now in order to implement I traitor you have to define this ITR built-in method okay and this I tell built-in method will return the self object so when you call ITR on remote control class object it will return you the same class object basically okay this might be a little confusing but you'll get better understanding when we start using this and the next thing you need to do is you need to always define your next method because once you have I traitor object you need to have next method implemented because that's how I treat object works whenever you call next on I traitor object it always gives you the next result okay so for the first thing I want to do here is I want to increase the index by 1 so let's say you have a remote control you start your TV then your TV you were at minus 1 and you want to now come to the first channel which is as Bo so that's why I'm increasing this index okay and if at any given point of time your index is equal to the length of this channel meaning if it is end of this channel list then you want to stop hydrating do you want to then raise top iteration so this is standard protocol for I traitors that whenever it reaches the end it always raises the stop iteration otherwise it returns the element in self dot channels so you are basically what you are doing here is just returning the next available channel okay now I'm going to define my remote control bus now so I will say okay so first what I did is I define my remote control class okay and my ITER is either of our so this ITER is an iterator object and then I will say print next off either okay and you can just keep on printing it as many times as you would like okay so let's run this program and see what happens so alright let me just run it through first okay so here it printed HBO first CNN ABC ESPN in the end it raised the stop iteration okay let me now debug through it so that you know how it is how this is working I'm going to set a breakpoint here in order to set a breakpoint you just click in this span here and it was at a breakpoint and then you click on this bar icon Bogg means debug okay so let's see cool so you are now now let me close this one okay so you are at the very first line of a program okay so I'm here and now in order to go to the next line or you can use this button or you can use after so when you press this it initialize this are object which is a remote control class object now let's go inside this and see what happens so to go inside this method you have to use this particular button or the f11 shortcut it's called step in do so you are going inside this function so when you go inside this function you notice you now came here because ITR is same as underscore underscore idea so it's always gonna call this method okay so let's see okay now idea here it says it's underscore underscore main remote control object at this so it is basically your I traitor object now you are going to call next method and I am going to again step into that method using f11 control or this particular button and when I do that you notice it's calling this method so when you call next it's calling underscore underscore next and when you call ITER just calling underscore underscore I turn this is how Python works basically it it knows that it has to call this method and you just keep on pressing this button at this point your if you want to know what is yourself index then you can look into into this variable pan and here the index is 0 so in the list the index starts from 0 says cannot return HBO here okay so let me press this button one more time so I am now at the next call and if you look at console you see that it printed HBO okay if you go into that one more time again it's coming to the same next method and here you can see not printer CNN you can keep on doing that so one more time if you don't want to go inside and if you want to just go to the next line directly you can use this button or f10 key word that's called step or okay so and now I am at ESPN so now I'm going to go inside this method because it's gonna raise top iteration now because we are at the end of the list okay so let's see now self dot index if you notice what is self not index with servlet index is 4 and length of self dot channels is also 4 right 1 2 3 4 so it is going to go into this if condition so now it is raising the stop iteration exception ok so if you look at your console now I'm going to just yep so now here it treats the stop attrition okay so that was all about I traitors thank you for watching

Original Description

Exercise: https://github.com/codebasics/py/blob/master/Basics/Exercise/20_Iterators/20_Iterators.md Next Video: 21. Generators [Python 3 Programming Tutorials]: https://www.youtube.com/watch?v=66HNCg7_gfE&list=PLeo1K3hjS3uv5U-Lmlnucd7gqF-3ehIh0&index=23 Code used in this tutorial: https://github.com/codebasics/py/blob/master/Basics/21_iterators.py Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. Website: http://codebasics.io/ Facebook: https://www.facebook.com/codebasicshub Twitter: https://twitter.com/codebasicshub
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 0 of 60

← Previous Next →
1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →