My Very First Python App – A Digital Bookmark

Persistence without Focus or Direction

When I first began to study Python programming a few months ago, creating an app, no matter how simple, seemed like a pipe dream. While I was beginning to understand some of the Pythonic concepts in theory, I didn’t know how to apply them. I battled on with broadly unrelated books, articles and courses, but this inevitably lead to a lack of focus and belief on my part. I knew the principles behind learning something new, but it seemed like I spent a lot of time in persistence mode while being out of sync with the focus and incremental progress that I also needed.

Connect the dots with Python OOP

Prior to taking the Python OOP Course I had read a little bit about object oriented programming but not an awful lot sank in. I was still coming from a place of trying to comprehend what a function, class and method was. This course has given me a sense focus and direction that I previously didn’t have. I firmly believe in starting small, keeping things as simple as they possibly can be, and building on my skill sets from a solid foundation. I feel this course has given me this along with the practicalities of tangible apps to start a portfolio with. The great thing about this course is it’s ROI on time and money. A student could easily get through the course in a matter of hours. Personally, I have chosen to gradually work my way through it while working on solving other problems and using other resources for learning Python. My first real app that I have made as part of this course is the below version of a digital bookmark.

Digital Bookmark

As part of the Python OOP course, I recently created a bookmark for my favourite websites.  I used the tkinter module to create the window.

Screen Shot 2017-04-21 at 09.31.10

Bookmark Customisations

I made two main customisations from the tutorial I followed.

  1. Change background colour.
  2. Include an image.

To do this I had to do my own research. It would be much easier for me to stick to exactly what I am shown but that is not what I am encouraged to do, and I won’t be able to retain the principles behind what I am learning if I do so. While my mind can sometimes be lazy, I find that it is often open to making small tweaks. Below is a description of the two adjustments I made, while my mind was telling me this seems like too much work:

Background Colour

To change the background colour of the window I simply had to add this line of code:

window.configure(background=’black’)

Sometimes the most noticeable changes are the easiest to implement 🙂

Adding an Image

I imported the PhotoImage module from tkinter in order to insert the GIF image.

from tkinter import PhotoImage

P1 = PhotoImage(file=”image.gif”)

I initially wanted to use a JPEG image but it wouldn’t work for me. From what I read it seems to be easier to insert a GIF image in tkinter. I don’t fully understand why this is but I felt that it wasn’t necessary at this stage to pursue the answer any further. Once I saw that the GIF image appeared, I then spent my time positioning the image to where I liked it best.

Positioning

I used the .grid method to position the elements in the bookmark window. There are 3 main ways to position in tkinter

  1. Pack
  2. Grid
  3. Place

When using the .grid method the window is divided into grids with unique coordinates for row and column. The positioning is relative. For items to appear horizontally the row value remains constant while the column values increment, for items to appear vertically the column values remain constant while the row values increment. Here is a table to demonstrate this logic when inserting buttons in a window with the .grid method:

Vertical Buttons Horizontal Buttons
column=0 row=0 column=0 row=0
column=0 row=1 column=1 row=0
column=0 row=2 column=2 row=0
column=0 row=3 column=3 row=0

Here is a tutorial on tkinter positioning, that goes into more detail.

DRY (Don’t Repeat Yourself) example

The Cleverprogrammer landing page and the Cleverprogammer Blog have the same beginning to the URL. The only difference is that there is a /blog in the blog URL. Therefore, in the course, we created a variable called URL and to direct to the blog we simply used the variable name URL + /blog.

Source Code

Screen Shot 2017-04-21 at 09.31.24

So there you have it, with my first app in Python now completed, I am looking forward to seeing what problems and apps I can tackle next 🙂

Did you find this blog post useful? I am continuously looking for ways to improve on the content so that my readers have the best experience possible. If you have any insights, questions or recommendations feel free to leave a comment or drop me an e-mail.

80/20 Python & Garden Peas

The Birth of the Pareto Principle

The seed of the 80/20 principle germinated, of all places, in an Italian pea garden. It was peahere that the economist Vilfredo Pareto observed that only a ‘vital few’ of the pea pods in his garden produced the majority of peas. He went on to use this principle to demonstrate that 80% of the land in Italy was owned by 20% of the population.

 

