Sunday, 26 June 2016

Learning Python part 2: The syntax

To write programs I needed to learn the language, the syntax to implement any programming ideas (ie algorithms).

Here are the following topics I wanted to learn:


  1. Variables (create, change, delete, use), data types and their operations
  2. Comments in code
  3. Numbers(integers, floats), expressions, their methods
  4. Strings, print, interpolation, command line input, their methods
  5. Booleans, conditionals, logic, expressions
  6. Conversion between data types ie string to integer
  7. Lists/arrays, and their methods
  8. Dictionaries/hashes, and their methods
  9. Control statements (if-else/ if-else-if/ ternary, case-switch)
  10. For loops, ranges
  11. While loops, break, exit
  12. Functions/methods, parameters, creating, calling, arguments, return
  13. Classes, initialize, instantiating, instance variables, composition, instance methods, class methods, self, 
  14. Date/time library
  15. Importing, modules/libraries
The following topics I will cover when I need to, as they are not necessary for the majority of code I will build at the moment.
  1. Errors
  2. File input/output 
  3. Lambdas
  4. Bitwise operators
  5. Inheritance
Plus being able to run the code, using a REPL that you can type code in directly and play around with it.

The above ideas will allow me to create code and learn how to implement testing and object oriented design later on.

As I have already done some coding in ruby and javaascript the fundamentals are going to be the same, i.e. if statements, loops, etc. But every language has different ways of writing them down for the computer to run them (for the compiler or interpreter to turn into computer language for the computer to run).

So I went back to basics and started the track on codecdemy.

I did not complete all of it, I skipped the file input and output section, I can always come back to this when I need to use it, but I managed to cover all the core ideas that I needed to know. Also, I was taking notes on some of the syntax differences to ruby. This I felt would make a good 'help me guide' to refer to when stuck on how to write an idea/algorithm in python.

As I have prior experience in coding, I did not bother reading the descriptions for each lesson, instead, I completed the tasks and went back to the examples in the description to help me if I was stuck. This helped me speed through the python track in around 3 hours, instead of the recommended 13 hours.

NOTE: If learning as a beginner to programming, I would suggest, to take your time learning the syntax, play around with each idea by creating similar examples. Maybe create a summary guide with your own explanations and examples for each part of the codecademy. 

Learning syntax can be very tricky at first, so I would advise using another resource, as a different explanation might prove useful, try Zed Shaws Learn the Hard Way. A google search for resources will yield some books and links. 

NOTE: Avoid reading just cause you have not got it. Focusing on reading, typing (no copy and paste) and playing around with it. Only when you have issues with certain ideas, then look for an explanation.

Testing I can use the language!

How do you know that you understand or can do something? 

Well, you don't ask that question and answer yes. As a newbie, you are not in the position to assess yourself as such. You don't know what you don't know! Instead, you need to test yourself. Can you code using the ideas with out any help or explain the ideas clearly without errors??

To truly know I could use the syntax, I did some Katas (coding problems). Normally, I would use codewars, but codecdemy had a section in the Python track with quite a few katas. Doing katas is a great way of letting go of the hand holding of a tutorial and apply some problem solving together with revising the syntax.

Solving problems with code is very hard for beginners, as you have to think in a logical and algorithmic way. Being able to change a worded or even a mathematical problem into and algorithm requires a lot of practise. Having a background in maths (or any mathematical background) does help with this, but I still need to practice. Will make a post on solving katas later.

Test syntax


Apart from the syntax of a language, I needed to understand the structure of tests. This took a while and I needed to find some examples. This lead me to searching GitHub for example code (by typing 'python tdd' in the search box), and reading the documentation to understand what was going on when I was stuck. Although, Zed Shaws Learn the Hard Way had some info on testing which was useful.

I also had ipython (a REPL) installed (similar to pry for Ruby) so I can play around with the new syntax. Plus lots of colours and probably other features that could come in handy.

I am not to fussed about memorising everything, as I will be creating programs and recalling the syntax from memory first (see testing effect). If that does not work or I forget, I can alway check my code notes or the internet, which will also help make that idea stick.

