Comp 112
Lecture 1
Expressions, Variables and Programs2018.01.30
Expressions represent data.
2 + 3
Evaluating an expression yields a value.
2 + 3 ⟼ 5
The value represents the same data, but in a canonical form.
2 + 3 = 5
Python knows how to evaluate expressions.
Typing an expression into the Python interactive interpreter causes it to evaluate the expression and display the value.
Integer Values (0
, 1
, 2
, -3
, etc.)
Integer Arithmetic:
_+_
), subtraction (_-_
), multiplication (_*_
)_//_
), remainder (_%_
)_**_
)Boolean Values (True
, False
)
Boolean Logic:
not_
), conjunction (_and_
), disjunction (_or_
)Numeric Comparisons:
_<_
), is greater than (_>_
)_==_
), is not equal to (_!=_
)_<=_
), is greater than or equal to (_>=_
)The expression:
parses as:
and evaluates to:
Types classify expressions.
To ask Python the type of an expression, use:
e.g.
The type of an expression is always the same as the type of its value.
type of integers: int
type of booleans: bool
What happens if we try to do division (_/_
)?
Floating point numbers are variable-precision decimal approximations.
They have type float
.
Python uses the decimal point to indicate that a number is a float
rather than an int
.
Compare:
The arithmetic and comparison operators are overloaded to act on float
s as well:
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 are sequences of characters (i.e. text).
To enter a string, surround it with (single or double) quotes:
Strings have type str
.
Python overloads _+_
as string concatenation:
and _*_
as string repetition:
A statement is an instruction to perform some actions.
When Python executes a statement, it performs the corresponding actions.
Observable changes caused by actions are called effects.
(e.g. taking user input from the keyboard, printing text to the screen, drawing a picture, saving data to a file, etc.)
The central role of statements is what makes Python an imperative programming language.
In Python, there is a statement that does nothing:
It may seem useless, but sometimes we must provide a statement even though we don’t want anything to happen.
Variables (“references”, “assignables”) are names that refer to values.
Rules for variable names:
may contain only letters, digits and underscores (“_
”),
cannot begin with a digit,
are case sensitive,
can’t be any of Python’s keywords (see reading).
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”).
When an assignment statement is executed:
First, the expression on the right is evaluated to a value.
Next, that value is assigned to the variable on the left.
A variable table (a.k.a “environment” or “state diagram”) keeps track of the value assigned to each variable:
variable | value | |
---|---|---|
w |
⟼ | 'hello' |
x |
⟼ | 42 |
y |
⟼ | 3.14 |
z |
⟼ | True |
⋮ | ⋮ |
Assigning a value to a new variable causes it to be added to the table.
Reassigning a different value to an existing variable causes its table entry to be overwritten.
It is an error to refer to a variable that has not yet been assigned a value.
As Python runs a program, it repeatedly consults and updates its variable table to keep track of the program’s state.
A Python program is a sequence of statements.
We can save a program to a plain text script file to edit or run later.
We will use Python’s built-in program editor, called IDLE, for writing Python programs.
Python script files should have the suffix “.py
”.
Running a program causes its statements to be executed in order (modulo features that we’ll meet later).
You can run Python scripts using:
python
” (or “python3
”) command from a terminal.A print statement causes a textual representation of a value to be written to the screen:
Print statements can be used in programs to show the value of an expression.
Otherwise, the value will be computed, but not displayed anywhere.
Note: the term “print
statement” is a slight lie: print
is not actually a statement in modern Python, the terminology is a historical holdover.
Every script file you turn in should begin with a comment containing:
Do the reading:
Come to lab on Thursday.
Complete homework 1.
Get help from the instructor or CA if you had trouble installing Python on your computer.
Comments
A comment is text in a program intended for humans not computers.
Comments are ignored when the program is run.
In Python, comments start with a “#” character and go to the end of the line:
An alternate style of comment starts with a triple-quote (
"""
) and extends to the closing triple-quote: