Computer Science for Everyone — Part II.

This is a continuation of my series on Computer Science for Everyone, which began here. Credit goes to Wikipedia for all quotes in this article.

A collage of words to describe Computer Science. Image Credit.

Let’s get right into it — the way a Computer Scientist thinks is with a programming language. And he/she uses that programming language to solve problems in a systematic way. We’ll cover the basics that apply generally to programming languages, but of course we must choose one to demonstrate examples with. The language of today is Python (which if you know me, is currently my favorite programming language to use).

In my opinion, there are 3 aspects of programming languages that apply across the board. They are variables, data structures, and control flow. Understanding these constructs allows computer scientists (or basically anyone that writes code) to switch between programming languages without much effort.

Variables

In computer programming, a variable or scalar is a storage location paired with an associated symbolic name (an identifier), which contains some known or unknown quantity of information referred to as a value.

An example of a variable in Python is the following:

a = 3

This takes the value 3 and stores it in its symbolic name, which in this example is the letter a. From here on, the value 3 can be accessed just by entering a in the console.

Creating a variable in Python.

When you assign a variable, a right-to-left association is made. That is, the value on the right is stored in the symbolic name on the left.

Variable assignment of 3 to the letter a.

You’re not just limited to storing numbers. You can store all kinds of data types, such as decimals, character strings, or data structures.

Data Structures

In computer science, a data structure is a particular way of organizing data in a computer so that it can be used efficiently.

In order to do useful computation with the values stored at variables, we use data structures in order to organize data in a way that’s fast and easy to use. For example, you could create a variable for every item in your shopping list. That would work, but it would be tedious to create an individual variable for every item, never mind keeping track of the symbolic names of those variables further on in your program. An easier way to represent a shopping list would be with a data structure called an array, or list, in Python terms.

Example of a list in Python.

Lists start out indexed at (counting from) zero, meaning 0 is the slot of the first element, 1 for the second, and so on. In Python, you can also access lists in reverse order using negative numbers. In order to access element N in the list, you could access index -1, and to access element O, you could access index -2, and so on. Lists allow you to store several variables and access them by an index, which is easier than keeping track of several variables in different symbolic names.

You create a list in Python like this:

python_list = [‘P’, ‘Y’, ‘T’, ‘H’, ‘O’, ‘N’]

You access the variable ‘P’ as follows:

a = python_list[0]

The variable a now contains the value ‘P’. You can see how using a compact data structure makes dealing with data easier — the data is all in one place. Lists are just a simple example of many common data structures that computer scientists use in order to efficiently manage data. Other popular examples include dictionaries, trees and graphs.

Control Flow

In computer science, control flow (or flow of control) is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated.

You can think of control flow as the logic that runs your application. For example, if you have eggs, then eat them, otherwise, print that you’re out of eggs. The if-then clause of that statement can be represented using an if-else statement in Python.

if eggs > 0:
print('I have eggs')
else:
print('I don't have eggs! To the store NOW!')

The first line, if eggs > 0, tests the variable eggs using the greater than relational operator (‘>’). The statement is true if the value stored in the variable eggs is greater than zero. In that case, the program will print ‘I have eggs’ to the console. The else statement is executed only if the matching if statement is false. In this case, the else statement will execute if the value stored at eggs is less than or equal to (‘’) zero.

The logical flow of an application can be modeled using a flowchart. A flowchart is a diagram that represents the possible outcomes of a program that depends on control flow. An example flowchart modeled after our eggs program is shown below:

Flowchart example.

With knowledge of variables, data structures and control flow under your belt, you can get lots of interesting work done with a computer. However, the most interesting aspect of being a computer scientist is verifying the speed and correctness of our programs, which will come up next. That means math! Who doesn’t love a bit of good ‘ol CS math?

Thanks for reading! I’ll be heading out to WWDC this upcoming week, so that means the next (and last) installment will be a bit delayed — sorry about that. Otherwise, thanks for sticking around!

--

--

Software Engineer publishing one thought at time.

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