Education

Developing the basics: Programming myself, post ten

treasureMap_10

A-Z of technical coding terms

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

After 12 weeks of computer programming, you come to get to know some common lingo which you can use to impress your Nan with over breakfast. Here is an A-Z of some terms to get you through the day. If you are wondering how the studying CS50 is going… it is hard, interesting, fun, and repeat.


A – Algorithm

The specific instructions used to achieve a specific task. In the workflow, it would go:

Input > Algorithm > Output.

There are better algorithms to achieve certain things. See https://en.wikipedia.org/wiki/Analysis_of_algorithms for more information on this though. In a high-level language, you don’t have to worry too much about these recipes as they are done for you, all you have to do is call a function like sort() and not worry about what is going on under the hood. Unless of course, you are into that.


B – Boolean

True or false, yes or no, on or off, Boolean is concise as these options take a byte to perform.


C – Compile

Your source code is not what gets read by the computer. It is in a different language, kind of like if you wanted to try out your new French skills, but the only sentient being you could find was a shark, exactly like that.

Compilers change the source code into something called machine code, then this machine language goes on to communicate in binary to the computer. Luckily in Python, you don’t need to worry about this as it communicates through an interpreter, kind of like being able to call Aquaman in the scenario above.


D – Datatypes

Python has five standard Data Types:

  • Numbers
  • String
  • List
  • Tuple
  • Dictionary

Each has its uses and benefits, strings are used for letters and text usually, numbers are simple specific numbers. Int, long; float; complex. Lists, Tuple, and dictionaries are great for storing banks of information.


E – Events

Unfortunately making a button doesn’t mean it will actually do anything. Events are used to make it possible to interact with the code. So it will execute when something is done.
Command = button_click
Def button_click():
print(“stop touching me!”)


F – Function

The key to DRY code (Don’t Repeat Yourself). Functions are reusable code which you can pass in parameters. You have to define a function before you use it though, as the code will call back to the function when it is used. If you have to do the same calculation, use the same values, or something similar, then you should try and use a function.


G – GUI

Graphical User Interface. Making code that runs in its own app is much more interesting than having to run and see everything in the console. Python has some built-in GUI components that can be imported, Tkinter, for example, is awesome.


H – High-Level language

A computer programming language which has been designed to be easy to read by humans, like Python.


I – Iteration

Repeated execution of a set of statements. This can be done either with a recursive function call or a loop.
While loops or for loops, that is the question.

While loops are used if you want to have at least one result, they will usually run forever until an event is reached, iterating over the same code each time until that result is met.

For loops, on the other hand, are good if you want to repeat something a specific number of times.

For (int i = 1, i < 10, i++)
{
Do this exactly 10 times
}

J – Just-in-time (JIT) compiling

A way to dynamically compile some bits of an application during runtime. In Python you can use numba which supports CPU and GPU execution optimization, this means that for certain operations you can use your video card to process the results. JIT can make your application faster as there is much more information available at runtime.


K – Keys
Not only do keys open doors and treasure chests. They also open up some possibilities to play with hashes (Dictionaries).


L – Libraries

Import libraries and unlock the functions and tools created by people who have been doing this kind of thing a lot longer than me. This means you can use the best algorithms, contained in functions to do exactly what you want to do. Want to get access to a website and read the contents in it? There’s a function for that. The best libraries I have played about with so far are pygame and urllib.


M – Method

A method is pretty much the same as a function: it takes arguments and returns a value. If you have used something like aLittleSomething.something(), that’s a method. So you can basically invoke on something

Hey = “you lookin for a little”
Hey_you = hey.aLittleSomething.something()
print(Hey_you)

N – Not equal to

!= and all its friends the operands can do strange things if left to their own devices == is not equal to = so tread carefully. It’s a trap.


O – Objects

Something a variable can refer to. An object combines variables and functions into a single entity. Objects get their variables and functions from classes. Classes are essentially templates for creating your objects. You can think of an object as a single data structure that contains data as well as functions. Functions of objects are called methods.


P – Programming idioms

