Education

Developing the basics: programming myself, post eight

treasureMap_8

The foundations to build on

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

I am now on post 8 which for reference is about 8 weeks in, and it is time to do a quick review and check that we are still on course.

My original study plan looked something like this:

Monday: 2 hours Adaptive Python
Tuesday: 3 hours Headfirst Learn how to program
Wednesday: 4 hours theory CS50
Thursday: 4 hours Adaptive Python
Friday: Rest day
Saturday: Rest day
Sunday: 4 hours Introduction to Interactive Programming in Python

 

In theory great. In practice not so great, as it started to look more like this:

Monday: 2 hours of nothing
Tuesday: 3 hours of nothing
Wednesday: 4 hours of nothing
Thursday: 4 of nothing
Friday: Rest day
Saturday: Rest day
Sunday: 4 hours of panic and regret and hard study.


 

I managed to turn it around a bit by building in some triggers, mainly PlayStation, to try and form a study ritual. PlayStation for an hour, then onto studying. This worked well for the HeadFirst book, but now I am going back to the other courses, and they are different kinds of courses completely, and it will be interesting to see if they work as well with this system. I think taking the courses one at a time is the best method, so now I will start looking into the computer science side more and focus on the CS50 course from Harvard University as it has a lot of people who advocate for it.

 

Where we should be at right now

But first, let’s take a look at how exactly far we have come from simply the foundation Python and the HeadFirst learn how to program courses in the last couple of months.

So what I set out to learn, to say ok I can program (a little bit):

Basic syntax of the language: statements and expressions

There are a few that will get you started:

import - to import cool libraries
print() displays the output on the screen
input() lets you input a value
int() converts characters to numbers, str(), float() alternatives work the same.
randint() produces a random number
upper() converts strings to uppercase
return() get the value back from a function
open() open a file and close() to close
def
if
while
else

 

Then you can put some of it together in a quick hello world!

name = “Bond, James Bond.”
print(“Hello, PyCharm! My name is %s!” % name)

What variables are and how to assign a variable

Easy. Variables are containers, and you assign variables saying something like

Variable1 = something

We can go even further with this and use some special variables using the Tkinter library called IntVar which can be set to different values:

Variable1 = IntVar
Variable1.set(something)

Label(app, textvariable = Variable1)

This would print “something” as a text label
Variable1.set(something else)

* Then this would change the text on the box to “something else” automatically whenever the variable amount changes.

 


Know the basic data types like integers, floating points, and strings, and the operands to manipulate them

Simple enough.
An integer is a full number like 4 or 5, as soon as you start trying to add integers to numbers with decimal points though, you release the fury. The two don’t play well together.

Floating points, again a number but this time a number which can include the points after the decimal place. 3.14159265359 is a good example. But to avoid mistakes, it is better to import a math library and call pi from there instead.

Strings – Always come in speak marks “like this” you have to escape a string to put a speech mark in it “something \”like this”\ should work”

Being British by birth, I would go for BODMAS for my operands. On top of the normal math ones you need consider the meaning of <, >, <=, >=, !=, %, :,

 


Know and be able to use the basic control structures: conditionals and loops

This is basically covered with if, else, and while loops.

This logic can already get you quite far, as the conditionals can be used everywhere to cause a result depending on another result. The While loop can keep a program running until you have finished.

Python is nice as you need to remember to indent a condition to the same level. Conditionals too can only be compared against conditionals of the same type, int vs. int, str vs. str, float vs. float, but once this is nailed down, the logic just needs to be thought through and you can achieve a lot already. The Rock Paper Scissors Lizard Spock game for instance.

Answer = “no”
While answer == “no”:
Answer = input(“are we there yet? “)
Print (“Next time you ask if we are there yet I will turn this car right around”)

 


Be able to decompose code into functions and methods

This part I am still not great at or if I even understand it right, I am pretty sure, it refers to finding parts where a piece of code can be reused. So rather than creating a method which does the same piece as before, you create a single function which can be called on each time. The reason being is that if you then change a piece of code, you do not have to find every time it is referred to. Instead, you change it in one place and not hundreds. So it is about not copying and pasting code, but instead reusing it with a function.

 


