Computer Science is fundamentally a creative discipline. As computer scientists we solve problems from a variety of real-world domains and we create new tools and new knowledge about the world. To do so we abstract away the specifics of a problem and design general solutions that are critical to many other fields.
We solve problems, develop algorithms, and make our solutions realizable using computers. This requires computer science to further include programming languages, hardware, operating systems, theory, and many application areas.
What do we use computers for, especially in the sciences? When are computers the right tool for the job?
At the simplest, computers are the right tool when we need to do something more than once. Often, many, many, times, and we want to do so efficiently. To “do tasks many times, efficiently” depends on a range of innovations and knowledge from applied math, to electrical engineering, to psychology, and everything in between.
In this course, we will get exposure to many facets of CS, with the concurrent goals of introducing you to CS as a discipline and developing your problem solving and programming skills.
An algorithm is a way of accomplishing a task; informally an algorithm is like a recipe.
More formally:
Syntax vs. Semantics…
It is always tempting to jump right into coding, that is, jump right to syntax. We advocate you resist that impulse, and really think through your algorithm and the semantics of its implementation first.
Example: What is an algorithm for adding up the numbers from 1 to 10?
Spend 2 minutes discussing strategy with a neighbor. Write down your strategy as a series of steps.
In our strategies, we performed a few different kinds of operations:
It turns out that remembering values, performing arithmetic, repeating steps, and checking conditions are extremely common patterns in solving problems.
One of the primary goals of this course is for you to be able to break down big problems into collections of these patterns.
We will be using Python 3, a widely-used, widely-available, general-purpose language.
We will be using the Thonny integrated development environment (IDE).
Today we will be working in the bottom (or right) box, the “Shell”. This gives us access to Python’s REPL (read-eval-print-loop) also called an interpreter.
The >>>
is the prompt for us to enter Python code.
Let’s try some basic expressions, hitting enter after each:
>>> 1
1
>>> 5
5
These are “literal” expressions, because the resulting value is the same as the expression.
Now some more complex expressions that include “operators”:
>>> 2 + 2
4
>>> 15 * 20
300
>>> 20 / 4
5.0
>>> 10 - 20
-10
>>> 15 // 4
3
>>> -15 // 4
-4
The operator //
is floor division: the result of the division is rounded down
to the next integer.
When we hit “enter”, Python is evaluating the expression (the “eval” in REPL) and displaying (or “printing”) the results.
What about errors?
>>> 2 asdf 2
File "<pyshell>", line 1
2 asdf 2
^
SyntaxError: invalid syntax
We didn’t follow the syntactic rules of the language and so Python reported an error. Python has no idea what “asdf” means, it is not an operator built-in to the language.
Helpfully, Python does tell us where it found the error, but it doesn’t tell us how to fix it (because it can’t read our minds to know what we wanted it to do). Often the error messages will seem a little opaque, one of the skills you will develop over time is the mapping of errors to error messages.
Earlier we saw 5
and 5.0
. What is the difference? They have different
types. 5.0
is a floating point value or float, 5
is an integer value or int.
A type consists of two things:
Integers are exactly what they sound like, while floating point numbers are a subset of the Real numbers. Both are necessarily finite subsets of their domains, that is, the computer can’t represent all real numbers at infinite precision. We will learn a bit about numerical precision at the end of the semester.
Let’s think about non-numeric types
>>> "hello"
'hello'
We term "hello"
a string literal because it is a string (sequence) of characters
whose value is the characters themselves, i.e., we are literally and exactly
specifying the value.
We can apply certain of these operators on strings, for example
>>> "hello" + "goodbye"
'hellogoodbye'
but what about:
>>> "hello" + 2
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
TypeError: must be str, not int
Python is telling us that adding an integer to a string isn’t a supported
operation of string types. Concatenation of strings with +
is supported (we
just saw that) and Python does know how to convert integers to strings. But
Python won’t do so implicitly as it can’t be sure that is what you intended.
You need to explicitly convert the integer to a string with str
.
>>> "hello" + str(2)
'hello2'
Everything we have done so far was very ephemeral. We gain tremendous power by being able to save and use intermediate results.
Assignment syntax: <variable> = <expression>
Example:
>>> x = 7
Nothing was printed by the REPL. What happened? To figure it out, let’s build up our mental model of what is going on in the Python interpreter.
Define assignment semantics:
Python Tutor Memory Model Picture
Brief introduction Peer instruction questions (proven technique for improving learning!):
Peer instruction questions (Section A, Section B)
See syllabus for reading, practice problems, prelab, and lab.
The course website go/cs150 has the official syllabus as well as information on office hours, books, schedule, grading, etc. The site will be updated daily with new material.
Please note:
We will typically have regular classes on Mondays and Wednesdays, during which we will combine lecture, peer instruction via Socrative, and paper-based practice problems. Problem solving on paper will help you build accurate mental models of what the computer is doing when running a program; they will also help you prepare for the exams.
Fridays will typically be lab days, and will start with a short quiz on the material we covered during the week. The quizzes are intended to provide a low-stakes opportunity to check whether you understand the material. We’ll use the remaining time on lab days to work on and answer questions about the next programming assignment.
Our first lab is on Friday. Read through the entire Lab 1 write-up. Make sure to the complete the prelab beforehand.