Developing the basics: Programming myself, post one


Setting sail on the journey to learn to programme

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

So finally it is here. The day has come. The weeks of preparation, of everything beforehand leading up to this one momentous event. I’m going to start with the PyCharm Edu and the Introduction to Python course because I want to get down and dirty and get right into the code and see how far I can get before I can’t understand a thing. This gives me a chance to then do some extra reading and some other learning on the side. Anyway, let’s do this! Let’s get the console to print Hello world!
For many people, this has been claimed as the most momentous piece of code they have ever written. Maybe it is just me and the world has spoiled all the magic, where even my robot vacuum cleaner ‘Dustin Beeper’ can work out a path around the toys in my living room with ease; that printing a hello world on the screen is not as impressive as I would have hoped. Maybe if I change the words around? Chuck in a few swear words for good measure. Ahh, that’s a bit better. Like the first words of a child, you only get to have them once, fingers crossed they are epic, in this case, you actually get to choose them for yourself.

print("what is going on here :) I don't "know" but it is awesome")

fail hello world

So… don’t do this and try – print(“do not add “quotes” to the middle of a string as apparently, they break it!”)

My first error… of many… many to come.
SyntaxError: invalid syntax

Actually, PyCharm Edu was trying to warn me, but what can I say “thug life”.

Try again!… This time with a little trick known as escaping.

hello world

Hello, World! itself is a very simple program. Yes, your first program… the culmination of all your computer knowledge to date. Congratulations. But it is steeped in tradition, and it does have its uses. The first is, it is a good quick test to check you have installed everything right, as if it doesn’t work then there is something very wrong. It lets you check out the syntax, by this I mean the parts of the computer language and how to quickly call on the components the language uses to do different actions. Its final use is as a tester to check a proof of concept, so checking that a piece of code can be run. This is used by hackers, for instance, to prove that code can be executed where system designers didn’t want code executed.

Ok enough of these awesome facts, back to work.

The introductory course in the PyCharm EDU covered the following and gave a taste of each:

  • Variables
  • Strings
  • Data structures
  • Conditional expressions
  • Loops
  • Functions
  • Classes and objects
  • Modules and packages
  • File Input and/output

These are the basics of what all the programs are in some way made up of. Working through this you can see how the different parts are working, but I wouldn’t say I can now go away and immediately make a program, but as a start and to get into a habit of learning a bit every day, this has been useful.

I actually got pretty into the PyCharm EDU Introduction to Python and made most of my quota of time just going through this. An important part of this is: not to cheat the tasks by looking them up on the internet *cough* as later on the tasks roll on and if you missed something then it is hard to get back on track. I would say it also really is kind of useful to understand the functions of the code which is why I liked doing this as a first off task to get acquainted with the code itself. If you can get through this, already you have a really strong basis for the code, you can see here all the basic things that people do with the code and how they are done.

Quick tip:

So if you are using the PyCharm Edu as I am, you can make the instructions window better, by clicking on the cog and selecting float. Then you can place this somewhere else on the desktop and it doesn’t get in the way of the rest of the IDE.

My advice for this would be to copy out some of the code snippets to revise on after and also to try and write out the code from scratch… Maybe you think this is easy… seriously try and write out one of the tasks from scratch with no guidance. It is pretty difficult, but this type of exercise really forces you to engage with the information. Also at this point, it helps to test things out and get things wrong, if you have got the answer, yet don’t understand what a piece of code is doing in all of this, it is a perfect chance to see its effect by simply testing for yourself.

Important lessons I learned

Always remember that the demonstration code runs and will output too in the console. I found this when I was doing the for loop and not realizing how the 4 kept turning up when I was trying to output prime numbers… 20 minutes of head scratching and then asking a developer resolved this trap.

Commenting the code points with what the parts of the code are is better than commenting what it is doing, you can see what it is doing, but harder to see is what the things are that it is doing it to. In Python, there is a hashtag #lovetocomment which is used to make the code kind of transparent so it does not run. Commenting – from my experiments – needs to be done line by line, as in all the lines which you want to stop running need to have a hash included at the start. You can comment out the task examples, so you can get feedback on the exercises you want to do.

Naming variables – seems simple enough. Wait it can’t start with a digit, so no 5hit variables then, or symbols ~. Or it seems, any of these words :

False, class, finally, is, return, None, continue, for, lambda, try, True, def, from, nonlocal, while, 
and, del, global, not, with, as, elif, if, or, yield, assert, else, import, pass, break, except, in, raise.

These are all Keywords and are reserved for special functions, so you can’t use them to name things. The best practice is to name the variables in a way which you can understand what they are supposed to contain immediately.

There are 2 types of numbers: float and int. Float contains a decimal place and int (the short form for integer) is always a whole number which will round down 2.643 as an int is 2. You can convert them to the kind you want with float (the variable you want to change to a float) or int (the other way around). Basically, the difference between the two kinds of numbers is: a float has decimal places after the main number, for extra accuracy, while an int is the closest whole number which it will round down if you have a decimal point in there, so you might lose accuracy.

Computer Science takeaways

Outside of the PyCharm EDU platform, I have also been learning certain computer science concepts and generally about how computers work. The first point is computers themselves only actually understand 0’s and 1’s otherwise known as binary. Effectively every single programming language is communicating with a computer through talking the computer’s language ‘machine code’. This means that whatever code you are coding in, in whatever language you are using, needs to be ‘translated’ into the language the computer understands. So the code you write (your ‘source code’) has to be processed before it can run. In Python, this program is called an interpreter. The interpreter processes the program bit by bit in order performing the computations as it goes. An alternative program would be a compiler, this is a little different as in this case it converts the source code completely into object code, this can then be run over and over without the ‘translation’ program needing to be used anymore. It is generally believed that the compiler method is faster to run as it doesn’t need to refer and translate the code each time, it is a self-contained executable.

Also, computers are really quite stupid. No really. They can’t infer meaning from the language. What you say to it specifically is exactly what it will do so if you don’t write exactly what the computer understands or if you write something in the wrong order the result it returns is not dependable. “3 Chickens” + “2 Chickens” = “3 Chickens2 Chickens”… This leads to what is known as semantic errors where it seems to work with no error at all, but the result is wrong because of how it understands it. Programming is a formal language, there is no room for thinking the computer will know what you want it to do; you have to tell it specifically.

This is why a programmer will find this joke clever:

A programmer was sent to the store by his wife:
“Get bread, and if they have eggs, get a dozen”
The programmer came back with 12 loaves of bread.

Also, you need to think about the order the calculations will be executed, back in high school we had BODMAS – brackets, orders, divide, multiply, add and subtract. A second one might be PEMDAS – Parentheses, exponential, multiplication, division, add and subtract. Exactly the same but depending on how you refer to the operands one may be easier than the other for you to remember. They are important to consider though, as 3 * 2 + 1 is different from 3 * (2 + 1), if you are in doubt as to the sequence then you can break up your calculations with parentheses|brackets to make sure they execute correctly.

test the maths
As a final take away – all programs are made up of the same basic instructions:

  • Input – There is something that is needed to be put to the program, a variable for instance.
  • Math – or some kind of action, this might be a function, which is the action on a certain piece of code.
  • Condition – check for certain conditions which branch to then do other things depending on the result.
  • Repetition – Or loops, where you can keep doing something repeatedly,
  • Output – What the result of this is.

With these five options nearly every solution can be made… Mind blown.

<< Read the previous post in this series


Do or do not, there is no try. – Yoda