Lecture 3
Lecture 3
Programming
Steve James
Lecture 3
RECAP
Algorithms
• Individual, well-defined, predictable
instructions (finite).
• Procedure in terms of:
– Actions to be executed
– The order to execute these actions in
Variables
• Name and values
• Assign name to a value
meaning_of_life = 42
y = 2.45824406892
• Data types:
– Integer, Float, Boolean, etc
Arithmetic Operators
• Addition, subtraction, multiplication, division,
modulus
name = input()
average_score = float(input())
PYTHON CODE
grade = int(input()) COLON
if grade >= 50: NECESSARY!
print("Passed")
TAB/4 SPACES
NECESSARY!
Logical Expressions
• A logical expression is something that
evaluates to either TRUE or FALSE
• FALSE has a numeric value of 0
• TRUE has a numeric value of non-zero integer
Rules of Precedence
• Logical operators have precedence too
Precedence Operator/ Evaluation
Operation
Level Symbol Direction
0 (first) () parentheses Inner ones
evaluated first
Left-to-right
1 <=, <, >, >=, less than or equal, less Left-to-right
==, != than, greater than,
greater than or equal,
equal to, not equal to
2 not negation (unary NOT) Left-to-right
3 and logical AND Left-to-right
4 or logical OR Left-to-right
https://docs.python.org/3.3/reference/expressions.html#operator-precedence
Examples
if 2 < 3:
print("2 is less than 3")
if 2 != 3:
print("2 is not equal to 3")
AND/OR Operators
• Imagine that two logical expressions (A and B)
evaluates to either TRUE (1) or FALSE (0). The
results of (A and B) and (A or B) for all possible
values of A and B are tabulated below (truth
table):
A B A and B A or B A B A and B A or B
FALSE FALSE FALSE FALSE 0 0 0 0
FALSE TRUE FALSE TRUE 0 1 0 1
TRUE FALSE FALSE TRUE 1 0 0 1
TRUE TRUE TRUE TRUE 1 1 1 1
AND/OR Operators
• Note that and and or are short-circuit
operators:
– If the answer can be proved after evaluating the
first condition, the second condition is not
evaluated!
Task
• Write a program that accepts two marks as
integers from input.
grade = int(input())
if grade >= 75:
print("A")
elif grade >= 70:
Can have as
print("B")
many elif’s as
elif grade >= 60:
we want!
print("C")
elif grade >= 50:
print("D")
else:
print("F")
num = int(input())
if num % 2 == 0:
if num % 3 == 0:
print("Divisible by 2 and 3")
else:
print("Divisible by 2; not divisible by 3")
else:
if num % 3 == 0:
print ("Divisible by 3; not divisible by 2")
else:
print ("Not divisible by 2; not divisible by 3")
ALGORITHMS
LOOPS
The Issue
• So far, our algorithms have been a forward-
flowing sequence of statements
• But what if we want to execute something
repeatedly?
Iteration
• Types of loops
– The while loop
– The do/while loop (not in Python!)
– The for loop
• Related concept:
– Break/continue statements
The while loop
• Code executed repeatedly as long as some
condition remains true
PSEUDOCODE
while logical_expression:
statement_1
statement_2 }
Loop body
• What it takes
– A control variable; i
– Initialise control variable; i=0
– Termination condition; if i < 100 is false, exit
– Increment control variable; i=i+1
– Some task to do each time; print …
A problem
• A class of ten students took a test. The grades
(integers in the range 0 to 100) for this test are
available to you. Determine the class average
• Top-level pseudocode:
Determine the class average of the test
• First refinement:
Input and sum 10 test grades
Calculate and print class average
Pseudocode
Set total to zero
Set counter to one