Comp 112

Lecture 1

Expressions, Variables and Programs

2018.01.30

Expressions and Values

A Simple Expression Language

Integer Values (0, 1, 2, -3, etc.)

Integer Arithmetic:

Boolean Values (True, False)

Boolean Logic:

Numeric Comparisons:

Evaluating Compound Expressions

The expression:

parses as:

and evaluates to:

Types

Types classify expressions.

To ask Python the type of an expression, use:

type (<expression>)

e.g.

The type of an expression is always the same as the type of its value.

Floats

What happens if we try to do division (_/_)?

11 / 3

Overloading

The arithmetic and comparison operators are overloaded to act on floats as well:

2.5 + 3.5
2.5 < 3.5

Overloading is when different (but conceptually related) operations are given the same notation.

This is just for our convenience, under the hood they are completely different operations.

The types are used to tell them apart.

As expressions get more complicated it becomes increasingly important to keep track of their types.

Strings

Statements

Variables

Variables (“references”, “assignables”) are names that refer to values.

Rules for variable names:

Variable Assignment

An assignment statement causes a variable to refer to the value of an expression:

The value of the variable becomes that of the expression assigned to it.

Warning: assignment is not mathematical equality! Read “=” as “gets”, and note the difference to “==” (“is equal to”).

Variable Assignment Process

When an assignment statement is executed:

  1. First, the expression on the right is evaluated to a value.

  2. Next, that value is assigned to the variable on the left.

Variable Tables

As Python runs a program, it repeatedly consults and updates its variable table to keep track of the program’s state.

Programs

Unlike the interactive interpreter, running a program from a file does not cause the value of every expression to be displayed.

So how can we get a Python program to show us what it’s computing?

Comments

Comments in Submissions

Every script file you turn in should begin with a comment containing:

  1. your name
  2. your Wesleyan email address
  3. the course number and your lab section
  4. the assignment name or number
  5. the date

To Do This Week: