1. Getting started with Python and Spyder
Oct 8, 2023
Python is a fantastic coding language to learn if you are an engineering student! It's free, widely used, has lots of support, and offers many packages and libraries that will help you do the things you want like scientific computing, data analysis, visualization and more. A drawback to python is that it's an interpreted language as opposed to a compiled language so it will run slower than things like C++ or javascript.
We should start by downloading python via the Anaconda3 distribution. This will give us things like Spyder, Jupyter notebooks, and more. In Spyder we can see where we code, our console (the output of the code) and our variable explorer window where we can see the values of our variables in real-time.
All right class, welcome to MSE 2001 Introduction to Programming for Material Science and Engineering,
or as we're going to call it, Introduction to Python.
In this class, I am so excited to teach you the fundamentals of coding because even as
a material science engineer where you think, why do I need to learn how to code?
You'd be amazed how important it is to learn how to code.
Every one of you, when you get a job, your employer will be asking you, do you know how
to code?
Right?
It wasn't that way all that long ago, but it will be that way going forward for sure.
It's only becoming more and more important.
Now I myself, I'm mostly self-taught.
I learned how to code on this guy, this old school TI-89 way before you could plug these
things in or have color screens.
I have no idea what language this was in, but this is where I learned how to do my coding
decades ago.
And once I learned how powerful it was that I could program in little scripts to help
me solve math and chemistry and physics problems, I was totally sold.
And it's been incredibly useful ever since in the decades that have followed.
So we have lots of options.
When I was in undergrad, we took a class on C++ and MATLAB and it was sort of combined
and they tried to teach a little bit of both.
And there's advantages and disadvantages to those two languages, right?
Nowadays, what we're going to teach you in this class is Python, right?
So why is Python the right language for us to teach you?
Well, there's a lot of reasons, but the number one reason why I think that you should learn
Python, it's open source and free, right?
That's a big deal.
If you learn how to do all this programming in MATLAB like I did, and then I didn't have
access to MATLAB, that didn't do me a whole lot of good.
It was so frustrating when I wanted to do coding projects and I didn't have the resource
to do it anymore.
So we're going to prevent that by teaching you Python.
It's actually becoming incredibly popular, far more so than MATLAB, even among engineers,
right?
So that's the second point why I think you should learn Python.
It is easy to use, but it has loads of support.
There's lots and lots of people working with this, and so there's lots of examples and
tutorials, right?
If you go to Stack Overflow, that's a place where people can post coding questions, you'll
see that this trend has been remarkable in the last 10 years.
Python has just blown up when you look at the percent of overall question views each
month.
More and more people are using Python, so it's becoming the tool du jour, I think, for
engineers and scientists.
And then the last reason why I think that you ought to learn Python is that, you know,
it comes with a lot of great packages that make our lives a lot easier as scientists
and engineers.
Things like tools for data analysis, like Scikit-Learn, or tools for scientific computing,
like SciPy, NumPy, and Pandas, or tools for visualization, like Matplotlib.
We'll talk about these libraries and tools a little bit later on, but believe me that
there are some really great tools out there that you're going to be taking advantage of,
right?
Now, what are the downsides of Python?
It's not all roses.
There are some downsides.
For one thing, it's what's called an interpreted language, as opposed to a compiled language.
Matlab is also an interpreted language.
What that means is that for every line of code, you can type in a single line, and then
that line gets turned into the machine code that it needs to run it, right?
Whereas in a compiled language, it prepares the entire program, so you have to compile
your program first and then run it.
And because it's pre-compiled, it runs much faster.
So JavaScript C, some of these compiled languages, will run faster.
So if your project ultimately, at the end of the day, is going to be something where
speed, you need it to run very, very quickly, maybe it's going to make decisions in real
time or something, then you might want to consider other options.
There's things you can do to make Python run multiple things at a time, but it's really
not what it's designed for.
It's meant to be a program where you can try something out, come up with an idea, try changing
a single line of code, explore and see if it works, and then see how it turns out, right?
That is one downside, though, just be aware.
All right, so to get started, what we're going to want to do is we're going to download Python
using the Anaconda installer.
What's Anaconda?
We're going to use Anaconda 3 because what it is, it comes with lots of other stuff other
than just Python.
When you want to code with Python, there's Kython, but there's also the environment that
you're going to do the coding in called Spyder, we'll talk about that in a minute.
And then there's all these additional packages, things like I mentioned earlier, NumPy, Pandas,
Scikit-learn, we want that stuff to come with it too.
And if we download it from the Anaconda environment, then we're going to get all that stuff with
it.
So let's go ahead and click that, and let's download this, right?
So if you go to this Anaconda, it's free, so you don't need to pay for anything, hit
this download button, it's going to bring you all the way to the bottom, right?
And here's the different installers that you could download, so depending on which sort
of system you're running on, you need to pick the right one here.
So for me, I'm on a Windows machine, and it's a 64-bit Windows machine, so I know I need
to download this one.
Now, if you didn't know which one you needed, you could check it out ahead of time, right?
You could come over here to your system, information, and when I go to my system information and
pull it up, I see right here that my system type is x64-based PC.
That means that I'm a 64-bit PC, not a 32.
Great.
So now I know which one I need to download, it would be the 64-bit one.
And fortunately, I already downloaded that earlier today, so I didn't have to wait for
it, because it does take a minute to download, it's kind of a big file.
So go ahead and download that.
Okay, since I've already got mine downloaded, I can go ahead and double-click this installer,
and that's going to bring up the wizard to install this, right?
So it's going to guide you through this.
Go ahead and hit agree to the user statement.
If you've got multiple people that use your laptop or computer, you might want to consider
installing it for all of them, but if it's just you, or if you're the only user, then
just leave it as just me.
Okay?
Now, this is something you should watch for.
The destination folder is going to put it by default in your C drive, users, whatever
your username is, and then it's going to create a folder called anaconda3.
That's fine for me, but for some of you, this might be a problem, because your username
might have spaces in it, right?
For example, if I use my full name, Taylor Sparks, it would put a space there, and that
can cause problems in Python, having spaces in these directories.
So if that were the case, you'd want to pick something else.
For example, you could choose C drive, you could create a folder called apps in your
C drive, and then create a folder called anaconda3 and put it there if you wanted.
But for me, I know that this works, I've installed it before, so I'm just going to
go ahead and say, okay.
So the last thing here is that it's going to ask you, all right, do you want this to
be your default version of Python, right?
I'm installing Python version 3.8.
However, on my computer, I also have other versions of Python.
I have Python 2, because I use that for refelt refinement, because the software, GSAS 2,
runs on Python 2.
So this can cause some problems, and so be careful in realizing that when you install
different installations of Python, you might need to tell it which version it's using.
For most of you, I think if you leave it just fine as register anaconda3 as my default version,
that you're going to be fine.
So we hit okay, and now it's going to install.
And this will take a minute, so I'm going to close the video, and we'll open it back
up once it's had a chance to install.
Okay, and a few minutes later, the installation is now complete.
So we go ahead and hit next, we're going to finish it up, and we are ready to go.
First thing it's going to do is, it's going to take us to some additional information
where if you want to learn more, it's got that available for you.
We're not going to bother with this, instead, we're going to go straight to Anaconda.
So from your start menu, find the Anaconda installation that was just installed, and
let it pull up.
Okay, it takes just a minute for that to upload and pull up for the first time, but fortunately,
that doesn't really matter, because we're not going to use it all that often.
Instead we're going to use a different thing, and I'll show you what that is in a moment.
It's going to ask you, hey, by the way, is it okay if I take information from your experience
and send it back to Anaconda so that it works better for other people in the future?
I think you should say yes to this, but do what you want.
I'm going to say okay, and then don't show it again.
Okay, now that we're here, take a look at this.
In this Anaconda Navigator, it shows you all the things that came with your Python installation
by using Anaconda.
It already came pre-installed with command prompt, right?
So you can do commands straight from your terminal.
We're not going to do a lot of that in this class, but it's possible.
It comes with Jupyter Notebooks, which are really a popular way to do your programming
in Python.
And then critically for us, it comes with Spyder version 4.1.4 already installed, and
that's what we're going to want.
So go ahead and hit launch on Spyder.
All right, so it's pulling up Spyder 4.
Now that's what we're going to do most of our programming in.
Spyder is a development environment, integrated development environment, right?
So it's what I think that is useful to do your programming in.
You could also do it in Jupyter Notebooks, but I think that Spyder is what you should
want to use because it allows us to do some really great things like see your code, see
the output of your code, and also see your variables in real time, which is not something
you can do in some of these other environments.
Okay, now that Spyder's open, the first thing it says is, hey, there's a new version of
Spyder.
Spyder 4.1.5 is available.
Oh, great, that's fine.
So you should have it checked for startups if you want, or you can turn it off, doesn't
matter, right?
In Spyder, this is where we're going to be writing our code, right?
So first thing is, you're going to have over here on your left in this panel, I'm going
to make it large so you can see it.
This is where we're going to be doing our code typing.
And then down here, this is the output.
It's called the console.
So this is where the code's going to appear when we run code.
So we're going to make that a little bit larger too so you can see what's happening over there.
So for example, if I take a code, let's say I assign the variable x, I want it to equal
five, right?
So when I run this, I can come up here to run, I can have it run just that line by hitting
F9.
I can have it run everything, right, all the things here by hitting F5.
So I'm going to hit F5, and it's going to run that entire code.
And sure enough, the only thing it told it to do was to store the value of five in my
variable x.
The last thing I want to show you over here is the variable explorer.
Here we see that sure enough, our variable x now has a value of five.
This variable viewer is really awesome.
That's a big advantage that not all, like Jupyter Notebooks doesn't have this.
They can easily see the value of your variables, which makes it really great.
And then we can do the classic, the very first thing that most coders always do is, and you
should do this as well, start out by telling it to print hello world.
Just to make sure that things are working, we're going to have it print hello world.
By the way, you see that this thing pops up below it.
It's automatically telling me how to finish this.
It knows that I used the print keyword, and so it's saying what can go in there.
Now this is a little bit overwhelming at first, so don't worry about it.
You can just ignore it.
But we're going to go ahead and run this.
So just on this line, I'm going to hit F9 to run that line.
And sure enough, out here in my console pops out hello world.
So we've written our first piece of code.
Let's go ahead and save this in a place.
Right now it's in the untitled or the temporary file, so we're going to save this somewhere.
I suggest that you create a place where you're going to do your work.
So I'm going to save this as, and I'm going to put this where I do my classes for this semester.
So I'm going to go to my 2001 folder, which is my teaching folder.
And I'm just going to call this hello world.
Now the extension on this needs to be .py.
It will do that automatically.
That tells you that it's a Python file.
We call these modules.
Any file that ends in .py is a Python module.
So we've written our very first Python module using Spyder integrated development environment.
We've got Python downloaded and ready to go.
We're ready to dive in and start learning more about Python.
So we'll jump into that in our next video.

