Lecture 1

Course Administration: Part 1

The online syllabus has information on office hours, text book, schedule, grading, etc. The website is the “official” source for the syllabus.

Please note:

  1. How to get help: Office hours, and tutoring
  2. Honor code

We will have two assignments, one due on Wednesday at the beginning of class, one due on Saturday morning. We will also have an in-class quiz at the beginning of the Wednesday session (that is similar to the weekly quizzes in CSCI150).

Why are we here?

Presumably you are taking this class for a variety of (non-exclusive) reasons:

  1. Interest in CS major
  2. Develop programming skills for use in another domain

These are not in opposition.

Why CS?

What do you we use computers for, especially in the sciences? Why pursue CS at all? Or alternately, 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. Or maybe more realistically…

Credit: XKCD

At some level, we will learn to do tasks many times, perhaps many, many times, and do so efficiently!

Such a view may seem to trivialize CS, but actually, what I find so interesting about CS is that to “do tasks many time, efficiently” depends on a range of innovations and knowledge from applied math to psychology. And is in turn critical to many other fields.

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.

What is CS?

My answer: “Algorithmic thinking/problem solving (usually involving computers).”

But what is an algorithm? Informally 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 the syntax. I advocate you resist that impulse, and really think through your algorithm and the semantics of its implementation first.

Python (our main, but not only, language in this class)

We will be using Python 3, a widely-used, widely-available, general-purpose language. Note that I said Python 3 (not 2), Python 3 is not backward compatible. Lets get started!

We will be using the Thonny integrated development environment (IDE).

Today we will be working in the bottom (or in my setup, the right) box, the “Shell”. This gives up 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

When we hit “enter”, Python is evaluating the expression (the eval in the REPL) and 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 error messages to errors.

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 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. Note, you would learn much more about numerical precision at the end of CS150.

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 "<stdin>", line 1, in <module>
TypeError: Can't convert 'int' object to str implicitly

Python is telling us that adding an integer to a string isn’t a supported operation of string types.

But that isn’t exactly what Python is reporting. Adding, or concatenating strings is defined, and Python is trying to convert the integer 2 to a string, but can’t. Note, Python does know how to convert integers to strings, but 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.

>>> "hello" + str(2)
'hello2'

Assignment

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, lets build up our memory 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 resulting value in memory
  3. Set the variable name on the left-hand side (LHS) to point to memory location with result

Python Tutor Memory Model Picture

For simplicity we will often make the “arrows” implicit and just represent the memory as (the simplified memory model):

x: 7

PI questions (don’t use the interpreter, we are trying to build up our intuition) (credit)

Summary

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