I believe there is truth in the maxim

 how you do anything is how you do everything

Ever since applying the 80/20 rule to language learning I frequently view the world through this lens.

Python 80/20 in action

When I learn something new I start by reading around the topic and taking some online courses. Usually these courses are short and free, but not always. This period could last anywhere from a few weeks to a few months, depending on the difficulty of the topic, how natural it comes to me and my current level of knowledge in it. For Python, I spent about 3 months before I made my 80% decision. Generally I will spend enough time to catch a glimpse of the ‘bigger picture’, I then look to focus about 80% of my learning on one resource. The other 20% is divided among a handful of other resources, that alternate and change as my level of knowledge increases.

80% ‘in’ is often a wiser move than ‘all in’

For the sake of clarity, 80/20 isn’t an exact measurement, it is a quick and relatively accurate ratio. Our brains are hardwired to make sense of the world through generalisations. If our brains were incapable of doing this, then the world would appear to be a much more difficult and scary place for us to live in. Instead of using this mechanism to ingrain stereotypes deeper into my head, I have chosen to leverage this innate capacity, when I scan the world with 80/20 goggles on.

The main reason I don’t go ‘all in’ on a single resource is that I find that this approach often leaves me ‘blinkered’. Even if I have the best mentor in the world, he/she is only human, and humans can only experience their own thinking no matter how hard we may try. Therefore, even if he/she is Six Sigma gold and makes a handful of mistakes per million, that could still be a few mistakes more than I may need to personally make.

I also believe that when I have a narrow focus of  attention on one single person or area, it leaves me wide open to becoming a victim of a dogmatic approach. What I look for in a good mentor is that they not only encourage me to use my common sense but they also recommend resources that they have found valuable themselves. This demonstrates that they are a team player and that they are doing the best they possibly can to help their students.

My 80/20 Python Plan

My goal for learning Python is to become a more effective problem solver, create personal projects, freelance and potentially become a Software Developer. With these goals in mind, I have included the below table of my 80/20 learning plan. My learning is centered around Clever Programmer because the platform is in alignment with my personal goals.

80 20
Cleverprogrammer SoloLearn
HackerRank
Project Euler
Code Fights
Talking to Friends/ Meetup Groups

 

Did you find this blog post useful? I am continuously looking for ways to improve on the content so that my readers have the best experience possible. If you have any insights, questions or recommendations feel free to leave a comment or drop me an e-mail.

tkinter, Tkinter in a Graphical World

Screen Shot 2017-04-12 at 18.09.32 I successfully ran my first import of the tkinter module this week. tkinter is a module I have been aware of in the past but it isn’t something I was able to use until now. The code I wrote as part of the Python OOP course allowed me to create the above GUI (Graphical User Interface) window with the word Banana. This may not look like much but I think it is incredible how quick and simple this can be created in Python. While it is simple I don’t want to give you the false impression that it is easy. Even while taking a course, I find that a beginner mind, will tend to find mistakes for you. It is almost like perseverance is a shadow that follows you around every corner until you learn to see through its self created nature.

The biggest obstacle I have encountered so far reappeared once again, when I was trying to import the tkinter module. I followed the code exactly as it was writtenScreen Shot 2017-04-12 at 17.59.35 in the course video. I must have rechecked these 7 short lines of code about 20 or 30 times and I spent some time trawling the internet for an answer. I was left feeling frustrated, but knowing that I wasn’t going to quit, I decided to deflect my attention to solving problems in the ‘easy’ section on HackerRank. I generally find HackerRank problems tricky but not too tricky. This strategy allows me to keep building on the invisible force of nature that is momentum.

It may have helped if I were able to assimilate the ImportError: No module named tkinter that appeared in terminal but as a novice programmer I don’t always find my intuition guiding me to read the error and think of some logical explanations. In this case, the error seems more like an outlier for a beginner programmer rather than a rule to soak into my awareness.

Solution

I put tkinter to the back of my mind and I eventually found the answer when on a call with my Python mentor. The mistake was so simple it was almost embarrassing. When trying to run my tkinter_intro.py file , I was attempting to run the below terminal command:

