Up until now we've been using Repl.it as our code editor and Repl.it has a
number of great features like how easy it is to share code and to fork copies of
the same code so that you can always get the starting code and the final code.
But Repl.it does have its limitations.
And as you're becoming more and more advanced and you start building more
complex projects, you'll start to feel the constraints of a simple text editor
like Repl.it. Now PyCharm is a tool that's used by professionals.
And I want to show you a couple of features so that you can see firsthand why
PyCharm is so popular with Python developers.
And the first thing I really like about it is also a very, very basic thing,
it's spellcheck for the English words that you use in your code.
This just means that while you're writing code,
you are creating names for your variables, creating your keys, your values,
or your print statements, that it makes sure that the English that you write,
the parts that's not actually code, is actually spell-checked
and you won't believe how many times this will save your skin.
Here's an example, something that's really,
really sensitive to spelling is a dictionary.
We all know that when we're using a dictionary and let's say we wanted to print
one of the values from our dictionary. We're taking our contacts and getting hold
of James's details, and then we're trying to get hold of his phone number.
Now, if you type this wrong, for example,
if I wrote phone number without an 'e', and I try to run this,
then you'll see, I get an error.
But the spell check should already alert you to the issue before you even hit
run. See how this is underlined with a squiggle
and it tells you that there's a typo in the word phon,
and you can click change to phone maybe.
And it's just a subtle hint to say, 'Hey,
I think maybe you got that wrong. Just DoubleCheck.' And indeed,
once we fix that,
then we get rid of all our issues and everything works perfectly.
The next feature that I really like is having more space to develop.
What do I mean by this? Well,
very often we actually tend to have more than one code file, right?
For example,
let's say that we moved our contacts into this data file and we imported it
instead.
So it will say from data import contacts.
And now when I want to code things up and I want to get hold of certain things,
for example I want to get Jenny's email,
then ideally I would want to see this side by side,
right? In PyCharm
all you have to do to split the screen is just right
click on the file and then say split and move right.
And now all of a sudden you can see both sides at once.
This means it so much easier to refer to some other piece of code when you're
developing.
So here I can now easily see that the key for Jenny has a capital J,
so I have to tap into my contacts,
tap into the key Jenny and then get hold of her email.
This split-screen comes in really handy as your code gets more complex and
there's more and more files.
And now another advanced feature of an IDE compared to a simple text editor like
Repl.it is a built-in linter.
So what is a linter? Well, a linter in real life, is something that picks off a
little bits of dust,
bits of lint from your clothing, and in Programming it's something that picks out
bits of code that you've written that might not be in accordance to the style
guide. When we're writing code and we're trying to decide, well,
how many spaces do I leave between things, do I use tabs or spaces,
what is the maximum line length,
all of these sort of things that maybe won't break your program per se,
but it will mean that your program might look different from somebody else's
and you just want to know what is the convention so that you can keep your code
consistent with other Python developers. Well,
this is what a style guide is for.
And the style guide that most Python developers will abide by is something
called PEP 8.
And we've already seen this when we were talking about tabs versus spaces.
For example,
the guidance is that an indentation should have four spaces, and indeed it should
be spaces over tabs.
And then there are other things like what's the maximum line length
so that your lines of code don't become really,
really long and difficult to read, or things like how many blank lines should
there between functions and variables in your code and all sorts of things.
This is a very long document and it's actually very difficult to read all of it
and remember all of it while you're in the middle of coding.
But luckily for us, if we're using PyCharm,
it automatically applies those rules and guidance to our code
and lints our code. For example,
if I was to create a function called my_function,
and inside here
I've just got two inputs, n1 and n2,
and then it calculates the total by adding n1 to n2,
and it returns the total as an output. Now,
at a later point, I decide to call my function, passing in some numbers,
maybe say 4 and 5. Now firstly,
notice how I've got some light yellow squiggly lines under both of these lines.
And when I hover over it, so I don't have to click on it.
I just keep my cursor on top of the line which has the squiggles,
you can see
it tells me that PEP 8 guidance specifies that there should be two blank lines
after a class or function definition,
but instead it only found one.
So basically what it's trying to say is that the style guide says that there
should be two lines after and before each of your functions so that everything
is more spaced out and easier to read. And here, when I hover over it,
you can see it says there's missing white space after the comma.
So the style guide says that every time you use a comma in your code,
you should always have a space. This way again
it's easier to read and it keeps your code consistent with other Python
developers so that when people look at your code,
they can see that you're following the standard conventions.
But remember that this does not affect how your code runs.
Even with all of these suggestions and these style guide rule breaks,
it doesn't actually mean my code won't work. If I run it,
you can see that it works perfectly without any errors.
And it's only a matter of keeping your code tidy and keeping it in line with the
conventions that Python developers have set out. Now,
another advanced feature of an IDE compared to a text editor is the ability to
view your local history.
What that means is I can go to show history
and just as if you were in a browser, you can see your browsing history,
well here you can see all of your coding history.
So you can scroll back all the way in the last 12 hours and see the edits that
you've made. For example, at thirteen past four today,
I created this brand new function. So this is the current file
and this is what it looked like at that moment in time.
Now scrolling forward into the future,
the next thing I did was I added a new function call here and I added some space
here.
So if you've had some sort of catastrophic events and you realize that you've
really messed up, you've deleted everything,
you can always scroll back to previous time points and simply just copy and
paste the code.
Or you can revert back to that particular time point.
Can you imagine if you are writing your essay and the number of times I have
lost my essay because my computer is crashed.
Imagine if you had this ability to just scroll back in time and find one
snapshot that you liked and then revert everything back to that moment in time,
how powerful could that be? Well,
you now have that in your hands with local history in PyCharm.
Now another really handy feature is the ability to view the structure of your
code. Instead of going to the project navigation,
if I click on this structure pane here
you can see that it breaks down my code into all of the variables and all of the
functions. That means that my function could be declared, you know,
many hundreds of lines somewhere else,
and I'm scrolling around and I'm trying to find it.
All I have to do is look at well, here's my function,
and if I click on it
it takes me straight there and I can now edit it if I wish.
And if I needed the variable jenny_email, well
it takes me straight there as well.
Once you start having lots and lots of variables and lots of lots of functions,
this is a lifesaver.
Now there's a lot of other features that I'm going to show you that PyCharm
can do, but I want to do it gradually. For now
here's the last tip on PyCharm.
Whenever you create a variable or a function name and you end up using it in
lots of places, so for example
you might call my_function here and then you might call my_function again
passing in some different parameters at some later point in time.
And then you decide that actually,
I really don't like the way that I've named that function.
It would make so much more sense if it was called add instead because it returns
the total right? If I was to do this manually in a code editor,
I would have to go add,
and then all of these lines will break and I have to find all of them and then
change them manually. And that's very painful.
So instead, what you can do in PyCharm is you can
right-click on the name of your function or your variable, go to refactor -> rename.
And now it will find all of the places where this function is created,
where it's called, and you can now change it everywhere.
So click refactor,
and it's now found the function that needs to be renamed and also all the places
where it's used. So it's used in two places, here
on line 11 and here on line 14.
So now I click do refactor and what'll happen is it'll change all the places where
it occurs. And it's much,
much safer than say just using find and replace.
Let's say that I had a print statement here that said, um,
my_function is a function.
And if I used find and replace where I just say, well,
my_function now equals add and I click on replace all,
it's going to now change it in all the places. But on the other hand,
if I use refactor -> rename,
then it's going to be intelligent enough to know that this print statement is
just
text whereas the places
where my function is used and the places where my function is declared,
that is what I want to refactor and rename.
And it leaves all of the innocent bystanders alone.
There's a lot of really exciting things yet to come as we start getting used to
using PyCharm. But as with any new tool,
you'll spend a little bit of time getting up and running with it and getting
used to using it. Now, when I was in primary school,
I still remember the moment when I got to graduate from writing with pencils
to writing with a Berol handwriting pen.
And it was a really significant moment in my life when I was allowed to write
with the adult tools. So this is kind of what's happening right here.
We're graduating to PyCharm.
It's going to take a little bit of getting used to and learning our ropes,
but it's going to take us closer to our goals.
Hopefully by now, PyCharm should have now downloaded and you're ready to head
over to the next lesson where we're going to install it.