There are tried and true, accepted, best practices for coding, different for each language. This is basically the code style. For Python you can find a lot of code style guides around, the following style guide is invaluable https://www.python.org/dev/peps/pep-0008/

Pythons design can be summed up in pep 20; the Zen of Python https://www.python.org/dev/peps/pep-0020/

The Zen of Python
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one– and preferably only one –obvious way to do it.
Although that way may not be obvious at first unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea — let’s do more of those!


Q – Queues

A variable type which supports a first-in, first-out policy. So as data is added the oldest data is removed for example.


R – Refactoring

This is the act of making very small improvements to the code, things that don’t change the behavior of the code at all, but the sum of all the minor improvements adds up to a much greater improvement to the program as a whole. If you went into the working code and tried to improve it in one massive sweep, the chances are it will break the code, change the behavior or get you slapped by your team lead. Refactoring is very calculated and will make your code very clean.


S – Shell

The Python shell is known as the REPL which stands for “Read – Execute – Print – Loop”. It is basically the interactive part where you can see the outputs and errors and do basic things. If you create a GUI and forget to display message events to the GUI, the errors will be possible to see in the shell in the interpreter.


T – Testing

As I have found out, if your code only works 95% of the time, it basically doesn’t actually work. Testing the code to make sure that it does what you are expecting it to do, can be annoying, especially if you find that something actually doesn’t work. There are some tools you can use, for instance, built into Python there is unittest. Or you can just test to destruction, throwing everything at it until it breaks, then you can work out why and fix it… or call it a feature.


U – Unicode

Text is represented in files by encoding. 2 of the most popular types are ASCII and Unicode. The standard ASCII character set only supports 128 characters so it covers the English alphabet but that is all. Unicode supports up to 4 bytes for each character so it can cover about 100,000 characters, so is great for all the special characters in other languages like Chinese. Why am I saying this? 2 reasons really. 1 – I can’t find anything good to use for the letter U. And 2 – because you will likely come up against exercises which require you to capitalize characters, it is possible to hack something together where you can say -32 from the bit and it will give you the character in capital. A is 65, a is 97, 97 – 32 = 65 == A. But this won’t work for Unicode, so it is good to be aware of other problems which might come up.


V – Variables

Variables exist in different scopes. There are:

Local variables
which can only be used in the functions they are in, and

Global variables
these can be accessed by any function in the program. These variables need to be created outside of a function.

Python will let you access and read the value of a global variable. But it will not be at all happy if you then try to change the value of it. It will give you an UnboundLocalError. To be able to change it you have to make sure you tell your code that’s exactly what you want to do, by using global and then the variable in your function.
global nameOfTheVariable


W – Warnings

Warnings and exceptions are a part of the game. There are likely to be some that come up a lot.

Syntax errors – these will normally come up in the IDE – but they are things like:
Invalid syntax. An invalid syntax error means that there is a line that Python doesn’t know what to do with.

EOL or End Of Line – this error means that there’s an open quote somewhere and the line ended before it found the closing quote.
Unexpected indent – indentation matters; if something is indented at the wrong level then it won’t run properly in the function.

EOF or End Of File – this error usually happens if you forget to close the parentheses. Python reaches the end of the file looking for the closing parenthesis.

Runtime errors – these happen on running the code. Some common ones:
Name error – A NameError means that Python tried to use a variable or function name that hasn’t been defined yet. Mistyping a word usually leads to this, or not defining a variable before it is used.

Type error – a TypeError means Python tried to use a variable which is of a different type than expected. So, for example, a string to an integer.
“Raised when an operation or function is applied to an object of inappropriate type. The associated value is a string giving details about the type mismatch. Passing arguments of the wrong type (e.g. passing a list when an int is expected) should result in a TypeError, but passing arguments with the wrong value (e.g. a number outside expected boundaries) should result in a ValueError.” – https://docs.python.org/3/library/exceptions.html


X – Pretty useful as a variable


Y – Also pretty useful as a variable


Z – 90 in ASCII

 

You have a faculty for defining the simplest in terms of the grandiose, so that a poor devil like me can’t understand it. – Malcolm Bradbury