python tkinter_intro.py

On my MacBook by default this will launch Python 2.

I should have typed:

python3 tkinter_intro.py

This would have launched Python 3. My MacBook comes preinstalled with Python 2, I manually installed Python 3 when I began to learn Python. There is also the alternative to make python an alias of python3.  The reason there is an issue in the first place is that in Python 2 the word Tkinter is capitalised and in Python 3 it isn’t. The problem I encountered was that the GUI window would fail to launch when I ran the Python script because Python 2 does not recognise tkinter as a module.

Reflections

When looking back on the process I can see that I was caught in the trap of over complicating a problem that had a simple fix. It is the same feeling a student gets when challenged by a curious philosopher with the below task:

Prove this chair does not exist

In effect, I tried to come up with some complex theory that would explain the existence of the chair. As long as I was looking in this direction I would never find the fix, simply because the more time I spend looking in the wrong direction, the less likely I am to find the simple fix right in front of me. What allowed me to eventually look in the other direction was giving myself distance from the problem by putting it to the back of my mind, and by having a mentor that had already been there and done that.

 

Did you find this blog post useful? I am continuously looking for ways to improve on the content so that my readers have the best experience possible. If you have any insights, questions or recommendations feel free to leave a comment or drop me an e-mail.

 

Fibonacci Sequence

Problem set: Given a fibonacci list of 2 items, add the next 10 numbers in the sequence and return the sum of the even numbers only. 

I was working on a variation of a fibonacci problem set on Project Euler recently. I spent the best part of two weeks trying to find a solution. I ended up solving about 50% of itfibonacci at most. Does this hurt my ego to admit this? HELL YES. Does this matter in the greater scheme of learning Python? NO, NO IT DOES NOT.

There were a number of things that I had to wrap my head around just to acquire an understanding of how to approach this problem. The two primary challenges were:

  1. Finding a way to add the next number in the sequence. (In a fibonacci sequence you get the next number by adding the sum of the two previous numbers.)
  2. Adding the even numbers.

In the initial few days, I try to figure out these problems using only my thought process. This is a lot tougher than searching for answers across the internet, but it means that my thinking process will gradually change and improve. This needs to happen in order to become an effective computer programmer.

Key Error

Attempting to solve the whole problem within one function was the biggest error of judgement that I made. I was unaware of the fact that I could make a second function to calculate the sum of the even numbers, and then pass this function into the fibonacci function. It was by going through the fibonacci problem set with my Python mentor that I came to see that a function can be passed inside another function. Once I saw this it made sense for us to create a sum_even function that could be passed inside the fibonacci function.

Screen Shot 2017-04-09 at 09.10.36

This makes use of the Don’t Repeat Yourself (DRY) method. It saves me time in the long run because if I want reuse this code I just need to call the function named sum_even

In programming, when I look at the reusable code in concepts like functions, classes and libraries I am often reminded of the Isaac Newton quote:

“If I have seen further it is by standing on the shoulder of giants”

Muscle Memory

I know that as I move forward I will need further exposure to this concept of passing a function inside another function, in order to incorporate it into my muscle memory and my automatic way of thinking. However, I know that my newly acquired awareness that such a concept even exists means that it will now be in the back of my mind somewhere.

Indentation

Frequently, what seems to catch me out is where to put the return statement. Up until a couple of weeks ago I wasn’t clear on the difference of returning inside a loop or outside it. A simple way to see whether a return statement is located inside or outside a certain loop is to look at what it matches up with. For example, in the above sum_even function the return statement matches up with the for loop and is outside of the if loop.

Fibonacci Solution Summary
In order to solve the fibonacci problem set, we had to break it into smaller subsets. First I figured out a way to add the next number in the sequence a certain number of times. In Python [-1] is always the index of the last item and it moves backwards from there. So [-1] is the last index, [-2] is the second last index and so on and so forth. For whatever reason, it took me a while to get used to this logic, but like in linguistics there are certain things that you just have to accept in order to move forward. The .append() method allowed me to add an item to the list and what it adds is the sum of index [-1] and index [-2]. The number of times I want this to execute is represented by the number within range and -2 serves to take away the number of items that are already in the list.

