Computer Science is not the study of computers nor is it only about programming. Sometimes, when people hear about Computer Science, they think oh, computers and programming!
In reality, computers are just a tool in computer science, and programming simply executes a sequence of instructions that we create.
Computer Science is all about computation, asking the question: “What exactly can be computed, how can we compute it, and how fast we can compute it?”
Computation is a fancy way of saying any type of calculation that follows rules or list of steps.
Multiplying numbers like 1 times 2 is a computation. There are rules and steps to multiplying two numbers. Likewise, adding 15 + 5 is a computation.
Computer scientists often toss the word algorithms, and that sounds fancy, but algorithms are simply the way that we solve problems.
Algorithms are just the list of steps that we use to solve any type of problem.
The rules and list of steps that apply to addition like remember to carry your 1s to the next column if the current column adds to 10 or above is an algorithm.
When you hear computations, computations are calculations that follow algorithms.
Because Computer Science dives into computations, we also think of problem solving.
“Can we solve this problem? How hard is this problem? How fast can we make the solution?”
Problem solving use computations!
The most important skill of computer science is problem solving.
Learning a programming language like Python is an excellent way to help you practice solving problems because programs are step-by-step instructions that you create in essence to solve your problems!
If algorithms are the ways or the list of steps that we use to solve problems, programming is about taking our algorithms that we have thought and planned and writing them into programming languages like Python.
The reason that we learn programming while learning Computer Science is because programming languages are languages that express computations and also solve problems for you.
Although we’ll be using Python as our language of choice in these lectures, the language is not important.
Understanding the fundamentals of programming and practicing problem solving in your own personal objectives and context is far more important.
Practice makes perfect.
Values, expressions, and statements
Assuming that you have Python already installed on your computer, you can save an empty file as a Python file by making sure that it has a .py extension.
Python has an IDE, integrated development environment, which serves as a program that can run a Python program. I’m using Python 3.6 in this book.
The first thing about Python that we’ll talk about are values. Values are letters or numbers that can be used in the language.
Every value has a type. Numbers like 1 and 2 have the type called: integers.
Words or sentences like “Hello world!” have the type called: strings.
Strings are enclosed in quotation marks.
The next thing that we’ll talk about are functions. Functions are fancy terms defining commands that do something in Python.
The most used function in Python is the: print function. The print function displays values on the screen.
To use print, you write, print and enclose a value inside a pair of parentheses.
You can run the program. What’s displayed is 4 and Hello world!:
You can also check the type of your value with the type function. Python has a bunch of these preconfigured functions like print() and type().
type(value) gives us the type of the value enclosed in the parentheses.
You can put functions within functions. We’ll put the type function inside the print function, so we can tell what type the 4 and “Hello world!” are.
What’s displayed is int, which is short for integer, and str, which is short for string because 4 is of type, integer, and “Hello world” is of type, string.
Now, let’s talk about variables. Variables give a name to our values. We can assign new variables with values. The variable name must be on the left side, and the values must be on the right side.
question = "How are you today?"
x = 5
y = 2.5
question is now the equivalent to the string, “How are you today?” because of our assignment. Likewise, x is the equivalent to 5.
The print and type functions work with variables, so we can use print and type on these variables.
What’s displayed is:
float is the type given when there is a decimal point dividing the integer and fractional part. 2 and a half has a decimal, so it’s a float type.
Variable names can use a combination of letters and numbers, and variable names are case sensitive.
When you use multiple word variable names, typically, you leave an underscore between each word like:
python_tutorial_question = "What can be computed?"
There are illegal characters that you cannot use in variable names like the $ symbol.
cs101$ = "Computer Science 101"
There are keywords in Python. You cannot give a variable the same name as a keyword.
class = "Computer Science 101"
There are twenty-nine keywords in Python:
and def exec if not return
assert del finally import or try
break elif for in pass while
class else from is print yield
continue except global lambda raise
You can still name variables that contain keywords, but they cannot be exactly the name of a keyword.
python_class = "Computer Science 101"
class is a keyword, but since it’s used inside a bigger word, the variable name is legal.
Next thing that we’ll talk about are statements. A statement is a fancy way of saying a line that executes in Python.
print(4) was a statement
x=5 was a statement
Next thing that we’ll talk about are expressions. Anything that evaluates something is called an expression.
1 + 1 is an expression.
We can print these expressions.
1 is an expression.
variables are expressions.
We can print all of those.
Next thing that we’ll talk about are operators. Operators are special symbols like addition and multiplication that perform a computation.
You can do simple math like 1 + 2.
3 * 4. The asterisk symbol is used for multiplication.
Two asterisk symbols are used for exponents. 2 ** 2.
Operators in Python follow the order of operations, which you may remember as parentheses takes priority, and then multiplication and division, and then addition and subtraction.
For instance, you can write (1 + 2) * 3, and the result would be 9 because of order of operations. 1 + 2 happens first, which equals to 3. Then, 3 is multiplied by 3 = 9.
You can use the addition operator on strings to combine strings together. For example,
topping1 = "pepperoni"
topping2 = "mushrooms"
pizza = topping1 + " " + topping2 + " pizza"
The next thing we’ll talk about are comments.
When programs get more complicated, developers leave comments that are basically notes, so that you or future developers understand what you did.
Comments in Python are prefixed by the # (pound) symbol.
# Number of seconds
seconds = 1800
# Number of seconds translated to minutes.
minutes = seconds / 60
# (pound) symbol is used for single line comments.
For multi-line comments, where you need to include extra notes, you use a pair of triple quotation marks, one set to start the comment and one set to end the comment.
is a multi-line