RustProof Labs: blogging for education (logo)
My book Mastering PostGIS and OpenStreetMap is available!

Python 3 and Jupyter Notebooks

By Kevin Reardon -- Published February 09, 2018

This post is intended to help beginners make progress in writing code. When first learning to write code, it is easy to get discouraged. I hope to help others who share my goals: begin to understand a complex subject such as Python 3, write better code, and have some fun along the way.

Why Python 3?

Python 3 is the first language I am learning because it is a great place for a novice to begin. There are lots of articles on the web explaining why Python 3 is such a good choice. Reading some of these can explain the “why” better than I am able to with my limited understanding of the subtleties of various languages. I found the following article particularly helpful.

Python 3 is used extensively at RustProof Labs and is a personal favorite of my boss, Ryan. Perhaps the most compelling reason for a novice, such as myself, to start with Python 3 is there are often fewer lines of code involved than, for example, with Java to accomplish a given task. Less lines of code can be a good thing. This makes debugging easier to accomplish.

I am including a code example written by Kunal Chawla to visually demonstrate why Python is a good choice for a beginner. The first block of code is what is necessary to print “Hello World” in Java.

class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

This second block accomplishes the same thing using Python.

print “Hello World”

The example above is written in Python 2. I have written the same print function using the newer syntax for Python 3 below. While the syntax has changed slightly, the point I am making is still valid. Python generally requires less code than Java to perform the same task.

print (‘Hello World’)

As Kunal Chawla said:

As a teacher who has introduced programming to thousands of students, I must say that I am thrilled with this development.

Moving Forward

For a novice like myself the continuing challenge is how do I start to work on a project? After looking at many examples of code, I understand how a few lines of code work independently of the larger goal of the project. For example, how to define variables. But my understanding often falls apart when I try to use those variables to do something. My solution to this problem is to break code down into small blocks, getting them to work as I expect them to, and building on my small victories. I have found Jupyter notebooks to be a great tool for achieving my goal. Jupyter notebooks are similar to an Integrated Development Environment (IDE). I think of them as documents. They contain both text and code, providing me a very human-friendly environment to experiment. I use comments extensively when writing my code. This allows me to go back later and understand why I did things a specific way. My comments also serve the purpose of helping other developers who may read my code in the future understand my reasoning.

# this line is a comment and is ignored by Python
print (‘Hello World’)

When I look at a completed project it can quickly become overwhelming. When something breaks it is difficult for me to know where to start correcting the problem. A great feature in Jupyter notebooks is the ability to run blocks of code independently. This allows me to slowly build my project with the confidence that each block of code is working correctly before I add to it. With this assurance, I can move forward.

With so many moving parts in a project, the next challenge to overcome is understanding how the parts fit together. What is a variable? What is a logical operator? How is a control structure different from a data structure? I needed a resource to help me answer these questions, and fortunately, I found one.

Education

Free online courses can be found at edX.org on a wide variety of subjects including computer science. It is here I found my first course on Python as taught by David Joyner entitled INTRODUCTION TO COMPUTING. What I like about this course is it is laid out systematically beginning with background information before jumping into specifics of Python. Code is often not linear, but my learning style is very linear. I appreciate the class style as it slowly builds one concept upon the next. With plenty of code examples in the class I use Jupyter notebooks extensively to test what I learned in class and build upon it using my own examples. I have found it helpful to apply the concepts to something of interest to me personally. Instead of building a list of food I might buy at the store, I am building a list of movies. I don’t like shopping for groceries, but I do like movies and it brings the concept I am trying to learn into my world. This is where fun comes into the equation for me and motivates my learning.

Final Thoughts

My final argument for using Python and Jupyter notebooks is that they are free and open source. And who doesn’t love free stuff?

For the novice, Python can be intimidating. If one breaks it down into manageable pieces, however, it can be a satisfying and fun learning experience. I encourage you to jump in, learn from your mistakes, and continue to move forward in your quest to learn Python.

By Kevin Reardon
Published February 09, 2018
Last Updated February 09, 2018