what is going on everybody welcome to
part 15 the final part to the Python 3
basics series and then you guys are
getting kicked out there are just a few
more things I want to show you guys so
let's go ahead and get into it so the
first thing I want to talk about is
packages with Python so you know the
reason why Python is such a popular
language in such a powerful language yes
it's easy to write yes it's fast to
write yes it's easy to read and all
those things but there are other
languages that meet those criteria like
AR and Julia and ruby and other ones
that I can't think of right off the top
of my head but there are lots of other
high level languages like Python high
level because it's very far from the
hardware level as opposed to like C++ is
a lower level language but still
classified as kind of a high level
language anyways there are lots of
languages like Python but why is Python
the best in my opinion it has to do with
the community around Python and not just
because we're all really good people or
fun to hang out with it's that's a joke
it's because of the third-party packages
that are available with Python that's
what makes Python so powerful because
actually what truly powers Python is C
so so python is is really just kind of
like it's like this constant API slash
wrapper around another why's much faster
language so what makes Python the
greatest is the packages so the the main
thing I want to talk about here is using
third-party packages with Python so
there's a bunch of different ways you're
gonna get third-party packages the main
way is with Pip which I'm going to show
you but I also want to talk about some
of the other ways and what even is a
package so you just understand that
these things are not magical or anything
so the first thing I'm going to do is
pop into tutorials and I'm gonna copy
testing grounds we'll go into testing
grounds as well and I'm also gonna edit
testing grounds here to be example mod
so that's just example mod died
great so the first thing I want to talk
about is I guess let's talk about one of
the more basic ways you might install a
package so most of the time it'll be via
pip sometimes it'll be a package that
you can only find on github but you
still want to install it if the package
comes with a setup PI file you can just
use the following so we can go to our
command prompt and then using Python and
whatever version you want PI 3 7 in our
case if it had it you would first of all
you'd need to be in the directory that
has setup to a PI but then you would
just run PI 3 7 setup PI install that's
it so so you would do that if it had a
build you might also build but it would
be in the instructions most of the time
it's just set up to PI install very
simple so so that's one way you would do
it but that's kind of a dying method now
that's one of the older ways you used to
install packages we don't really do it
that much anymore but sometimes there
will be a thing on github that doesn't
have a Python package index thing for
whatever reason so you can use this but
let's also talk about what even is a
package like what's a library what's a
thing we can import because we have made
these imports but we don't maybe
understand how that works so the first
thing I want to do is in example mod dot
pi let's just define do a thing it's
gonna pass nothing and for now I'll just
have it print doing something really
cool and that's all our example mods
gonna do just a simple function just to
give you an idea so then now coming over
and keep in mind example mod is in the
same directory as our testing grounds is
so now coming into testing grounds we
can import example mod import example
mod and when you import something it's
going to look for it in three places
it's gonna look locally it's gonna look
in your standard library location and
then it's gonna look in your third-party
library location which is you it's in
your Python installation then Lib then
site - packages it should be in there I
might have that wrong I'm pretty sure
that's the path though anyway it's gonna
look at all those locations and this is
why
it's really important too because it
looks locally it's important to never
name your script the the name of the
package you intend to use because you'll
wind up just importing the script that
you're writing and then you're gonna get
some weird attribute error I see it all
the time this is probably the number one
error that people ask in comment
sections on tutorials of mine they're
like hey I'm getting this weird error
and I'm like you named your script the
thing you're trying to import it and
you're importing yourself stop it
so anyways watch out for that so import
example mod so this imports this script
right so it's gonna look when it goes to
import any directory or file named
example mod not well any file it would
just be a dot PI file so we've imported
example mod or a dot P Y Z anyway we've
imported example mod let's go ahead and
show how we might use it so you could
use example mod dot what does it do a
thing I think it was do a thing yeah so
we can run this and we can see cool it
just did something really cool we just
imported it the other thing you can do
is import something from a file so we
could instead say from example mod
import do a thing and then we don't need
example mod dot we just use the function
boom
same thing again pretty cool now the
other thing we can do is we can do let's
just make a new function here a second
function define do do another thing and
print doing some some other cool thing
save that import do a thing and then
what was it do another thing so then you
could do a comma do another thing we can
save that and then let's copy this come
down here paste run it again and you can
see okay we've imported these two things
from example mod now another thing that
you can do is if you were gonna import a
lot of functions you might use a wild
card like this so this is just almost
like a regular expression for everything
or a wild card that just means all the
things
so we can do that and we can run this
but this makes your code pretty hard to
read first of all because that someone's
reading your code and like you do a
thing they're curious what is do a thing
they're gonna have to do some digging to
figure out where the heck did that come
from because like say like in the case
where we have only one import the wild
card okay we know where that came from
probably first though we're gonna have
to comb through the entire script that
you know maybe it's thousands of lines
see if this function has been defined in
here and then we'll have to go to the
import be like okay it's an example mod
somewhere you know but the worst thing
would be like what if what if you just
continued using these wildcard imports
like you you know inside of example mod
you've got some wild car and import it
just makes it really annoying to read
someone's code that's used these so my
suggestion to you is don't do it but
sometimes people will do it and so
anyway just know that's a thing the
other thing you can do is you can import
a thing as a thing so not only could we
have imported example mod as a thing we
can also import do a thing as Det short
for do a thing so that we can come down
here and we can actually just run that
we've basically kind of you know renamed
the function and we run that again so so
that's pretty cool as well also let's
say you've got a lot of scripts in a
thing that we would more likely call a
package like in this case it's just a
single script that we're gonna import I
wouldn't call that a library or a
package well you would call packages
like a group a cluster of scripts so
let's make a new folder and I'm gonna
call it mod der and then I'm gonna take
example mod here and I'm just gonna put
it into mod dirt so then you might also
do something like this so from mod der
dot example mod import do a thing print
or no we don't have to print we just run
do a thing boom okay and so that's how
you could have like a group of scripts
inside of a directory and reference one
of them specifically
okay I think that pretty much covers the
I mean the extreme basics of what a
module is it's not that complex in most
cases it's just a Python file in some
cases it does get a little more complex
because it's really a Python wrapper
around C code so it does get a little
more challenging and especially
historically it was very challenging to
work with windows in this way but lately
all the C wrapped packages that I've
seen install very easily with what's
called pip now pip stands for let's see
pip installs packages so it's one of
those like recursive acronyms like PHP
or something silly like that and so the
Python package index is what pip kind of
communicates with so here you can search
for projects and stuff like that to
figure out what you want to install and
then you really just install it with pip
now the thing to keep in mind now that
you've learned what a package actually
is it's just Python code so remember
it's easy to forget what are you doing
when you install a package because it's
as easy as going to your command line
and we did it with like numpy in one of
the videos I think it was the last one
pip install numpy you know you just run
that thing and you don't really think
much about it but the thing is you're
you're plausibly bringing in a lot of
code and then when you use numpy when
you import numpy whatever is in like a
main loop or or not a main loop but a
main function to run or when you
initialize that thing that you've
installed your plausibly running a lot
of code and it's in a way that a
firewall is less likely to catch because
it doesn't look as suspect as you
running some crazy exe file so pay
attention and think about what you're
doing here because when you're
installing a package from pip you are
trusting someone else's code you're just
taking it and unless you're reading
every line which are I know you're not
like you have to understand what you're
actually doing and because of this I
would really watch out
because like for example really recently
let's say a Python package Python
package ohmygosh package index mulisch
yes just the other day here we go 6 days
ago on this website they're talking here
about six packages they found to contain
malicious code on the Python package
index now it's hard to see this table
but if i zoom in you'll you'll see that
like for example this is intending to to
to mimic the package Jango for example
but you see they called it D Ango so
it's it's for people who accidentally
typos and it's the Django package plus
some stuff okay and you don't want it
alright and at least in this case what
they were doing was anytime you entered
like a Bitcoin address it would just
change it to a different Bitcoin address
the whoever was doing this and that's
what that was doing but it can do all
kinds of malicious things and so you
just need to be careful so you know
check and recheck the package name maybe
go to the official package on the Python
package index I'm looking to see I don't
even know usually I don't even look here
it would be cool if they said how many
installs were used
you also could look at their github page
like this one doesn't appear to which is
weird because I swear text wrap isn't
that new like what what's say we went to
Django look at Django - let's see if
their stats are good so here you can see
okay on their github they've got quite a
few I don't know what it would look like
before but the other thing you could
always do is go to their github and just
you know confirm you got the package
name right okay anyway pay attention
that stuff and then you can see here all
you have to do is use pip to install so
we're not going to use Django in fact
I'm going to use colorama which was one
of the infected packages so it was type
owed to be the European spelling I guess
of color with you but the proper
is pip install oops colorama and so we
can install it that way also I'm just
trying to get myself into the habit of
always using the specific version so pi
- 3 7 - and pip install Colorama like so
and that will give us the Colorama
package now when you get a new package
let's just googled Colorama there's a
variety of ways that packages will
document themselves and everyone no
one's documentation z' as good as
everybody else's so you just kind of
like or everyone's documentation is a
little different so some packages have
unbelievably great documentation it's
really easy to use other packages don't
and then in terms of you know complexity
they all vary Colorama is pretty simple
library so their documentation is
basically right here on this page but
certain things like Django like will
have either their own website or there's
a website called read the docs and
that's a pretty popular documentation
website and so it just kind of depends
but usually you can just go through the
documentation and learn how to use the
packages so for example I scroll on down
here and I basically just scroll like
here's the main thing I just scroll
until I see some text that is code so in
this case I can see okay so to use
Colorama okay I need to initialize it
alright looks like on Windows we need to
run in it on the other platform it
doesn't matter so this is just for
cross-platform okay now to get it to
print colors all I have to do is
apparently you do this then you add to
the string so this is probably it's
building the string so using the plus
here so so this for dot red probably
corresponds to a bit of code that
probably looks really complex to you so
for example this ANSI sequence here like
this is probably more so what it
actually looks like what this will
translate to but this is you know a
little more easy for us to to type so it
looks like we could change the
foreground the background style and then
we can reset everything and
thing to note is like when you change
colors you you have to change them back
so when you say for red that means
everything that comes after that is
gonna have a red foreground subpage
engine okay so let's go ahead and let's
just first of all it's like just use
Colorama really quick in our testing
grounds in probably in sublime it's not
going to work so we'll write it in
sublime but they don't have to run in
the terminal most likely so first of all
let's do in fact let's just run the
sample actually so here is the sample
and notice the sample doesn't have in it
so we'll have to add that let's just
paste that in comma in it and then in it
not in it in it okay save let's run that
I don't think it's gonna work it doesn't
I don't think no so we definitely run it
in the terminal that's not really
surprising to do where am I
here we go so here PI - 3 7 testing
grounds PI and you can see we got some
red text some green text and then back
to normal and in fact it's kind of weird
I am on a VM so maybe that's why it
looks so funky but that looks pretty
that's like really ugly looking text I
don't know why this is really crisp and
clear but that text was not let me go
properties it's already quite large
let's go 72 I don't really want it that
big we'll go 36 let's use just try a
different fault font hopefully I didn't
screw anything up but we'll find out
okay whatever so the next thing I want
to do is let's add this now to our
tic-tac-toe game because this is all
fine and dandy but let's actually like
change something in tic-tac-toe or
actually use the package because demos
are always different than actually using
it or you just don't realize certain
things right so what I want to do is Bop
in to tutorial 14 I think a copy-paste
that call that call this tutorial 15 now
right-click it open up and Sue blank and
I'm gonna exit out of there and let's go
to the actually first we need to bring
in Colorama so let's go to testing
grounds and we just want to import well
really all of this so copy come over
here
paste then what we want to do is go down
to where we display the game board which
is here where we print the row here so
this is where we're actually displaying
the game board and what we'd like to do
now is make it pretty so okay so rather
than print count row we still want to
probably print count but we want to
modify the row little bit so what I'm
gonna do is I'm gonna make a new I'm
gonna make a new variable and I'm gonna
call it colored row because we need to
we need to build the row string so I'm
gonna say colored row and for now it's
gonna be nothing and then we want to
iterate over each item in the row so
what is each item in the row going to be
well there's only three things in this
case if it's a three by three it'll be
it'll start off as all zeros and then as
players play it's either gonna be a 0 a
1 or a 2 so what we want to say is for
item in row then what we want to say is
if if the item is equal to a 0 what do
we want to do well we're just gonna say
colored row plus equals and in this case
we don't want anything we just want an
empty space now it was gonna be a space
a comma the number and then a space
again so I think a space see between
each step I'm gonna put two spaces for
now it might be an extra space we'll
figure that out in a moment
then if item actually we should make
this Elif Elif item double equals a 1
let's say colored row plus equals and
then in this case how do we do it let's
let's make one's a green so we can make
the it would be
green plus the thing that we want it to
be so I'm going to copy this right here
I'm gonna come over here paste four dot
green and actually I think it's all caps
green and then four dot green plus the
thing that we want in here so in this
case I'm gonna make that a space the
thing and let's make player 1x while
we're here making this happen might as
well do that so X and then a space on
both sides because you got the space in
the comma so so we make that Fortner on
green now one thing to note is as we do
this this has now changed the foreground
to be green so then after this if it's a
zero notice how we didn't do anything so
we could either you know reset at this
point or we need to reset after every
you know colored letter so I think I'm
actually gonna reset it after the letter
so I'm gonna say plus style dot reset
underscore all and this will get us back
to you know whatever the default color
is and then we want to do the exact same
thing except for number two so then I'm
gonna come down here l if it's a two and
then here we'll do magenta and it's
going to be an O all right so then
rather than print row and count up here
let me just cut this and then we'll come
down here tab back to okay now print
count sure whatever and then oops
colored row so now we'll get the
beautiful beautiful colored rows
hopefully let's see what happens so
we've come up into our console now and
let's pie - two points Evan I'm so used
to type in three six a tutorial fifteen
hi let's do a 3x3 so you can see it's
currently totally empty let's play a one
and A one and
mm we need to fix our space in a little
bit there probably need a one space over
let me play a to one okay so I think our
initial spacing we just needed we need
to space it over ever so slightly so
let's add one more space to the let's
see colored row zero one so really
actually everything should be it should
every thing needs to take up three
spaces I think so actually let's make
that three spaces there because it's a
space the thing in a space because
before it was a I'll say a space than
the number and then a comma so it should
be three spaces so let's say save that
come back out here
you run this again it's a three by three
let's go one one okay now it's lined up
right and then let's run a I don't know
two one and then let's go one zero and
then to zero and then one -
congratulations player one has one but
anyways now we have beautiful beautiful
colors awesome alright and now for a few
more Easter Egg e type things if we run
Python one thing is to import
anti-gravity gravity and you get this
nice little comic about basically what
it's like to use the wonderful language
that is Python I'll let you read that in
your free time the other thing is in
Python whenever there is a future change
maybe in a different language and then
we want to back port it to another
language we can do that
so the way to do that is from future and
then you import the thing from the
future and one of them that we can do is
braces and then we get kind of this
goofy syntax error that just says not a
chance so I wouldn't expect a braces to
be in Python anytime soon okay there is
one last thing for me to show you but
I'm gonna leave that for the end
and we are done and I know you guys
still gonna feel green you're still
gonna feel new
that's normal you're gonna feel that way
for years so so the best thing that you
can do is get out there and start
actually working on things that are
exciting to you when I learned Python
the the thing I was trying to learn was
to do sentiment analysis and natural
language processing like that's why I
learned a program in the first place and
most people would have been like oh
that's a horrible beginner project but
for me it was perfect because it was
what I was interested in actually doing
and so because I was excited about it it
made learning Python easy and that's why
I think basics are you know it's
important that we just get out of the
basics as fast as possible is there a
bunch of stuff that I did not cover that
we would consider to be basic Python
definitely I mean we left a lot of stuff
like even dictionaries we just barely
graze them so I felt like it would be a
sin not to show you dictionaries but
there's a lot of stuff that we didn't
cover and then some people have been
asking about like object-oriented
programming stuff like that if you go to
Python programming at net click on the
fundamentals here and then go to the
intermediate Python series this series
has a lot more concepts to it in terms
of more advanced things and then also
object-oriented programming is done here
and we go quite a bit through that
including like doing special methods and
all that kind of stuff so if that's what
you're looking for
it'll already exist it doesn't need to
be in this series now this series was
really just to get you understanding
Python and then get out and then
actually work on projects that are fun
if you want some ideas come to the
homepage click on stuff that interests
you you can also go to the Python
subreddit so reddit.com slash are slash
Python that'll take you to you know the
recent goings ons and happenings in
Python right now so you can get ideas on
projects see what other people are
working on there's also learn Python as
another one so there's that you can also
come and join the discord discord GG
slash centex will take you to an invite
for the syntax server and you can chat
with a bunch of other people and yeah I
mean that's that's basically it so just
get out
and start working on stuff that's
exciting to you and you're gonna pick up
and learn Python on the way when you hit
concepts you don't understand Google it
when you hit an error you don't get
Google it it's really simple to learn
Python it's just you guys stick with it
and not get burnt out and I think the
best way to do that is is to work on
stuff that's exciting and interesting to
you specifically and that's not going to
be found I don't think in a basics
course so anyways I'm gonna leave you
guys with the last thing that you should
import and I'll let you read it on your
own screen but you can import this and
this will give you the Zen of Python by
Tim Peters and it's just kind of a good
little thing to kind of live your life
by
when writing Python code it's just as
something to continue continually
remember and look at your script and see
am i violating these things it's just
kind of a good way to good thing to keep
in mind as you code so anyways you can
read that on your own that's it that's
the end of this series quick shout out
to the most recent sponsors Suraj
Deshmukh and tark's gamma sorry if I
just totally butcher you guys his name's
I apologize as usual if you don't know
how to sponsor or become a member or
whatever we want to call it you can
click that beautiful blue join button
that's it guys get out of here