Another great place to ask for help is stack overflow, it is a great way of formulating the problem that you are stuck on. Which in a lot of cases solves the problem. I would  suggest looking for previously asked questions via Google like typing 'Stack overflow <key words or problem you have>'. If all else fails, ask the question, but be specific, write the code you have issues with and follow the rules. 

One thing that is bugging me are the indent errors, because of the difference between a space and tab in the editor causing several breakdowns in myself and the python interpreter. At least I do not have to deal with missing semi colons, looking at you Javascript!


Thursday, 16 June 2016

Learning Python part 1: The why and the setup

This week I have decided to learn how to use Python. 



Python is a popular language, with lots of libraries and has become one the leading languages used for data science and machine learning

I felt this would be a great way to show people that I could learn a new language and be able to do some programming and apply design techniques to it. 

I am not perfect at the other languages I know, ruby and javascript, but I feel comfortable in using them, so doing this is also proving to myself that I can learn a language independently. 

After Makers, I already have a framework of how to go about this and have a plan which I will document my progress here.

So far the plan I have set myself is:


  1. Setup 
  2. Learn the syntax
  3. Create a test driven Fizzbuzz program
  4. Create a test driven tech test program and apply object-oriented design 
  5. Creating another program, something new like a game using the terminal.
  6. Create a web app or game (any sort of project that uses libraries and/or frameworks)

The setup


Aim: Setup system to run a basic test driven 'hello world' program.

To use python I needed to install the necessary files to create and run python scripts and tests.

NOTE: This is based on using MacOS and with homebrew installed

I installed Python using brew install python3 in the terminal.

NOTE: There are two versions of Python, version 2  and version 3. I would suggest going for version 3 (as done above) as this generally used with web framework Django (and I believe helps if using Windows).

Next, I created a project skeleton, using this tutorial, I would follow this and run the tests.

NOTE: A project skeleton, is just how your files and folders are setup. It is always good to separate your tests from the source code via different folders. This does help with big projects with lots of files.

I used an extra library, to add colour to my tests (I miss RSpec): pip install rednos

NOTE: 'Pip' is a library manager just like bundler for ruby, or Maven for Java. Pip comes installed with the latest version of Python.

So to check everything is running fine, I would enter the project skeleton directory ( ie ' cd <project name>' ) and run tests with this command

nosetests --rednose -v

The '-v'  add information, ie a description of the test, to show what tests were passed.

This will produce the following output:

$ nosetests -v --rednose

tests.NAME_tests.test_basic ... passed
-----------------------------------------------------------------------------
1 test run in 0.003 seconds (1 test passed)

NOTE: You can just run tests using nosetests if you want a simple output, just a series of dots for passing dots.

NOTE: Adding '--with-coverage' at the end of the above statement, you can get the test coverage of your code. Focus only on the files that you have created rather than any extra files displayed. Doing TDD means getting 100% coverage!!!

Note: This is such a common command  I added this as an alias so I could just type in pt (short for python test). Check this tutorial if you want to do this, I have created quite a few aliases for common git commands too.  A really useful one I found is to create a new folder and enter it

So to create a new project,  I need to copy the project skeleton and rename the folder as the name of the project. Then change all instances of 'NAME' in the files and filenames and I am good to go.

NOTE: Before going on to creating a simple program with tests, I learned some of the syntax first.

To complete the setup, I created a basic hello app, which takes a name and returns "Hello <name>" and tested it.  To view this and see how the project skeleton was altered check the code and browse through the files here:



Issues

I had a lot of issues setting up a project. I like to separate my tests and program scripts into separate folders, but running the tests caused so many issues with finding the program for the test to run against. Eventually, I found Python the hard way which had a very easy to follow tutorial that helped me setup a project skeleton. It is also a great way of learning the syntax too.

Using nose to run my tests, solved so many issues that running 'unittest' or the script using python command, were giving me.

Naming the descriptions of tests were not great, so I decided to use a naming convention so the tests appear in order on the terminal to the order on the test script, rather than alphabetically.

For example

test_1_description 
test_2_description

I also added doc strings """description in here""" at the start of each test, which gave a nice description of what was happening, kind of like an 'it block' in RSpec. This would show when running nosetests with -v flag in terminal.