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!


No comments:

Post a Comment