Skip to main content

Introduction

Programming is the process of implementing logic to create a program which usually solves a problem. It is a useful skill because we are able to automate repetitive and mundane tasks and focus on the more important tasks which require human intelligence.

How does a Computer Work?

The most basic hardware used for computation are...

CPU (Central Processing Unit):
It is the brains of a computer which runs a set of instructions from a program. It can only read in machine learning which is in binary (0s and 1s).

RAM (Random Access Memory):
It is the short term memory of a computer which stores data and instructions temporarily. It is volatile which means that the data is lost when the computer is turned off.

Storage (Hard Drive, SSD, etc):
This is long term memory which stores data permanently. It is non-volatile which means that the data is not lost when the computer is turned off.

Hardware Interaction
Fig. 1 - Hardware Interaction

When you are running a program, it is loaded into the RAM from the storage and then the RAM sends instructions to the CPU to run the computations.

Usually the program also interacts with IO (Input/Output) devices such as keyboards and monitors. Programs can also interact with other computers over a network.

Machine Language

Like stated before, the CPU reads in machine language which is in binary. However we do not code in binary as it would take forever to create the applications we use. We consider machine learning a low level programming language because it is very close to the hardware. We primary code using high level programming languages such as Python, Java, C++, etc. These languages are easier to read and write and are translated into machine language by a compiler or interpreter.

Compiler:
Is a program which translates the high level language into machine language and then creates an executable file which can be run by the CPU. Popular languages which use compilers are C, C++, Java, etc.

Interpreter:
Is a program which translates the high level language into machine language but runs the program line by line. Popular languages which use interpreters are Python, Ruby, etc.

With compliers the program needs to be written completely before it can be run. With interpreters the program can be run line by line as it is being written.

Getting Started with Python

Python is a programming language that is widely used. Common applications of python include data science and machine learning but it is a general purpose language which can be used many applications.

Python Logo
Fig. 2 - Python Logo

Installation

You can write code line by line directly in the interpreter and it will run you code line by line because Python is an interpreted language. However, for complex programs it is better to write code in a text editor. You can use an IDE (Integrated Development Environment) such as PyCharm or Visual Studio Code which have a text editor but also other features useful for programming.

The python interpreter can be downloaded from here.

You can download VS Code from here.

Writing Code

When a computer reads through a program, it runs the code line by line sequentially unless there is a control flow statement which will be shown later. Each line of code is called a statement which is like a sentence in English. There is a specific way to write a statement and we call this the syntax of the language. Finally, the order of statements matter and we call this the semantics of the language.

The most basic statement is the print statement which prints out a message to the console. A console is a text based interface that interacts with the computer.

Prints Hello World to the console
print("Hello World!") # 1

The syntax is print("message").

Running

You can run python code using through the terminal, an IDE, or the python interpreter.

Terminal:
Replace the filename with the name of your file and run the following command in the terminal.

$ python3 filename.py
caution

Do not write $ in the terminal. It is just there to show that it is a command.

IDE:
Open the file in the IDE and there is usually a button to run the code. On VS Code, you can press F5 to run the code.

Interpreter:
You can write code line by line in the python interpreter and it will run it immediately as you finish typing the line.