what's going on everybody welcome to
part 12 of the Python 3 basics tutorial
series in this video we're going to
learn about probably one more built-in
function and talk about how we can do
the the diagonal victories now to do the
actually you know for you especially
with a you're hard coded there are
really only two ways someone could win
diagonally so hard coding wise it's
actually pretty easy to cover this one
so long as we're playing a 3x3 but I
want to maintain the ability for our
tic-tac-toe to be scalable to any size
so it's going to be a little more
complex but again there's only two
different ways and there have a pattern
to them so let's go ahead and get into
it so this one was for vertical winners
so all I'm gonna do I'm just gonna
comment it out again later will will
will add them all together but for now I
want to work on diagonal so let's go
ahead and add a diagonal win once again
player to just getting more plays it's
quite unfair so so what are the two ways
that someone could win well they could
either get this spot this spot this spot
and what would those be well what would
the indexes like what would the game
indexes be like this spot is 0 0 what's
this one this is 1 1 what's this one two
two very simple all right what about
this one this would be 2 0 1 1 again and
0 2 notice a pattern so at least this
way for all like the indexes alright
just identical you just increment up by
lan of game right really easy and then
going the other way you won you just go
up by Len of game the other one you go
down starting at you know Len of game
basically okay so if the in case that's
not making any sense to you guys
you would just say if gay
zero-zero double equals and then we're
just gonna go Boop and Boop and Boop and
we would have a final check about not
equaling zero oh I'm not worried about
that right now so anyway a winner and
this wouldn't this would be one one and
then this would be to two so again zero
zero one one two two definite pattern
there and then again this one would be
let's say if you go either way but like
two zero one one or zero to run that
winner but if we take away that win no
winner okay and then what if we give one
the victory in the other direction
winner okay so pretty cool so we could
do that but again yes I want to be
dynamic I want to keep us not hard
coding things because it just it's just
gonna bite you later so so let's go
ahead and make something a little more
dynamic so first of all how would we get
how could we hand out like this case so
these are two separate cases and they
really are one is going this way and one
is going this way so let's handle for
the first case here because this one's
the easiest so all we would really need
to do is like let's say we have a list
of diagonals because basically at the
end of the day when we were doing like
verticals we build a list and see if all
the numbers are the same we did
horizontal we didn't even have to build
a list to see if they were all the same
so when we do diagonals again we're just
gonna build a list and see if are they
all the same so so that's all we're
doing again so we're gonna say diagonals
is a list and then we're gonna say four
and we're just say I X to be index in
range of the Len of our game so that
will be 0 1 2 as we've already seen all
we need to do is diag stop end and then
we append game not that whatever that
was game and then that IX for index and
IX again really simple
then all we have to do well let's just
print diag I don't think you need to see
the this thing but basically we're just
gonna use this logic again and rather
than check it's gonna be diag so I'm not
gonna run that again we're just gonna
see is it is it the same or not no it's
not right 0 1 1 but what if we make this
one sure enough one on one and if we
pass it into this little functionality
we have here which by the way we've
already started repeating you know what
that means
so anyways we can see this and when we
run it through that sort of check logic
it's gonna rain true we're gonna know we
have a winner
so that way was actually really really
easy and so now let's do the other
direction because the other direction is
going to be a little more challenging
but not much so let's say we want to go
the other way so I'm going to let's see
what I want to do here part of me was
like we've got so many commented out
blocks you don't have to comment this
out I'm just trying to do it just so the
only thing you see on screen is the
things that we're we're talking about
and by you I mean me so so we're gonna
go the other way now so how would we get
you know like basically one needs to go
in order and we've already seen range
Len game that's how we can go and order
how can we go in reverse order well we
basically just set it so for let's just
say I don't know I in well first it was
range of Len of game we can actually
reverse that around so we can use
another built-in function called
reversed now prints I let's just run
that real quick two one zero done okay
pretty cool and again as always if
you'll want to learn more about reversed
I mean there's not much to learn about
it but if you want to do you could check
it out
also I hopefully by this point
you've realized oh my gosh there's a lot
of really helpful built-in functions and
again you know if you wanted to do
reversed can you think of a way you
would do it yourself or range I mean
that my implementation of range would
not be as efficient as their
implementation or Len or whatever but if
you wanted to do it you could and for
most of these let's see if they're doing
it here they don't mmm
usually somewhere around here I don't
know a lot of times you'll see the
actual code that they use to actually
produce the thing but I don't see it
here anyways I don't know spend too much
time hunting for it it's somewhere there
or you could just read the source anyway
moving along also some of these are like
C implementations so I'm trying to think
of some of them like like pickle pickle
in Python 2.7 is just like a regular
pythonic implementation of pickle and if
you don't know what pickle is we'll
probably talk about it later but it's
just a way for you to like save your
eyes like it allows you to serialize
your object and like basically you can
think of it as it's a way to save an
object to like a file on your computer
and load it back so it's like already a
Python object when you load it in so in
in like Python 2 that was a pure Python
implementation if I'm not mistaken and
then there's a C pickle in Python 2
whereas in Python 3 it's just the C
implementation I think they figured like
if someone is aware of the existence of
C pickle they're always going to use C
pickles so why would we do this so now
there are C implementations
I don't think reversed is one of them
but anyway maybe it is okay so let us
continue along so now we've got reversed
range Len games so now what if we did
the following like what if we said so we
just iterated over reversed what if we
what if we did something like this so
what if he said calls equals reversed
reversed why am i typing this out even
just copy-paste boom okay so let's say
that's columns and then rows equals
range land game so now we've got these
two they're not lists but two iterables
one goes zero one two the other one goes
to one zero then what we could do is
like we could for I don't know ID X in
rows we could print ID X and then calls
ID X right that makes sense I think no
oh well we probably hit another issue as
well
hold on eat okay yeah so okay so we made
our first mistake so oh well let me
bring that let's bring that back up
shall we
so let's say you hit this error and
you're like what I don't know what that
means
um I'm just gonna quit Python dang it
like how are you supposed to know what
this means right that's not fair is it
well let's go back to the professionals
professional coders tool okay I'm just
searching literally the error let's go
to the first result that says Python 3
so that's us anyways whew maybe too much
okay I can't blah blah blah who cares
what he's talking about but he gets this
error the same errors we got scrolling
on down
okay reversed function returns an
iterator not a sequence do you know what
that means probably not that's not very
helpful to you he's not wrong but he's
probably not helpful so then we come
back to here and we see the second best
answer I'm noticing a trend at like the
second answer is the most useful for a
beginner anyway we can just see very
clearly you have to change it back to a
list type and then the first answer
might start to make sense it's not a
list type so that's what we have to do
we've got to convert it to a list okay
so we come back over here
we get rid of this error and we go list
boom
kind of weird that range isn't angry I
bet we I mean we're not trying to call
an index on a range though because I
don't think that would work that
shouldn't work I actually don't know the
answer and I really kind of want to find
out let's find out the answer can we
call an index so X range 3 what if we
say X - oh that's pretty cool I guess
pry it just like next X is it to the
index or something I'm not really sure
anyways
that works you just can't do it on a
reversed range or reversed anything I
guess ok whatever moving along ok so now
that we fixed that everything works and
these are the reversed indexes that we
need so we're feeling you know pretty
good but before remember like this isn't
really the easiest thing for us to read
and we definitely we do know about like
index calls index when someone's reading
this they private no freaking clue what
you're talking about the other thing we
could say we could use enumerate for
example and do kind of this very similar
task that we're doing but in this case
we're actually we're not trying to we're
not trying to enumerate we're not just
trying because like in this case we're
just conveniently using ID X because it
happens to be actually an index right
and so conveniently we are able to use
it but that's not actually what we're
thinking we're trying to do we are
trying to combine these two things and
then iterate over them and that's
actually a really common task in
programming and as you might suspect
there's a built-in function for it
it is called
zip so what this is gonna do is it's
just gonna aggravate elements from each
notice that each of the iterables so you
could continue passing multiple
intervals so let's let me just quickly y
equals range I don't want to do that
let's just say two one zero zip X Y okay
so that just creates a zip object so how
about for I in zip X Y print I okay you
can see they've been zipped together
what if we did Z equals five six seven
and then we just do a equals zip X Y Z
for i na I think I did one too many
spaces there print a for I I'm actually
kind of surprised why did it say zip
object there I thought we would get away
with that for I in zip X Y Z print I
there we go
so for a equals zip for I a I'm kind of
curious why didn't that work
I must be missing something obvious
someone comment down below what the heck
I'm missing anyway my point of this is
to show you that you can it's it's an
infinite number it doesn't have to just
be two but anyways that's really what
we're looking to do and it's gonna make
our code a lot more readable so instead
what we we could say is for call row in
zip calls rose print call row mmm okay
same
outcome also I suspect we could get away
with not needing to convert that to a
list anymore
sure enough okay so that looks a lot
better also okay so I'm gonna remove two
lines here you don't have to do this and
but sometimes it can be fun to try to
condense code it's more of like a
challenge to the programmer
I wouldn't recommend this but you could
cut this paste and cut this paste
probably at some point you you're gonna
exceed the Pepe 78 characters per line
also what happened here why is that what
happened there I'm gonna do that again I
think we just caught the tail end of
Rose maybe or something is that what
happened cut paste okay okay and again
so we could do the same thing and then
we've eliminated two to two lines of
code so we could do that but if if
you're quickly reading through this code
it may not be the most clear thing
you've ever read in your life but what
we could do to even further condense
this down is instead so we definitely
need the reversed bit but what if we did
the following we could also do for call
row in enumerate and we just need to
enumerate over reversed range Len game
and then we could print call row so not
hugely shorter than the above but we're
getting there and a lot simpler than the
original code that we wrote and again we
still end up with the same same results
now at least for me I understand what
enumerates doing immediately when I see
it reversed that's pretty clear I know
what reversed is doing and then we're
ranging over the length of the game I
actually find this code to be pretty
simple to like quickly look at and
understand what's going on if you don't
you really could define calls and rows
first
but also just the fact that it's for
column and row in enumerate it tells me
okay the index is the column in the row
must be the reverse range Len right so
that it just I think that makes sense
but you can go overboard for sure and if
you're working in teams or something
like that your teammate may or may not
appreciate what you've done okay so then
this one would be diag sequels this and
then you would die agz dot append and
then basically game call and then row so
in in fact it would actually be real
call let's B must be correct so as we
iterate over game row row row and then
it's called so it's actually row then
call them the other code would have
worked just fine but it would have been
wrong
okay so Diana let's print die Ang's okay
two one two right and that's true now we
could go and verse now it is two two two
we run the check and it will come back
true so this was the other diag I'm just
gonna uncomment this now okay so this is
like the two two bits of code for the
diagonal aspect I have no idea how long
this tutorial has run some people have
been complaining that the tutorials were
like too short normally like my
tutorials are somewhere in the 10 to 20
minute mark but people are always
complaining they're too long and then in
second I make a nine minute video they
like god it's too short so I think like
the sweet spot must be like exactly ten
minutes or something I don't know if
people have been trained because now
creators try to make ten minute videos
because if you make ten minute or longer
video you can you can throw in a second
set of ads i I've never done that but
you can do that and that's why a lot of
creators have started making ten minute
or ten minute and one second videos
so anyways honestly I just go until the
topic is covered so if you think
something's too long or too short sorry
I really don't pay attention to the
time so okay so we have done the two
diagonals I'm pretty confident that
we've got it but in the next part part
number thirteen what I'm gonna do is
we're gonna put everything together and
then we can kind of play with the game
map make sure we'll check everything and
then we should be able to do that within
a couple of minutes and then we'll add
user input so the user can actually like
play the game now we'll check that and
the code will basically be done the only
other thing I want to make the game
dynamic itself also we need to make
dynamic the string that gets printed
right above the game board because
that's gonna bother me if that's like
the one thing that's not dynamic and I
think that's about it and to make that
dynamic we'll use a third party package
someone did mention it before but I was
like go we're not there yet
anything and then maybe we might use
another third party package just to make
things look a little prettier but other
than that we're pretty much done with
the tic-tac-toe game and the basics the
only other thing I'm planning to do is
that we need I want to have one more
third one third party package that we
get into just so I can show you guys how
you should go about using third party
packages because that's what Python is
all about so I'm I'm leaning more so
towards web development something with
flask
just because flask is pretty easy to
quickly do examples with I might or I
could do like data analysis or something
like that so if you have a vote between
data analysis or web development it's
not going to be a full series it's just
like a quick example like it would be
like one two three videos at max let me
know down below if you've got some other
thing that you'd want to request you can
request that too it's got to be basic
enough to like I said be like one to
three videos but some way to show using
like third-party packages basically and
working with modules stuff like that so
anyways that's all for now questions
comments concerns suggestions whatever
feel free to leave them below otherwise
I'm gonna see you guys in the next video