Screen Shot 2017-04-07 at 21.03.29

Next I had to find a way to add the even numbers. The above sum_even function was what allowed me to do this. The main thing to notice within this function is that an easy way to return an even number in Python is to use the modulus operator %. The modulus operator looks like a divisional operator but it gives you the remainder after division. So if a number is even, when it is divided by an even number like 2 there should be a remainder of 0.

Separately these solutions made sense to me, but I wasn’t at a level where I could pull the pieces of the jigsaw together just yet.  The key things for me were that I put an honest effort into solving this problem, I could understand the component parts and I had a mentor who could help me put the pieces together.

Solution

Screen Shot 2017-04-09 at 09.09.42

A Blast from the Past: The Fibonacci sequence came into existence from the medieval Italian mathematician Leonardo Pisano in a discussion about the problem of breeding rabbits. In the present day the fibonacci sequence is used in numerous fields such as music, visual arts, and architectural design. It is even used to explain phenomena in nature and human psychology.

When Python became more than an ‘Afrasian’ snake

Language learning helps me learn to code
 
It is difficult to see the bigger picture when you haven’t previously seen it in a different field. In my experience, there is no such thing as a completely unrelated field. In every field I have studied I have seen the presence of fundamental underlying principles. While seeing the bigger picture once doesn’t guarantee I will see it again in another domain, it does mean that I have a solid point of reference. When I have a point of reference it is easier for me to recognise what I am looking at to begin with. One practical overlap is the fact that syntax and semantics are frequently spoken about in both linguistics and programming. They are spoken about in pretty much the same manner because we are dealing with languages in both fields, it’s just in one field we swap a human out for a computer.
 
Why Python?
 

It took me a few months before I made my final decision to commit to Python. DuringPython_Logo this time I briefly flirted with JavaScript, Ruby on Rails and PHP. I don’t view this as wasted time or a period of slight procrastination because, among other things, this allowed me to see that becoming good at one language would make it easier to learn another. This is largely because the semantics behind the language remains static by nature, while the syntax and the purpose of the language dynamically changes. Shakespeare once wrote in Romeo and Juliet;

‘ A rose by any other name would smell as sweet’

I am now going to butcher this phrase and say that in programming;

‘A loop by any other is still a loop’

Once you learn to think like a programmer you don’t need to relearn it, you just need to work on becoming a better programmer by widening and deepening your understanding. The exact same approach is found in the field of linguistics and language hacking. I firmly believe that a minute spent properly planning and organising myself is worth hours, days or even months to my future self. I wanted to choose the language that I thought was the most versatile and had a relatively simple syntax. Based on this criteria Python was the language that stood out for me.

Python Focus

A few online videos, books and courses later I found my current Python mentor. He seemed to have a similar way of looking at things, but with a lot more domain experience and expertise. This gave me hope because I seemed to have made a good decision with a shallow conceptual understanding of programming. My choice was based primarily on my experience with online learning and from conversations with a friend who had taught himself Ruby on Rails. I believe in the maxim that I need to be a good generalist to become a great specialist and so far I feel it hasn’t steered me too far wrong.

 
Here is a video from my current Python mentor Rafeh Qazi explaining why he sees Python as a good choice for a ‘newbie’ coder. He gives an overview of different programming languages and what they are used for. He explains why a student would choose Python and why he/she might consider learning more than one programming language. Qazi also talks about human languages in relation to programming languages, this makes this video a lot more accessible to complete novices:
New2Code is a website created by William Kennedy. William is a self taught ruby on rails developer, one of the areas he focuses on is soft skills for computer programmers.
 

Fun Fact: Python actually refers to the British comedy show Monty Python. Teachers of Python often use variables names such as ‘spam’ and ‘eggs’ in their code rather than the more frequently used ‘foo’ and ‘bar’.

Did you find this blog post useful? I am continuously looking for ways to improve on the content so that my readers have the best experience possible. If you have any insights, questions or recommendations feel free to leave a comment or drop me an e-mail.