Bringing things together - Iterators/Iterables - Python 3 Programming Tutorial p.13

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

Key Takeaways

Brings together iterators and iterables in a Python 3 programming tutorial

Full Transcript

what's going on everybody welcome to part 13 of our tic-tac-toe and Python 3 basics series we are coming down the homestretch but we actually have like we basically have everything that we need done done we just have a lot of polishing and putting together of everything so hmm let's go ahead and get into it so the first thing that we've done is we have done all the code for validating every winter we just don't have it all together yet so what I'm gonna do is start bringing it all together so this was some code for a vertical winner this here is code for diagonal winners this was just a quick example of a diagonal winner and then this was code for horizontal winners so in fact let me cut all that will go to the tippity-top boom and then we will tab all of this over and okay we are all set I'm just gonna leave some notes here for myself I'm gonna say this is horizontal and then die agonal and then vertical okay and then the other thing I wouldn't mind doing is like when we say we have a winner it would be actually kind of nice to say hey we have a you know such-and-such winner so like for what reason so instead what I'm gonna say is I'm gonna make this an F string so we can pass in variables and we're going to instead say player and the player is going to be whatever it was at row zero if they one learn a lot of locations but that's good enough player is the winner horizontally cool so I'm just going to take that copy come oh and you know what we don't have the checks for the die we gotta add those into can't believe we didn't do this last time paste tap that back and then paste there if row and rather than row it needs to be diag so DAGs oh my gosh this is a lot and this is a perfect example of something we absolutely must fix this is so error-prone to be doing this but first let's fix this and then we'll go ahead and make the function to fix this but we will do it I promise okay so that's done so this would be player blank is the winner diagonally diagonally and this would be let's see which one was this one so this is first we're gonna do the reversed and then the other way so reversed would be a regular upwards motion so I'm going to just put in parentheses a slash like that so it's like a little more clear okay they won you know escalating diagonally I don't know I can't think of the right word anyways copy come down here to have this over there we go is the winner diagonally and this one will be a backslash now a backslash in python is an escape character so it escapes the value of a different character so for example if you wanted to say let me just do a new thing here if you wanted to say a string was equal and you wanted to use always double quotes but then you also had like let's do single quotes cuz it'll make more sense so single quotes and then you wanted to say don't but you wanted the apostrophe T well that's going to be a violation in Python right because you close it off here and then you get this T this thing so what you can do to make it legal is backslash you escape the value of that quote and just the string element of it and this makes it valid and if we were to print string it would just be don't it wouldn't be don't backslash you know or dawn backslash quote T quote or whatever so the same thing is true with the backslash if you actually want to see a backslash in a string we're gonna do double backslash so we escape the value of a backslash so that's why we have two backslashes there okay vertical and then again I'm just going to copy this paste and it's not checks zero it's tight it's not died it's check zero close that off and this is vertically yeah boom oh right so and now I mean honestly we'd probably don't need anything for vertically but we have the bar now it makes me want to do one for the horizontal but anyway let's see so the next thing that we're gonna do is so we've got all the winds handled for now so so now we're ready to bring in the rest of the game and this is how you would like handle for if a game has been won and really quick so we should get a vertical win there got a diagonal win oh okay sorry I'm so stupid okay now we should get a vertical win okay great and then let's test a horizontal win too and then we'll put it to here player two has is the winner of horizontally okay great so okay we've got that now we need to bring in all of the gain stuff and we haven't touched game since I believe this was our part nine so we'll open up part nine and then we're just gonna copy and paste game what we'll just copy and paste all this stuff so the whole game board thing and then some examples of our game board we're just going to bring all of that in so I'm gonna come over here and we're gonna escape this I'm gonna delete win game for now I'm gonna scroll out just so we can do some modifications paste that in so now I've got code to determine a win that's great and then now we've got our actual game board in here so now what we want to do is actually kind of program in the ability for us to like kind of play a game and iterate through and stuff like that so the first thing I would say is let's just comment these I just won so I don't have to type it in a minute but basically we're gonna say play is let's say equal to true here and then we're gonna say players players is equal to you got like player one in two and then Wow play because eventually because basically we would want to let people continue playing tic-tac-toe until they don't want to play tic-tac-toe no more and so so we're gonna let them do that now I'm gonna copy this come down here while play ok so now the game actually is going to be redefined every time on start tab over and actually those should all be zeros let's see is this okay this one's on all zeros so this might be quicker just a copy and paste this one to be honest then to like fix all those pastes okay tab over great somebody was complaining about trailing commas fix it okay um and then maybe add a couple here right okay anyway so wow play okay so every time we start a new game of tic-tac-toe we are going to want to reset the game okay so now what we're gonna do is we need to well we could randomly pick a starting player if we wanted but I don't really think you want to necessarily random like it's gonna cause some trouble later on we can talk about randomly starting with a player but you guys like words playing the game could also just iterate who you know with each iterate you know work that out with each other who gets to be player number one and two I don't really think that's essential that we change player 1 and 2 there's really no reason for that so but if you want to check it out in the text-based version I kind of talked about what you could do to do that to randomly pick but for now I don't really think that we're going to do that so so the game begins and then we want to have you know basically we want to say game 1 equals false right and then for as long as game 1 is equal false we want to continue first of all switching between players but also you know displaying the game board and inputting and taking in players movements that they want to actually play so so obviously at the game because like while not while play that means we want to continue playing tic-tac-toe so this is really like game wide like are we still playing a are we still interested in playing tic-tac-toe but then we need a loop for the specific tic-tac-toe game that we're playing right now so then we would say while not game one so this will continue to run run until the game has been won and basically at this point we're going to continue taking in player movements so for example if you want to take in input from a user you are going to use basically it's you would say column choice equals and then input and then you could say what column do you want to play and then let's go ahead and and just remind them you have 0 1 & 2 and then for input always add a little space otherwise when they type something it's going to it'll appear immediately after the string so it's always nice to add a little space there so that's column choice we'll copy that paste and then we'll do a row choice row choice also we want some way to pick a current player and all that but for now I think I'm gonna let that go we'll just say current player equals 1 we're gonna come back to that I promise so current player calling choice row choice and then you would want to them actually play the game so we would do this we would say game equals game board rather than game board that was we use that back when we were trying to throw a throat purposely throw an error player equals and so again it's because we have like decently named variables we don't need to do the whole you know play you don't have to give the actual parameter name we'll just pass these variables so current player and then we had row choice and then column choice okay so we play the game but remember before we actually play the game it would be useful just to display the game board once every time this is run the game board will be displayed but for now let's go ahead and copy and then before the game actually runs we will go ahead and pass game equals game board and then it will just continue to keep running okay so so one option let's see so let's go ahead and save this actually why not so we'll always be player one and then let's try to run it now if I recall right input in sublime is kind of doesn't really work and it still doesn't really work so we can't use sublime anymore we'll have to use the good old command prompt so CMD pie - 3.7 and then tutorial third tutorial 13 pie hit enter okay what column do we want to play let's just play 0 Oh we'd set call him twice okay so okay so we made a couple of mistakes so first of all we said calm this should be row row and then here this input returns always a string because whenever you input something it doesn't know is this is going to be a float and int or whatever so it's always a string so now what we want to do is convert this to an int we can do that with a built-in function literally int in parentheses whatever you want to convert so I'll just do that int so now we've converted it to an integer I'm going to say that come back over here we're gonna break out rerun it with the up arrow enter let's try again zero zero okay great so he played at zero zero we could play one one now it's always gonna be player one cuz we're not changing too - okay great so that's working pretty well now what we want to do is change which user or which player is actually playing now there's a bunch of different ways that we could do this and I think what I'd like to do is just show some examples so what I'm gonna do is just create a new thing we're gonna call it testing grounds or something will open up in sublime and I'm just gonna clear this out and so so there's like a bunch of different examples of how to how to change players so for example trying I'm trying to find the way I initially had done it it's in the text-based version and now I can't I don't know which one it was anyway but basically what we could search like let's just search it so so we're looking to let's say rotate between players or rotate between items and a list so like rotate between items in a list Python okay Python list rotation is the first thing that comes up rotate 0 rotate 1 so this actually appears to be rotating the list oh oh this is pseudocode uh-huh yeah you wish okay I've noticed like I've never seen that okay this is some pretty curious looking code we'll continue running okay day q there is a rotate here and you can rotate it by one this seems to be like more for like longer list like what if we typed Python flip between we could do 0 and 1 we don't really want 0 1 we want 1 & 2 but we could always add plus 1 so we scroll down yeah ok you could do that one - Val would get you where you want it to be that's kind of funny and then you could use true or false yeah that's still not quite what I'm looking for so that one's kind of curious like what if we like let's go to the testing ground here let's say we have players equals list it's one actually we probably need to use your own one here and actually probably not zero on one we probably need to you yeah one zero so what this is going to do is this is going to kind of abuse the fact that index it's going to abused the fact that we want to use 0 and 1 and then we can use the fact that ones index is zero and zero index is one so what if we said for I in range 10 we'll just normally this will be like a wild true loops like this would be our tic tac toe loop but for now we'll just go through ten iterations we could say let's say current player equals one so current player equals one or let's say in fact let's say choice equals one then what we could say is current will say current player equals choice plus one because that's actually like we always have to add one in this case so we could print whoops print current player but then we can say choice equals players with the index of choice so run that and you'll see here we're going to iterate between player one and two and one and two and one and two and if we didn't add one it would be zero and one maybe that would be a little more clear to everybody so that's one way we could just iterate between players every single time so that's an option and let's come back here I'm just kind of looking through okay so then flip between zero and one Python C cycle values in list starting at a circuit certain element I'm not sure what if that would be useful but what I'm looking for right so what I was looking for was itertools has a method called cycle so we can use dot cycle and what that's gonna do and I'm trying to see so the term I had originally searched was actually Python flip between Python flip between numbers we'll see if this will come up with the same one scroll down okay yeah so this is the one I had found and I actually never heard of it or tools dot cycle because as I was writing my method of doing it the way I was doing it was so ugly that I was like shrilly there's a way and then also I was like surely there's a way to like actually like rotate so we've seen I think it was a day cue I forget which one it was that we found before that actually has a way to rotate and then you could always use like the index of zero or something like that but so coming on let's see so let's try using itertools cycles so I so looking down here you can import cycle this is just a range of two so that would be like zero one basically they're calling dot next on now with my experience I know that's not gonna work anymore but let's try let's try how to how we would actually like do that so so the other option we would have is import itertools and then we could say player choice equals itertools cycle and then we're gonna cycle between player 1 and 2 because we can and then for I in range of let's say 10 what could we do well we could just print player choice dot next now I already know this is not gonna be happy but let's say you get this error right and don't forget like this stuff is sublime really if we ran this in a console we would see this so I understand what this means but you might not so again you copy that error come over here paste it in go to this first option who cares what they're doing let's just look at this guy's what he has to say ok dot next was removed in Python 3 use next hitter instead right so then he says all you have to do is take dot next and then convert it to next as the built-in function that it is so let's do that so let's just say we get rid of dot next and then we print instead next player choice boom okay so that works and that's actually much more condensed than what we were writing anyways so that's pretty good I think that's I think that's what I would go with I think that makes the most sense the only confusing thing maybe is next but all next does is does the next thing in an iterable so for example if I said X actually let's leave that code there so we're gonna we're gonna use it pretty soon probably so I'm gonna do this if I said X equals range 5 prints next ex oh I thought that was gonna work oh we can't fine okay we should be able to convert it to a list though no you're still not gonna let me do that okay we have to iterate over the next anyway this is beside the point I'm not gonna go down that route but anytime you have an iterator err not it's not the same as an iterable that's the mistake I'm making here you can just use the next to grab the next value from it but anyways let's go back to the tutorial here and so to actually like pick our player we're just going to say let's say player choice equals itertools so basically all we're going to do is we're going to come up here and this is our iterable under the rarest of circumstances I will go back and edit a video and this is one of them so you know the question of iterator versus iterable is the type of thing that's going to show up on it on a test to do like a gotcha moment basically so I think it's pretty important that we cover and understand this so first of all an iterable this is a thing we can iterate over okay now in it or iterator this is a special object with next method okay so now iterable is iterable an object everything in pythons and objects so a list you know that's a list object it is an object but iterator the thing to keep in mind is it has some extra little methods associated with it that one you can use next but to also have a stop iteration and this is what's gonna it's just a major difference that depending on your use case you may or may not want it so for example let's just go over a couple examples real quick so let's say X is a list it's 1 2 3 & 4 this iterable is it an iterator nope not an iterator because we can't do print next X right we can't do that but we can you know for I in X print I write we can iterate over it no problem so that is an iterable now recall what we're showing here is import itertools and then down here let's say we'll call let's say N equals itertools dot cycle X mmm now this is iterator for sure right we would - we're pretty confident if we're doing something with it or tools on it it's probably an iterator so it should have a next method so let's just print next and run that okay we get a 1 now what if we print what if we do 2 next ends so as you can see 1 2 3 it's remembered where it is but what if we continued to do this copy of a spacers interestingly we get multiple cycles through here because this thing basically infinitely cycles so the next question is well is it iterable for I in n print I looks like it is just forever because it cycles on and on and on so also iterable now what if we don't really want to do that like what about X so we've already to step establish that X is not while it's iterable it is not an iterate or can we make it an iterator absolutely using a built-in function called hitter so we could say y equals it's her X and then the question is what if can we now can we iterate over Y for I in Y print I and you see okay it appears that we're still able to iterate over Y so yet again it is an iterator also iterable but then the question is what about here what up what about after like what if we try to do it twice you see we can't actually do that and also for example we could let's print and in fact let's not even next so first of all actually will do it for now we'll say print next why okay you see one and you're kind of see the same thing that we saw one too with cycle but the interesting thing too is you could say next why but then for I in why print I so what you're gonna see now is it says two three four because we actually because it's an iterator we we did the next here and now the only remaining elements are two three and four and then it's done you can no longer continue to iterate over why you've you've reached the end of you know you've reached this stop iteration so the big thing to take home here isn't it's it's like you know you can iterate over iterators with the iterable it's not necessarily an iterator but some iterators can be iterable okay and the big thing to take away iterable it's literally can we iterate over it and iterate as in like with a for loop or something like that so anything you can do it as string a list these kinds of things are it's horrible but they're not iterators and the thing with an iterator is it comes with the special next method also the stop iteration and you get this kind of behavior from it you reach an end typically because again this is an iterator also an iterable and as you saw it goes on forever cuz it's like it's just a cycle right there is no stop iteration it just goes back to the beginning all right bonus material for you guys so anyways player choice equals tools cycle so then current player well in this case we just need to always call next player choice and now we have our next player so let's go ahead and test that real quick break this run that whoops we didn't import itertools let's go do that come up to the top imp or itertools go ahead and save it we'll come back over here run it again okay well so player we're not printing out what player we are we'll do that again - let's just do one one okay you can see that it switched players but we should also probably print out which player we are so we'll come back down here current player equals next so print f what have I done Oh F there we go current player and then this will be a current player okay save that let's run it one more time current player is one zero zero okay and then one one okay so we can see current player 2 plays ok great ok so I think I'm gonna stop here this is getting pretty long already and we still have quite a few things to do for example where players can still play on top of each other so for example player number one could now play over one one and they just took that spot ok we just got a funny but obviously we don't want to let that be happening we're not actually using our win here we've got a lot of repetition also where's our low lovely print this is not scalable and probably a couple other things that I'm not thinking about so we still have a few more things that we need to clean up but I'm hoping that I can do all of it in the next video if I don't let myself get hung up on trying to show you guys an iterable okay so questions comments concerns whatever feel free to leave them below otherwise I'm gonna see you guys in the next video

Original Description

Welcome to part 13 of the Python 3 basics tutorial series. Here, we're going to be bringing our TicTacToe game together, so let's just get started. #python #programming #tutorial
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

Related AI Lessons

Up next
How to Open HPL Files (HP-GL Plotter)
File Extension Geeks
Watch →