Intro to Computer Science — In 5 Minutes (with python)

There is a ton of complexity involved in computer science, but as programmers, the number one tool we have at our disposal is: abstraction. Abstraction is what let’s us communicate with the computer in a way that not only allows it to perform it’s job, but allows us to understand the code we have written and, therefore, perform ours, as well.

Back in the day, computer programming involved a lot of explicit instructions: creating a slot in memory, adding some bytes there, executing some instruction at some line. This was true for various low-level languages from the early 50s and 60s such as Fortran, and remains true for assembly language today. However, with the advent of high-level scripting languages, such as Python, it is no longer necessary to deal with the computer at that level of detail. Instead we can tell it much more declaratively what to do, and the computer will figure out the how part all on its own. What we’ve accomplished in doing this is abstraction. We will go into abstraction and how this works, but first, let’s cover some basic fundamental ground by outlining how to use the tools python provides.

Python Fundamentals

At bottom, every line, statement, and expression in a programming language can be reduced to some sort of primitive value. In some languages these are referred to as primitives, but python calls them values. There are 3 basic value types to start with:

  1. Integers — whole numbers, including negative numbers and zero (-1, 2, 0, 4…)
  2. Floating-point numbers — Decimals (3.14, -2.6…)
  3. Strings — Words, sentences, indicated by quotes “” (“dog”, “cat” “dog and cat” …). Ultimately these boil down to numbers as well, with each character in the string having a corresponding character code that represents a particular letter.

These are the raw materials we get to work with, and not even think at all about their underlying representations. Instead, we just say 42, and it will behave as we would expect an integer to, allowing for all the basic arithmetic expressions that you know from primary school. These are called operators, and in Python they are:

  1. + — plus, add two numbers, add two strings, but you can’t add a string to a number…
  2. - … minus (e.g. 3–2 = 1)
  3. * — multiplication (2*2=4)
  4. / — division (e.g. 4/2 = 2)
  5. % — modulus, gives the remainder of division (e.g. 3 % 2 = 1)

As a programmer, you always need some way to add numbers, to divide them, etc. But with operators, you have the full power of mathematics available to you, should you have the knowledge to apply it.

This is all well and good, but as a human being, I can’t really keep track of everything I’ve written or calculated within a computer program. Luckily, python has provided for us an extremely simple means of saving a value in memory so that we can come back to it later. This is done by declaring, like:

For example,

From now on, I can access the value “two” by referencing the variable “a”.

One more thing: say we’ve done a bunch of calculations solving the riddle of the universe, but doh! We can’t get at the result! How can we ask the computer to tell us the result of whatever we’ve calculated? We can do this by using the print() function, which will take advantage of whatever native input-output (io) interface is available to it in order to display whatever is passed in as an argument.

For example,

Holy smokes! You now are a programmer, and you’ve even written your first program. That code above represents a program to print the number 2. Notice we’ve only used a couple words which have intuitive meaning to us, like “print”, and “something equals something,” and yet we’ve managed to generate a very orderly display of pixels on our screen representing the number 2. Believe it or not, programming does not get much more complicated than this. We are always using familiar language to tell the computer to perform actions, and the computer will carry them out for us. There is usually no need to know anything about how a computer works to be a good programmer. Again, this is the power of abstraction manifest.

Abstraction

So we’ve been talking about abstraction in a very “abstract” way. Let’s get more concrete with some examples.

Let’s say we are writing a program to choreograph sequences of jiu jitsu moves. One way we could do this is write them out.

We could define the first move in a sequence using a series of statements comprising the properties of the move:

move_1_name = “tuck and roll”move_1_difficulty = 1move_1_order = 1 // when does the move occur in the sequence?Then the next move:move_2_name = “choke grip”move_2_difficulty = “5”move_2_order = 2

This works, but what if I want to do something more complicated, like talk about both moves together, as a sequence? Then I have to refer to each of the variables I’ve named individually, remembering the names I declared them with and referencing them. Instead, wouldn’t it be better to have a concept of a “move” and a “sequence”? Then, I wouldn’t be thinking about a move as a bunch of disparate properties, but instead as a holistic set. This higher-order “collection” is sometimes referred to as an object, but in python it is referred to as a data-type.

Data-types are collections of values. The one I want to introduce to you for the purposes of this lesson (and this is the last new concept that I’ll introduce for now), is the Dictionary. A dictionary is just a collection of key-value pairs, where the key is a string (in other words, a name), and the value is some value. It can be a literal value like an integer or a string. It can even be another dictionary. But let’s not worry about that for now.

How would we go about modeling a jiu-jitsu move? We can say:

move_1 = {
"name": "tuck and roll",
"difficulty": 1,
"sequence_order": 1
}
move_2 = {
"name": "choke grip",
"difficulty": 5,
"sequence_order": 1
}

Notice that the keys and values are separated by colons, and entries are separated by commas. Meanwhile, the curly brackets are what we use to declare the dictionary. Then we are storing that dictionary as move_1. You’ll get used to this syntax, and it’s very common.

Now I can access a property on that object like so:

move_1_name = move_1["name"]print(move_1_name) //prints "tuck and roll"

In doing this, we’ve consolidated our knowledge about various moves in a coherent structure. This lessens the burden on us to remember whatever details we might have included about particular moves and where we saved that information. Instead, it is all housed at one location.

To do a Sequence, we would need another data structure provided by Python: the List. I promised I would introduce only one more concept but I think we can go ahead and hammer this out here. Instead of curly brackets, lists use square ones:

my_sequence = [move_1, move_2]

Boom. We now have an ordered list of moves representing our combination. Lists are accessed similar to dictionaries, but instead of having keys for accessing particular properties, they have indices: integers representing the position of the item to be accessed. That looks like this:

move_1 = my_sequence[0] // accessing the first move in the sequenceprint(move_1) //prints: {
"name": "tuck and roll",
"difficulty": 1,
"sequence_order": 1
}

Notice array indexing starts at 0, as with all programming languages that I know of.

Alright, we’re done for now. Hope you followed along and enjoyed that. And hopefully you understand more the concept of abstraction, and how at bottom it really just makes things easier for us.

References

  1. Automate the Boring Stuff with Python — https://automatetheboringstuff.com/#toc

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store