Lecture 1

Course Administration

  1. Course webpage go.middlebury.edu/cs150
  2. Bring a computer to class each day
  3. Regular classes usually Mondays and Wednesdays, quizzes and labs usually on Fridays
  4. Prelabs due at the beginning of lab day; weekly labs due the following week
  5. One written midterm, one take-home programming exam, weekly quizzes, weekly labs, and a final exam
  6. Assistants in Science Instruction are Catherine Miller and Robert Lichenstein; both have afternoon office hours
  7. Peer tutoring in 75 SHS 224 Sunday through Thursday evening each week

Objectives for today

  1. Define algorithm, syntax, and semantics
  2. Describe the purpose of a variable and perform variable assignment
  3. Evaluate arithmetic expressions over integers and floats
  4. Describe the concept of type

What is Computer Science?

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.

Why CS 150?

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.

Algorithmic thinking

An algorithm is a way of accomplishing a task; informally an algorithm is like a recipe.

More formally:

  1. Finite number of instructions or steps
  2. Each instruction is well-defined (not ambiguous)
  3. Eventually halts
  4. Solves a general class of problems

Problem solving

  1. Explicitly define problem to be solved (inputs, outputs, etc.)
  2. Develop an algorithm to solve the problem
  3. Implement that algorithm as a program

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.

Python

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.

Python Types

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:

  1. A set of values, and
  2. A set of operations that can be applied to those values.

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'

Assignment to variables

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:

  1. Evaluate the right-hand side (RHS) of the expression (all expressions in Python produce a value, and thus have a type)
  2. Store the resulting value in memory
  3. Set the variable name on the left-hand side (LHS) to point to the memory location with result

Python Tutor Memory Model Picture

Brief introduction Peer instruction questions (proven technique for improving learning!):

  1. Solo vote: Think for yourself and select answer
  2. Discuss: Analyze problem in teams
    • Practice analyzing, talking about challenging concepts
    • Reach consensus
    • If you have questions, raise your hand and I will visit
  3. Group vote: Everyone in group votes
    • You must all vote your consensus answer
  4. Class wide discussion: Led by YOU (students)
    • Tell us how your group thought about the problem
    • Explain why the wrong answers are wrong

Peer instruction questions (Section A, Section B)

Summary

  1. Expressions
  2. Evaluation
  3. Variables
  4. Assignment

See syllabus for reading, practice problems, prelab, and lab.

Course administration wrap-up

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:

  1. How to get help: Office hours, ASIs Catherine Miller and Robert Lichenstein, and peer tutoring. Keep an eye out for lab FAQ entries.
  2. Honor code: all work must be your own. Read the policy for this course and ask if you have any questions.

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.

Reminder

Our first lab is on Friday. Read through the entire Lab 1 write-up. Make sure to the complete the prelab beforehand.