Use some basic data structures

This seems to be my Achilles heel. I don’t think I really know any basic data structures? Just to check though on the *internerd and it turns out data structures are lists, tuples, dictionaries, strings, sets, and frozensets.

I have come across each of these at least once with the PyCharm Edu foundation course. Lists and dictionaries I am comfortable enough with, though dictionary is referred to as “hash” in head first Programming. Strings too. Tuples, sets, and Frozen sets need to go on my list of things to go over again though.

From PyCharm Edu:

“List” is a “data structure” — where we can store a collection of different pieces of information under a single variable name.
A list can be written as an “array” of “comma-separated values (items)” between square brackets:
list = [item1, item2]
The list might contain items of different types, but usually, all the items in the list are of the same type.
Like the “string” === “list” can also be indexed and sliced

squares = [1, 4, 9, 16, 25] # create new list
print(squares)
print(squares[1:4])

Tuples: Identical to “list” — the difference is tuples cannot be changed: we can’t add, change, or delete elements from the tuple.
Tuples are constructed by a comma operator enclosed in parentheses, for example: (a, b, c)
A single item tuple must have a trailing comma, such as (d,)

alphabet = (‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’,
‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’)
print(len(alphabet))

Dictionaries: A dictionary is similar to a “list” except that we access its value by looking up a “key” instead of an index.
A key can be any string or a number.
Dictionaries are enclosed in curly braces e.g.
dic = { ‘key1’ : “value1”, ‘key2′ : “value2″ }

phone_book = {“Jill”: 123, “Jane”: 234, “David”: 345}
print phone_book
phone_book[“Jill”] = 4534
print phone_book
del phone_book[‘David’]
print (phone_book)
print print_book[‘Jane’]


Useful dictionary methods are
keys()
values()


Understand arrays or list types

I think this is mainly covered from the point above. There are some subtleties between the different types which I should now begin to look into and try to use a little more.

[ ] this is an empty array
You can sort() and reverse() the data stored in an Array.
Or combine them and have sort(reverse = True)

Then print the values in order starting from [0]

 


Understand dictionaries

{ } this is an empty dictionary

Again this is still being covered under the same data structure points. This is a subject which basically I have found myself breezing over but not paying too much attention to. It is a big part of the fundamentals of programming though, and you need to really have a good understanding of the data structures as they are useful for many different applications.

 


Record data structure classes in Python

All of this comes under the same data structure umbrella. This subject needs to be revised as off the top of my head I would not be able to hash it out (pun intended) but any of these would not take too long to work out again. Important for me is understanding the uses of each type in the way they are best used.

Dictionary, for example, is the best if you have a few bits of information relating to the one unit, for example, name, age, telephone number – they are all different information but for the same thing, which you cannot do in an array.

 


Iterate over arrays

“for” loops are used to iterate over a given sequence.
On each iteration, the variable defined in the for loop will be assigned to the next value in the list

primes = [2, 3, 5, 7]
for prime in range(len(primes)):
print(primes[prime])


Read and write from files

There is a very simple way to open files in Python
Open (“file.txt”)

And a simple way to write to files in Python
Write()

And close the files
Close()

Extra useful extra methods:
Split() to split up the strings in files
For line in – read the lines of a file.

 


Be able to debug code

Although I have a long way to go, debugging code is starting to make more sense. I can generally work out the cause of the issue if then I can’t work out the solution. See this blog post for debugging and this one from our WebStorm for how to use the debugger functions in the IDE’s.


Know the basics of good coding style

Comment your code and avoid code bloat with copy and pasting code. Name variables and functions logically. It is a start, but there is still a long way to go.


So that is where I am at

At 8 weeks I seem to have at least covered the essentials, they all still need a lot of practice to really come to terms with, but it is interesting looking back on the things which I have covered over the last few months and seeing some progress. I hope you too are getting by, and that by reading these terms they spark a little familiarity with you. It is just now at a point where you can start practicing and building on the knowledge, the thing that people will never tell you about being a developer – every day is a school day, there is always something new to learn.

 

“Take pride in how far you’ve come. Have faith in how far you can go, but don’t forget to enjoy the journey” – Michael Josephson