1. This is a quirky guide that we made to be a refresher on the very Basics of Computer Programming
in case you forget how to program. This isn't a language specific guide (meaning we are not using
python code); rather, this guide is all about the concepts. If you want to re-learn more advanced
topics like Objects or review with Python Syntax, check out Codecademy.
Learnin'
What is Computer Science?
"What is computer science? Computer Science is here. It's the science wherever people
learn to compute things. So look around, you've finally found, the thing you asked
about! For computer science is your science if you're lazy, problem-addicted, and in CSC
club" - Urinetown the *hand flourish* MUSICAL
Computer Science is the scientific approach to computation (how do we compute efficiently andq
effectively)
It’s how we make computers work for us; software is more important in coding than hardwareq
It helps us solve problems like creating a video game or running a businessq
What’s a program?
A program is nothing more than a recipe in a language that the computer can understandq
What needs a recipe? Well, basically anything you want to create or do! >Examples: make PB&J,q
eat pizza, go running, twitter
Programs contain several constructs called: variables, statements, methods, functions, operations,q
and objects
Variables
These variables are the same as the ones in maths class except these can have longer names (xq
vs tableHeight) and can also hold things other than numbers (greeting = "hello everyone!")
2. A “place-holder,” or an indicator of a value that can change. This placeholder can changeq
throughout a program or stay the same
Variables add ambiguity to the program that makes it applicable for a wide amount of scenariosq
that include user input, etc.
Variables can be named in a variety of ways. We will go over these naming conventions later on.q
Statements
Statements are the smallest standalone elements of a program that the computer canq
understand
A one line set of instructions, such as get the peanut butter or x = 5q
Also can be thought of as the small bits of a set of instructionsq
Functions and Methods
Let's say we type a set of statements or instructions into the computer to complete a task, and that
task is made of smaller tasks and each of those tasks made of even smaller tasks, the ones that the
computer can understand by itself (Computers are rather dumb, so you have to be really explicit
with your instructions). Now what if you need to preform one of those medium sized tasks over and
over again under slightly different circumstances?
Example: You want to make a penut butter and jelly sandwich as part of your life
emulating program, what are the steps?
get out the penut butter1.
get some bread2.
get the jelly3.
a knife maybe?4.
make the sandwich5.
Those are some pretty good instructions for a human, but what about a computer? Ignoring that
computers don't speak English, these instructions probably aren't specific enough for the dummy
computer to complete the task. So we need to break them down further. Let's look at step 5.
Step 5, in depth:
get two slices of brand (that's a variable) bread1.
lay the bread on the plate2.
open jar of peanutButterKind peanut butter3.
if you want jelly open jar of jellyKind IsJelly (in case it is actually jam or4.
something... Jelly & Jam.. Is there a difference?)
pick up the knife5.
plunge knife into peanutButterKind peanut butter6.
...7.
3. Let's say that these are simple enough instructions for the computer to understand. Notice how I
have several variables that I am using to make the program more generalized for use to make a
variety of peanut butter and jelly sandwiches. I even threw in an if-statement (we will go over those
later). Now, what if we want to use this piece of code somewhere else in our program of life? Copy
and paste right? WRONG. If I copy and paste the code 12 times and all of a sudden I find a bug or
something I want to change, what do I do? I would have to change every single copy. That's why we
have methods and functions.
Methods and functions give a name to a series of steps that you can then run anywhere else in the
program. This means you can have one copy of the code that updates every instance that you have
when you want to change/fix something. You can think of methods and functions as variables for
processes. Methods and functions are similar, but also very different.. Let's look at each of them.
Methods vs. Functions
There are more intricate differences between functions and methods, but the main difference is
that methods do not return data and functions do. Functions work just like functions from maths
class except these functions, like variables, can have word names:
f(x) = 2x
double(number) = 2*number
Functions give you something when they are done with their process just like this function givesq
us the answer to 2*x depending on what x is.Example:double(2) returns a value of 4If I said
twoDoubled = double(2) then then twoDoubled would be the returned value 4
Methods do not return values, they simply run the instructions and the program moves on fromq
there. Methods do not have a simple math analogue that you would be familiar with; however,
you can consider your solving a math problem sort of like a method. The process you take to
solve a math problem doesn't need to return a value because you recorded it on the paper.
We can even take our example and make it even more generalized, so we can reuse that code to
make any kind of sandwich. But we will need parameters...
Parameters
Parameters are values that get passed into a function or method. In our example above number is a
parameter of the function double(), it is the value that gets passed into the function to help
compute the function.
Example (another one) :
//Gets the roots of the quadratic equation Ax^2+Bx+C
/*
BTW: These are called comments. They allow me to explain or
4. take notes of what my code is doing without being a part of
the process. Different languages have different ways of
indicating where comments are, but this-- two forward
slashes for single lined comments and a pair of forward
slash asterisk pairs for blocked or multi-lined comments--
is a common convention languages have.
*/
getQuadraticRoots(coefficientA, coefficientB, coefficientC){
/*
because making this function would involve doing things
that we haven't learned yet, I am just going to leave it
with the parameters and returned value(s).
*/
return {solution1, solution2}
}
Data Types
Data Types are the kinds of values that a given variable contains. There are several different kinds
depending on what you want to do with your variable (The ability to choose can make your
program much more efficient, computationally and spatially.) Some programming languages make
you define what data type or kind of variable you are working with when you make the variable;
others-- python is one of these-- are able to figure out what data type your variable is based on
what value you give it. The ones I am going to lay out for you-- there certainly are more than these,
and you can even make your own later on-- are integers, doubles, floats, longs, and booleans.
Integers
Integers are, well, integers! This includes whole numbers, positive, negative, and zero. Integers are
going to be one of the fastest options if you are storing or manipulating values that are, well,
integers! Be careful dividing them as you won't get a decimal no matter how hard you try.
Ex:
int number = 5
Doubles
Doubles are your rational numbers, for the most part. They have pretty darn high decimal precision.
Most operations can be done with doubles just don't try to evaluate the radius of the observable
universe.
Ex:
5. Good Idea
double numberOfJumpingJacksBenCanDo = 4.21565
Bad Idea
double piBenHasMemorized = 3.1415926535897932384626433832
795028841971693993751058209749445923078164062862089986280
348253421170679821480865132306647093844550982231725359408
12848
Floats
Floats or floating point numbers are doubles with less space and, therefore, less precision. They
are still good for a lot of tasks and certain tasks see improved efficiency with these numbers. I
usually go for doubles over floats by instinct; however, they do have a lovely place made for them.
Longs
Longs are really, well, long integers for the times where you have to count how many grains of sand
on a beach. (Okay, maybe not that much)
"So why would I ever use an Integer variable if longs can be, well, longer?"
Longs, like all of these data types, take up a certain amount of space no matter how big the number
is. A long with the value 5 will take up the same amount of space on the computer as a long with a
value that justifies the use of a long, like 2147483647. Longs will also take long-er to run calculations
on and thus less efficient.
Booleans
Booleans (not bullyans. You are safe on this side of the playground) are true/false values. They are
usually the smallest of the primitive data types in any language because they can be represented
with a 1 or 0.
Example:
bool mattShoweredToday = false (When does he ever shower?)
Strings
"So all these numbers things-" "Data types..." "Yeah these data types, they are pretty
great n' all, but sometimes I don't want to speak maths... You can use, ya know, words
6. n' all right?"
Strings are the data types that can store a list of characters-- characters are another data type you
will eventually be familiar with; they are individual letters or symbols-- that one can use to display a
message to a user or have an error message or even make the computer talk to you in a
roundabout kind of way. Strings are defined with quotation marks. You can combine strings and
search through them and all sorts of wacky stuff.
Example:
String favoriteAnimal = "turtle"
The Limits of these Data Types
The aforementioned primitive data types have limits. You probably have gotten that gist from the
differences between floats and doubles. All of these types have a set maximum size that they can
be and when you try to go past that size you will get an error, or perhaps, if numerical, it will go
negative or some other strange thing. Longs obviously can be much bigger than an integer;
however, they are much slower in computation and take up a large amount of space no matter how
big the number is. As we progress, you will learn more about these limits, why they exist, and what
you can do to get around them with other types like long long, long double, signed vs unsigned, or
making your own.
Summary (tl;dr)
Variables are a place-holder for changing valuesq
Statements are the smallest standalone instructionq
Methods are reusable blocks of code that you can jump to within your programq
Functions are reusable blocks of code that you can jump to within your program that return someq
sort of value
Parameters are the values that you pass into a function or methodq
Data Types are the kinds of values that a variable can store. They include integers, doubles, floats,q
Strings, booleans, and more-- maybe even some that you make yourself.
Integers are integersr
Doubles are rational numbersr
Floats are smaller, less precise doublesr
Longs are long-er integersr
Strings: for those times you want to use wordsr
Booleans are true and false valuesr