Education

Developing the basics: Programming myself, post two

treasureMap_2

Pick a path and stick with it

<<Read the previous post in this series  Read the next post in this series >>

Everything is possible in theory. 2.5 hours a day I need to dedicate to the pursuit of learning to program if I am going to get through all the courses I have set for myself to complete in the next few months. This should be easy if I just get my head down each day and study, but the reality is you probably have other responsibilities or if this is the first time you have needed to study, there are going to be hurdles to get over (more on this in a later post). You can though limit the hurdles you encounter if you use a good tool.

Over the last few weeks, I have really discovered the benefits of using the PyCharm Edu IDE. Though it doesn’t have all the features the full version has, for a free version for me starting out, it has been incredible. It is incredibly good at telling you when something isn’t going to work and better yet how you can fix it.

I have taken on a couple of resources to learn from. But in each of them, I have found the PyCharm Edu tool really awesome, especially for practicing the things I am learning. Remember practice is the key to really establishing the skills.

So here are some ways to get the most out of PyCharm Edu, from what I have found so far.

Basically what is important is that you don’t have to just use it to go through the built-in courses. It is actually a pretty powerful entry-level IDE on its own, and perfect for when you are starting out with writing code, especially if you are learning out of a book.

To create a sandbox project which you can use to test your code go to File > New Project…

empty project console

Then you will be taken to an empty IDE like this. It doesn’t look like there is much going on, but this here is where you can put all your project together and really start to play about.

The first step then is to make a file to put your code into. Right-click the project file > New > Python file.

new file

Then name it something epic – like “testing stuff”.
Now though you probably will want to see the results of the code you are about to run? In the bottom left of the PyCharm Edu window, there is a button named “Python Console”. Go ahead and click this, and you will get the console where you will be able to see the results of your tests.

console

consolePython

Now you should have something that looks like this:

whole console

All that is left now to do is add some code and give it a run.

guess = 0
while guess != 42:
    g = input("What is the meaning of life?: ")

    guess = int(g)
    if guess == 42:
        print("Yes now go forth and prosper!")
    else:
        print("you Lose!")
        if guess < 42:
            print("no you silly sausage!")
        else:
            print ("what are you drunk")
    print("the meaning of life is 42!")

It is maybe not the best code in the world and will crash if someone tries to use a text string, but it is mine and it kind of works (in a later post I plan on taking you through debugging this).

putting it all together
I hope that this helps you set up a testing environment for yourself. This is honestly the most fun way to learn to code that I have found. The PyCharm Edu development environment itself has really enabled me to try out new things with the code. For example, when you make a program which branches with if and else options, you need to make sure that the same if/else statement is indented to the same level. With the PyCharm Edu, it will warn you and let you know where to indent each statement, which is perfect as it can be hard to keep track of.

So if you are using a book – or something where there are a lot of code examples to try out -to learn from then it is really an invaluable resource to put the exercises into practice, and so I recommend using the Edu in this way, you will get much more out of your learning.

It is really hard to describe just how much it helps, as you really need to try it for yourself, I know though that in some cases if I had tried to make some of the things I have made, in a text editor without any assistance, I would really struggle. Without it, I would probably still be trying to make some of my earlier experiments work even now. It gives you the freedom to explore higher level concepts and try things from scratch with an assistant to help you when you get stuck, this is basically the only way I can describe it.

You can actually find exactly how much it has helped you though. If you go to PyCharm Edu console – Help > Productivity guide, it will tell you all the times it has helped you out and where it has helped you. Mainly my issues are quite basic, but there are features for when I get more advanced.

productivity
Also, something else I found pretty interesting as I use the tool a lot and so it makes sense for me to try to get the most out of it. Go to – Help >> tip of the day – to get some really useful tips for using PyCharm Edu.

What specifically I’ve been learning about

Now that I have finished the PyCharm Edu introduction course, which was really great for a foundation knowledge in how Python works, what I wanted now was some context before I start on the adaptive Python course, so I took some time to take a look into the first few weeks of CS50.

This is actually a really interesting course and well worth a look. Each lecture is long though, and I found myself twitching toward the 40-minute mark of a 1.5-hour long video. I think it is important to look at the computer science behind it all, as we are not just learning a language, we are learning how it all works together.

As I mentioned in the previous post I have already been looking into some computer science points, and so it came as no surprise to learn machine code is binary, and somehow your code needs to be translated into this to be understood by computers. The CS50 course though starts by covering the C programming language; this is interesting as it explains some of the big points in the code again. Including libraries (which you go through in the PyCharm Edu as “import”) but here it is <#include stdio> and main, which is the entry point where your code starts. Getting an explanation of these again really helped to cement the concepts, as having things brought up, again and again, helps to build a familiarity with the ideas behind programming. It provides a different take on the subject. There are some great explanations of the concepts with some great demonstrations usually performed by the students in the class, it definitely helped me to understand some of the ideas better.

 

Otherwise, I have been mainly reading and learning about:

Functions and libraries

So functions themselves are an important building block to understand. The best way I have heard them described is that they are essentially a black box, what is going on inside them under the hood is not really something that we look at. It is mainly about putting in the input you have and get the output that you are expecting. So to use the technical terms the function is something that contains statements, what this means is there is a sequence of statements within a function which when you “call” the function get executed with the statements contained within the function.

Functions are smaller parts of the whole picture. So rather than write out the entire source code from start to finish, finding that something is broken, and smashing up your computer in frustration. you can instead make a lot of the little subparts and make sure they work. Making a small function that results in achieving something that you want it to exactly, means you can simplify the issue. It is also really useful if you have to keep making a specific calculation (an example would be the radius of a circle – See below) where you need to keep testing against different input values then this is an ideal place to define a function. So rather than having to write out the calculation, again and again, you can write it once and keep using it over and over. Better yet is to use someone else’s function which they have taken the time to create and use that again and again. Basically, if you have any part of your code which needs to perform some action in a lot of different circumstances, then you should aim to use a function, rather than copy and paste the code which leads to something referred to as code bloat in the industry. Programmers have a principle they generally live by – DRY; don’t repeat yourself. If you have to repeat something then use functions to keep it as dry as possible.

Functions need to be declared before the rest of the executable code is run. There will be an error message otherwise, but if you are using PyCharm Edu, then it will warn you about this beforehand in any case.
In Python, you need to use the def keyword (one of the keywords you can’t use as a variable) to define a new function.

So to define a new function in Python, it looks like this

def a_function():
print(“this is a basic function in Python”)

Important to note is that you have to define the function before you can start using it.

Once the above code is added the function is defined. You can then call this code in your program by simply using

a_function()

Then this will print out “this is a basic function in Python”.

Libraries

If you import and use libraries, you also have access to some pre-made functions. Import Math means you then have a whole host of new calculations and math options available to you to use right out of the box as a function and variables. Another benefit of using functions from libraries is you are able to use and understand high-level programs without having to know all the details of how this function was all put together.

For instance, now you can use Pi rather than knowing the exact full number – just type math.pi, and you have this number ready to use – as an example, to use this in a calculation you can simply type:

volumeSphere = (4/3) * math.pi * (r**3)
print (volumeSphere)

r is the radius you have of the circle and this will give you the volume of the sphere.

 

You can also then create your own functions
So if you want to find out the volume depending on what radius you are using you could use a variable and name it radius and give it a number:

Radius = r

And creating the following function would work –

def sphere_volume(radius):
volume = (4/3) * math.pi * radius ** 3
return volume

And you can get the result with this:

print(sphere_volume(radius))

As I have found Python’s urllib library is really pretty cool and I am pretty excited to get better so I can start using this to do really interesting things (though I’m pretty sure there are a lot of really great libraries I have still not come across).

Here is some code for you to tryLet me know in the comments what you get returned!

import urllib.request

page = urllib.request.urlopen("http://www.jetbrains.com")
text = page.read().decode("utf8")
someText = text[45:99]
print(someText)

 

On a final note from the evaluation and revision of how my plan is going, I am finding that the mix of media is currently hindering the progress somewhat. I am starting to think that it might be better to focus on one course, complete it, then move onto the next rather than multi-learn from a lot of different sources.

So I am going to test this out and for the next 2 weeks dedicate it to just the one course, in my case the Headfirst Learn how to program book. There are 3 main reasons for this: 1. It has a set end goal – finish the book, 2. It has a set number of pages, this is easy enough to break-down into the number of pages I need to cover in a day ~400 pages that means I should do around 40 pages a day over the next 2 weeks. 3. It is a full course, and it is relatively short – so achievable through a 2-week test if it turns out not to be the best way of doing this then I won’t have lost too much time.

 

<< Read the previous post in this series

Give ordinary people the right tools, and they will design and build the most extraordinary things. – Neil Gershenfeld