Lecture 1
Lecture 1
David Weston
Acknowledgement: Many of these slides are based on slides accompanying the course text
1
Lectures, Labs, Assessments
2
Accessing module resources
You can access copies of the lecture notes, lab sheets, class
work, class work solutions etc. from Moodle
http://moodle.bbk.ac.uk/
http://www.dcs.bbk.ac.uk/~dweston/
3
Books
4
Advice to users of Display Screen Equipment
Posture
• http://www.safecomputingtips.com/ergonomic-office-desk-
setup/
(informative picture of posture)
Eye Strain
https://www.dcs.bbk.ac.uk/intranet/index.php/VDU_Advice
5
Aims of Week 1
6
Computer Programs
7
Hardware and Software
THE BUILDING BLOCKS THAT MAKE UP A
COMPUTER
8
Hardware
9
Simple View of a Computer’s Components
10
The CPU
The CPU has two components, the control unit and the
arithmetic logic unit
The control unit directs operation of the processor.
• All computer resources are managed by the control unit.
• It controls communication and co-ordination between
input/output devices.
• It reads and interprets instructions and determines the
sequence for processing the data.
• It provides timing and control signals
The arithmetic logic unit contains the circuitry to perform
calculations and do comparisons.
• It is the workhorse portion of the computer and its job is to
do precisely what the control unit tells it to do.
11
Storage
12
Memory
13
Executing a Program
14
Software
15
Algorithms
16
Introduction to Algorithms
17
Algorithm: Formal Definition
18
Problem Solving: Algorithm Design
19
A Simple Example
20
Second Example: Selecting a Car
Problem Statement:
You have the choice of buying two cars.
One is more fuel efficient than the other, but also more
expensive.
You know the price and fuel efficiency (in miles per gallon, mpg)
of both cars.
You plan to keep the car for ten years.
Which car is the better deal?
21
Developing the Algorithm
Determine the inputs and outputs
From the problem statement we know:
• Car 1: Purchase price, Fuel Efficiency
• Car 2: Purchase price, Fuel Efficiency
• Price per gallon = £4.00
• Annual miles driven= 15,000
• Length of time = 10 years
For each car we need to calculate:
• Annual fuel consumed for each car
• Annual fuel cost for each car
• Operating cost for each car
• Total cost of each Car
Then we select the car with the lowest total cost
22
Translating the Algorithm to pseudocode
Break down the problem into smaller tasks
• ‘Calculate total cost’ for each car
• To calculate the total cost for each year we need to calculate the
operating cost
• The operating cost depends on the annual fuel cost
• The annual fuel cost is the price per gallon * the annual fuel
consumed
• The annual fuel consumed is the annual miles drive / fuel
efficiency
Describe each subtask as pseudocode
• total cost = purchase price + operating cost
23
The Pseudocode
24
Bank Account Example
Problem Statement:
• You put £10,000 into a bank account that earns 5 percent
interest per year. How many years does it take for the
account balance to be double the original?
How would you solve it?
• Manual method
• Make a table
• Add lines until done
• Use a spreadsheet!
• Write a formula
• Per line, based on line above
25
Develop the algorithm steps
26
Translate to pseudocode
Pseudocode
• Half-way between natural language and a programming
language
Modified Steps
• Set the year value of 0
• Set the balance to £10,000
• While the balance is less than £20,000
• Add 1 to the year value
• Multiply the balance by 1.05
• Report the final year value as the answer
The pseudocode is easily translated into Python
27
The Python Language
28
Programming Environments
29
IDE components
30
IDLE Shell - See Lab Sheet for more Detail
31
IDLE Text Editor
32
Your first program
33
Text editor programming
You can also use a simple text editor to write your source code.
This approach is not recommended.
Once saved as hello.py, you can use a console window to:
• Compile the program
• Run the program
• On windows this console window is called the command
prompt. You will need to be able to move to the directory
containing your program. We shall return to this next term
Compile/execute
Output
34
Organize your work
35
Python interactive mode
36
Source Code to a Running Program
37
“Hello World”
38
Analyzing The Program
39
Basic Python Syntax: Print
40
Syntax for Python Functions
41
More Examples of the print Function
42
Our Second Program (Page 12, printtest.py)
##
# Sample Program that demonstrates the print function
#
# Prints 7
print(3 + 4)
43
Errors
44
Syntax Errors
45
Logic Errors
46
Summary: Computer Basics
47
Summary: Python
48
Summary: Python
49
Summary: Errors and pseudo code
50
Week 1 Homework
51