MIT Python Programming #2

Lecture 2: Operators and operands; statements; branching, conditionals, and iteration Instructors


Massachusetts Institute of Technology: Introduction to Computer Science and Programming

Introduction to Computer Science & Programming Class Notes

Primitive Data – 3 Types (Numbers, Strings, Booleans).

No matter how complex a data structure you create, fundementally at there
basis you will find some combination of NUMBERS, STRINGS, and BOOLEANS.

Associated with every primitive value is a type.

Strings: Strings
Numbers: Integers, Floats
Booleans: True, False

Operands and Operators are combined in programmatic expressions(code).

Interpreter evaluates and prints code. A script does not print unless it is

The following expression resulted in a Syntax error:
3+ ‘ab’
The error
is a SYNTAX error caused by combining 2
different operands. 3 is an number(integer) and
‘ab’ is a string(string). The following expression resolves this error:

str(3) + ‘ab’(Type Conversion)

str(3) redefines the integer value of 3 as a string. The error is a static semantic error(not syntax as previously stated).
The syntax is ok as we use an operator(plus sign) and an operand(string,
integer), The semantics is the problem because the operator was expecting a
certain kind of structure.

All languages do either strong or weak type checking. Type checking can be
defined as the interpreter validating statements within code meet standards set
in place by such code. All codes offer a range of type checking of weak to

The class was recorded in 2008 and utilized an older version of Python. An
error was NOT FOUND with the following statement:
‘a’ < 3
Current version of Python(3.1.1)
found an error within the above line. Python version used in video is an example
of weak type checking as. The reason an error was not found is that the letter a
was interpreted as being binary data(ASCII Standards).

The following statement
turned the result

Reason being?

The number 9 and 5 are integers. A number with a decimal is a float hence they are to
separate operand values.

The following example:
3 + 4 * 5

turned the following results:

Cause – Operators Precedence

Operator Precedence
**   >>   * /
>>  + –

Exponentiation (**) before multiplication and division ( * / ) before addition
and subtraction ( + – )

So the expression 3 + 4 * 5 multiplied 4 * 5 = 20 before adding 3 (20 + 3 =

To get the result of 35 we use an exponentiation formatted as follows:
(3 + 4) * 7

When in doubt use parans. (I believe he meant parenthesis)

When a certain value is achieved, use a name so that value can be
referred to in other places.

Variables are set using an assignment statement.
Example: x = 3 * 5

Code Links to Machine

Type of variable – Gets from value. (7 is an integer, ‘xyz’ is a string)

X = 3 (X becomes an integer)

Python (Dynamic Types)

If later in program after defining x = 3  you redefine x = ‘abc’, x then
changes its type back to a string.

This is a really bad idea. Don’t change types arbitrarily.
That was a bad idea!

Statements – Legal commands that language can interpret.

Variables Names – Important to the statement.

In Python their are approximately 28 keywords that cannot be used as variable

Straight line program
– Sequence of instructions 1 by 1. HTML could be considered an example of this.

Branching Programs
– Can change order of instructions based on some test. (Usually value of variable)

If? Else:

Boolean Combination        (Boolean has 2
values: True, False)

(and, or, not)


if x < y and x <  z print ‘x is least’

elseif y<z: print ‘y is least.
else: print ‘z is least’

The above Boolean first tests the values of  y and z to confirm if they
are less then the value of x. If this is True it states to print x is least.
The second line states, What else can I do if the above line turns false? Then tests for the value of y to be less then z and if
the answer is TRUE, it prints ‘y is least’

The last line states print z is least, as all other possibilities of the equation have been exhausted.

Here is another example of the same program, written incorrectly:

if x < y  print ‘x is least’

if y < z print ‘y is least’
if z < x print ‘z is least’

Obviously the first line does not factor in all parts needed to properly run.

Iteration (loops) Note – Last 10 minutes of class may need to be reviewed.

The above is my personal notes in regards to this class to help me in the learning process.

  Related Posts