- In 1991, a Dutch programmer named Guido van Rossum
decided he wanted to create a language that would be easy
to be able to program for anyone that wanted to pick it up.
This particular language, he named Python,
and contrary to popular belief,
it wasn't because he loved giant snakes,
he was actually a huge fan
of "Monty Python's Flying Circus".
So based on the fact that he wanted a name that was short,
unique and a little mysterious,
he named it after his favorite British comedy troupe.
The thing to understand about Python
is that it's an interpreted programming language,
that means you don't compile it.
It's available for Linux, Windows, Mac,
the embedded hardware that you will find out there
in the industry today, lots of little modules for STEM kits,
you name it, it's out there.
And as we mentioned, this code doesn't need to be compiled,
and that's the thing that makes it unique
'cause it's a scripting language.
It allows you to be able to create a script
that can be followed and executed based on that,
on all the different code that you put into it.
It's either interpreted through an interactive shell
or it's gonna be translated into byte code
and executed by the Python Virtual Machine.
It's really focused on readability and coding speed,
so the idea is you should be able to look at a Python script
and be able to see exactly what's going on
based on how it's constructed.
It's highly extensible, there's a huge standard library
that's been written in Cs, so it's very fast,
of custom packages you can use to be able to extend Python.
So why should you care about Python?
Python, as we mentioned before,
is easy to understand and learn.
People can pick up Python very quickly
because it just works the way that it should.
It's also become the de facto standard when it comes
to infrastructure and API interaction control.
There's tons of examples and most infrastructure vendors
have picked up Python as one of the core languages
they support.
Python also fully supports restful APIs,
so you can literally interact with any piece of gear
that has an API that you communicate with.
It's also listed as number three
on the most popular list of programming languages
by the TIOBE Programming Language Index.
It's continually raising up and in a status
every single year as more and more people
pick up Python and learn it to be able to use
within their infrastructure automation projects.
So which version should you use? 2.x or 3.x?
Well, the biggest thing to understand is
that 2.x major release is not compatible
with a 3.X major release.
They don't have the same syntax in a lot of cases.
The biggest area that you'll see this
is in just the simple print statement.
Within 2.x, you type print and then the codes,
and that was all you needed.
Whereas within 3.x, you actually type in print
and then you call it like a function,
so you'll have a parentheses around it
and then whatever you want to print out.
Now, understand that the 2.7 version allowed you
to be able to use both ways to be able to get people
to start making that transition to 3.x,
but if it's written in straight 2.7
where it's using those types of original specifications
for syntax, it will not be compatible.
And you have to spend some time going through the script
and updating it for the 3.x train.
Now, the 2.x support ended in January of 2020,
so 2.7 is considered end of life,
that means there'll be no more updates to it.
Many of the modules have already been poured it over
to the3.x version and new development is happening there.
So if you do have an old script, make sure
that you understand that 3.x is recommended version,
so you may wanna consider updating that script.
You also want to make sure that any new projects
that you're working on will typically be using
whatever the latest version is.
So right now, that's 3.9 that you can download
from python.org.
There are a lot of examples out there of 2.x scripts
in the wild, so you'll see a lot of 2.7 code,
maybe some software developer kits from different vendors,
maybe they haven't updated them yet,
you can still run those but just understand that your time
is ticking if you will on how long
those are gonna be supported.
Now, to be able to install Python, in Windows,
you basically go to python.org and download the executable.
It's pretty straightforward.
On the Mac, it's actually natively installed,
but the thing to understand is it's the old version,
it's the 2.7 version.
So if you want to have the latest version,
go to python.org and install the latest from there directly.
So regardless of the operating system,
we recommend you do that.
Now, from a Linux perspective,
it's actually already built in
and often will be something that that's typically updated,
so you'll have a current version on Linux.
Now, from a text editors' perspective,
there are a number of choices out there.
These are just a couple of examples.
So one that I like is Atom.
Atom is one that you can download,
you can go to atom.io and it's freely available
and has an enormous amount of support
as well as extensions and modules that you can add to it.
Another very popular one today is Visual Studio Code.
Visual Studio Code is written by Microsoft
and fully supports the Git and the Git Ecosystem,
really both of these do, and it comes down to flavor.
If you like the version that Microsoft has or one like Atom,
there's also a million other examples out there,
so whatever it is that you choose to use,
make sure there's a good ecosystem for plugins,
there's the ability to be able to interact
with GitHub and Git and all the other different things
you may wanna do, and that will point you
towards which editor is best,
but they all do a great job.
Now, you also can go down the path
of an integrated development environment.
Now, what's this?
It's a software application that facilitates coding
and software development, so it's designed
to be able to make your coding easier.
So an IDE normally will consist of a source code editor,
kind of like a text editor, built-in automation tools
and a lot of the different capabilities
of being able to help with debugging
and many of the different things that will lie
to be able to do intelligent code completion.
So for example, if you start typing in a little something,
it'll actually pick up what you're the gist of what it is
that you're trying to do, and actually finish that
in a lie to be able to code even faster.
Here's an example of one PyCharm.
This is an IDE from the company JetBrains.
There's a community version of it that you can use for free
but this is a really, really good IDE for Python
that is very well-supported within the industry.
So now we've done a little bit of an overview of Python,
you've downloaded it to your machine
and you have it running locally.
Let's go ahead and take a look at how it's gonna appear
on your command line.
So here I'm on my Mac, this will be very similar
on a Linux side, little bit of a command difference
on the Windows side, but for the most part,
all of the things I'm gonna show you are gonna be identical.
Now, the first thing you wanna do is you wanna make sure
that Python's running.
So here, I'm gonna go ahead and type in python3
and be able to execute it.
And this is gonna get me to the interactive command line.
So here I can actually go ahead and, you know,
interact with Python.
I can enter some Python code.
So here I'm gonna do hello world and then run that.
And as we can see, Python is working.
To be able to exit out of interactive mode,
I just type in exit, and then that's it,
that's all I need to do.
If you want a variation that allows you
to be able to have that interactive shell with Python,
but you can do it in a little bit cleaner manner,
that's a little bit more visually appealing,
you can also use IPython.
So IPython is very similar, except it breaks up
all of your output into individual lines.
So here we can do the same thing,
so I'll type in print hi
and then execute that.
So as you see, it's broken it up into individual lines.
So the first thing that I entered was the hi statement,
it responded, now it's waiting for the second one.
So this is a great tool to use if you've got a lot of code
and you want to be able to have a very clean way
of being able to differentiate previous code
that you've executed.
And I actually like it quite a bit.
So you may see this in parts of other lessons
that we're gonna do throughout this video series.
To get out of this one, you just type quit
as opposed to the exit function.
So that gives you a quick overview
of some of the different ways you can interact
with the Python interpreter.
One of the things that you're gonna need
to also understand how to do, and this gets into getting
into virtual environments.
So as you're going through and you're working with Python,
remember, 2.x code, 3.x code, very different.
In addition, you also have scenarios
where you need certain versions of modules
and things like that.
So virtual environments were designed
to be able to make this very easy to be able to address.
So here I have a directory that I've created, Python code.
It's just basically just has one Python program in it
as well as the requirements.txt.
We'll get to that here in a second.
But the first thing I wanna do
to be able to implement a virtual environment
is I wanna type in python3, and then I'm gonna use
the m flag to be able to identify the virtual environment.
So I can go ahead and type in venv,
which is the virtual environment module,
and then I can name it something.
So here, I'm gonna name it myvenv,
It doesn't matter what this is, it's just for you.
So once I do this, what's happening in the background
is Python is setting up this virtual environment.
So base on the version that I launched this with,
which in this case is 3.9, that's the virtual environment
that it locks into place.
So once that happens, what you'll see is
there's a new directory that's been created,
and this is gonna include a lot of capabilities
to be able to store certain versions
of your modules and things like that.
So this kind of locks in your environment if you will.
Now, in order to be able to activate it,
I have to type in source myvenv/bin/activate.
So what this does is this now has launched
and activated my virtual environment.
So that myvenv was that directory that I created
within this Python code directory when I executed
and stantiated the virtual environment for Python.
And then once I did the activate,
now I have my virtual environment up and running.
So anything that I do in this one will not affect
the rest of the installation for my machine.
So that means that I can have different variations
of Python running across all these different applications
and not have to, you know, have every version the same
because of the fact that that's just what I have loaded
on that particular machine.
So it's extremely useful for creating those Python programs
that have unique or special requirements.
And as we mentioned, specific versions of modules
are very important to be able to keep consistent
when you're running on your local machine
or if someone wants to take your code and run it.
So, as we were talking about before,
there was requirements.text file is a great way
to be able to provide the opportunity
for someone to load all the different modules
that you need for your particular application.
So if I do a cat on requirements.txt,
you can actually see that I'm gonna be installing
certain versions of software.
So I'm gonna need ansible, black, flake,
all its certain versions.
So that double equal sign means that
that's a specific version I want.
That will allow me to be able to lock it in,
and all I have to do to be able to install this
is just type in python3 and then m
and then do a pip install
but do a -r and then the requirements.txt file.
And once I launch this, it'll go through
and will actually install everything
that's within that requirements file.
So all these things will get loaded,
this'll take a little bit of time,
but the beautiful aspect of this
is that keeps everything consistent,
and that's a very important thing.
So the last thing we wanna show
is how to turn off your virtual environment,
and it is very simple.
You just type in deactivate,
and then deactivate is something that you'll see
that myvenv that's over in the left-hand corner
of my Z shell here, you'll see that it's now back to normal.
So that means I've completely deactivated
my virtual environment.