Python Basics 1 - Introduction to Computation
Table of contents
Hey everyone 👋🏼
The Pythonic Way is a series that will teach you Python right from a basic understanding of computers to the introduction of sorting algorithms. So let us start the series.
What does a Computer do?
A computer performs calculations and does billions of them per second and store the results in its large storage that has the capacity to hold 1.5M books.
But without a good algorithmic design a computer will be very inefficient in solving problems. Good algorithm design is needed to accomplish a task. Even with the development of modern computers, computers still have their limitations.
For example, some problems are still too complex to compute:
- Accurate weather prediction
Some problems are fundamentally impossible to compute
◦ Predicting whether a piece of code will always halt with an answer for any input
What is an Algorithm?
A question arises if we want a computer to computer something what knowledge computer is gonna use to perform that computation?
All knowledge can be thought of as declarative or imperative.
Declarative knowledge refers to statements of fact. For instance,
- There are cookies in the jar. 🍪
Imperative knowledge is more like a recipe for how to find or figure out a fact. It’s a sequence of how to steps to get things done.
Go to the kitchen.
Stand next to the counter.
Open up the shelf.
Look inside the jar.
Computers often work with imperative knowledge to help us solve problems.
One such numerical example of a recipe would be to find square roots by method of Heron of Alexandria
Heron of Alexandria was the first to document a way to compute the square root of a number. His method for finding the square root of a number, call it x, can be summarised as :
Start with a guess g
If g*g is close enough to x, stop and say that g is the answer
Otherwise create a new guess by averaging g and x/g i.e, (g + x/g)/2
Using a new guess, which we again call g, repeat the process until g*g is close enough to x
Consider, for example, finding the square root of 25
Set g to some arbitrary value, e.g 3
We decide that 3*3 = 9 is not close enough to 25
Set g to (3 + 25/3)/2 = 5.67
We decide 5.67*5.67 = 32.15 is still not close enough to 25
We set g to (5.67 + 25/5.67)/2 = 5.04
We decide that 5.04 * 5.04 = 25.4 is close enough so we can declare 5.04 to be an adequate approximation to the square root of 25.
An algorithm is a set of instructions that achieves a result (like the recipe for finding cookies in a jar). All algorithms have:
A sequence of simple steps.
A flow of control process that specifies when each step is executed.
A means of determining when to stop.
Computer as a machine
Does a question arise how to capture a recipe/algorithm in a mechanical process?
There were two choices here :
The first one was to use what was called a fixed program computer, and which would be a computer designed specifically to calculate a particular computation.
One such example of this kind of computer is Alan Turing Bombe which was used to crack German’s Enigma machine code in World War 2.
Another one which is a better alternative to fixed program computer is A stored program computer is a computer that is designed to load and calculate different programs (like a regular laptop computer which can do many different things).
Computers are generally made of three things
Memory, to store both data and instructions for computing things.
Arithmetic Logic Unit (ALU), which computes simple calculations with data stored in the computer’s memory.
Control Unit, which keeps track of what operation the ALU should be performing at a specific moment in time (using a program counter).
Alan Turing (a great computer scientist) demonstrated that one could compute anything they wanted using a few simple operations (move left, move right, scan, print, erase, and do nothing).
He also demonstrated that anything which could be computed in one programming language could also be computed in another and this is a property called Turing complete. It's one of the fundamentals of computer science.