Vous êtes sur la page 1sur 52

KIT001

Programming Preparation
Dr. Erin Roehrer

Lecture Two: Why Program?




Introductory Programming Concepts

Reminders for this week

PUBLIC HOLIDAY Monday 9th of March 2015


This means there is NO lecture next Monday. Lecture is rescheduled for Thursday,
12th of March at 9am. Same locaQon but will only run for one hour.
Lab Classes/Tutorials commence next week, no labs classes on Monday due to public
holiday. Monday classes are now Tuesday 1pm.

CIS accounts, lab access have you sorted this?

Lecture Objectives
CompuQng Tools and Terms
Why program?
What is a programming language?
Programming Tools and Terms
What is Python?
Using Python

Computing Tools & Terms


Components of a computer
How those components interact
How computers store and manipulate information
Operating System

Hardware & Software


Hardware
Physical, tangible parts of a computer
keyboard, monitor, wires, chips

Software
programs and data
a program is a series of instructions
data is what the program instructs the computer to

manipulate (numbers, words etc)

A computer requires both hardware and software

Hardware CPU and Main Memory


Central!
Processing!
Unit"

Primary storage area for


programs and data that
are in acQve use

Chip that executes


program commands

CPU interacts with


main memory
Main!
Memory!

Hardware Secondary Memory Devices


InformaQon is moved
between main memory
and secondary memory
as needed

Central!
Processing!
Unit!

Hard Disk!
Main!
Memory!
USB Disk!
DVD/CD!

Secondary memory
devices provide
long-term storage

Typical Hardware Conguration


Monitor!
Keyboard!

Central!
Processing!
Unit!

Hard Disk!
I/O (Input Output)
devices for
communicaQon with
CPU

Main!
Memory!

USB Disk!
DVD/CD!

The Central Processing Unit (CPU)


A CPU is also called a microprocessor or core
It/they conQnuously follow(s) the fetch-decode-execute cycle:
Retrieve an instrucQon from main memory

fetch!

execute!
Carry out the
instrucQon
- access and modify
data if instructed to

decode!
Determine what the
instrucQon is

10

Software Categories
Operating System

controls all machine activities


user interface to the computer
manages resources such as the CPU and memory
Files
Programs e.g. editor, compiler

Application program (App)

generic term for any other kind of software


we will write applications

Graphical user interface (GUI)

some of our programs will have a GUI

Applications
UlQmately, any applicaQon we create will interact

(successfully or unsuccessfully) with the computer


and the data it contains.

Any applicaQon manipulates data in some way.


Create
Read
Update
Delete

How computers store data


All data that is stored within a computer is

converted to 0s and 1s

The data is stored in the computers memory


The memory is divided into storage locaQons

known as bytes

Le[er, numbers, and symbols are stored as binary

numbers (combinaQons of 0s and 1s)

13

Memory
9278
9279
9280
9281
9282
9283
9284
9285
9286

Main memory is divided


into many memory
locaQons (or cells)

Each memory cell has a


numeric address, which
uniquely idenQes it, o^en
these are numbers in
hexadecimal, e.g. 9a3f

14

Storing Information
9278!
9279!
9280!
9281!
9282!
9283!
9284!
9285!
9286!

10011010!

Each memory cell stores a set


number of bits (usually 8 bits, or
one byte) called a word

Large values are


stored in consecuQve
memory locaQons

15

Storing Information
9278!
9279!
9280!
9281!
9282!
9283!
9284!
9285!
9286!

10011010!

firstName!
rstName is called a
variable.

To help us remember where


a value is, memory locaQons
can be given a name
(idenQer)

16

Storing Information
9278!
9279!
9280!
9281!
9282!
9283!
9284!
9285!
9286!

10011010!

firstName (String)!
rstName is a variable of
type String.

To help us understand the


value, variables are
associated with a type

Why Program?
Computers are very fast, but stupid machines
Things that humans might nd hard, or boring,

computers are actually good at

Relevant to most elds of experQse:

Recording Data
Analysing Data
Finding Pa[erns
Plobng Data

AutomaQng Tasks
Comparing Data
MulQ-Tasking
Image Processing

Why Program?
As such computer programs are used not only in our

leisure time with games and social networks, but also in:
Biology, Chemistry, Physics, Bioinformatics
Commerce, Health, Industry
Sociology, Law, Media Production

Basically computers are used in every field, and often

programs are written by people whose main field of


expertise is not computer programming

Why Program?

To create successful applicaQons that contain

procedures so the computer can communicate with


us and know what we want it to do!

What is a Programming Language


A programming language specifies
words and symbols that can be used to write a program
rules to form valid program statements

Source code (text) written by programmer


Can just be typed into the editor and run; or can be stored in a

file
A program usually consists of one or more classes; Classes
consist of one or more functions; Functions consist of one or
more statements

Types of Programming Languages


Machine Language

Assembly Language (Low Level Language)

High-level Programming Language

Machine Languages
Machine Language
A computers naQve language a set of primiQve
instrucQons.
In the form of binary code
E.g.: 1101101010011010

Assembly Languages
Programming in Machine Language is tedious!
Assembly Language was created to make life a bit

easier.

Uses a short descripQve words, mnemonic, to

represent each of the machine language


instrucQons.

Add 2, 3, result

High-Level Language
English-like
Easier to learn and use than assembly language
InstrucQons in high-level language are called

statements

High level languages are called source code and

need to be translated using another programming


tool called an interpreter or a compiler.

High-Level Languages
Pascal
C
Visual Basic
Java
C++
C#
Python

Programming Tools & Terms


Syntax
Semantics
Structure of a program
Program development cycle

Syntax and Semantics


A program will only run if it follows EXACTLY the rules of the

language (syntax)

syntax rules of a language dene how symbols, reserved words, and


idenQers may be put together to make a valid program

A program will always do what we tell it to do, not what we

MEANT to tell it to do (semanQcs)

semanQcs of a program statement dene what that statement


means (what it will do when executed)

A program that is syntacQcally correct is not necessarily

logically (semanQcally) correct

Structure of a program
Think of wriQng a le[er or email:
A wri[en language species
words and punctuaQon that can be used to write a le[er
rules to form understandable and recognisable

statements
whitespace doesnt change the meaning of the le[er

Paper/Email contains the le[er


Consists of secQons (salutaQon, subject, body, signature)
Body consists of paragraphs; Paragraphs consist of

sentences

Re: My recent dummy-spit

Dear Mummy,
I am writing because I cannot find my teddy. It was
sitting next to my beer, and I can no longer find it.
If you have stolen it, could you please return it? The
other guys in the regiment all have theirs Thanks.
Sincerely,
your loving son Mikey.

Components of a program
IdenQers
Rules le[ers, numbers, and underscore (_) only and
cannot be the same as reserved words
ConvenQons start with a le[er, examples later
Comments
# rest of line is comment
''' everything is comment up
to '''
White Space (space, tab, blank lines)
Blocks must be indented evenly
Statements end with a new line (<Enter> key)
Reserved words

31

Structure of a Program
# declarations
# executable statements

block !
containing!
code

# and/or comments
Some languages
Use syntax to collect statements into blocks, e.g. {} but

Python just uses alignment


Use syntax to separate statements, e.g. ; but Python can
just use the end of line

Statements

print("Hello World!")
print("Programming is fun!")

Programming Style & Documentation


Appropriate Comments
Proper IndentaQon and Spacing

The Program Development Cycle

Design the
program

Write the
code

Correct
syntax
errors

Test the
program

Correct
logic
errors

What is Python?
Developed by Guido van Rossum in 1991.

Python is a high-level general-purpose programming language that can
be applied to many dierent classes of problems.

With Python, students can be quickly introduced to basic concepts
required for successful programming.

Easy to read
Less development Qme (shorter code)
Reduced learning Qme


What is Python?
Complied

Directly
converted
to machine
code

Directly
converted to
byte code

Indirectly
converted
to byte
code

C, C++

Java, C#

Python

Interpreted

Purely
Interpreted

Shell, Perl

Who uses python?


On-line games
Web services
ApplicaQons
Science
Instrument Control
Embedded Systems

Pythons Reserved Words


and
as
assert
break
class
continue
def
del
elif

else
except
exec
finally
for
from
global
if
import

in
is
lambda
not
or
pass
print
raise
return

try
while
with
yield

When we dont follow the rules.


Three types of errors
Problems with syntax (compiler errors)
No execuQon or executable program produced

Problems during program execuQon (run-Qme

errors)

e.g. divide by zero, wrong sort of input


program terminates abnormally

Problem with semanDcs (logical errors)


program runs, but produces incorrect results

40

Find the error


Syntax Error Python is case sensitive

PRINT("Hello World!")

41

Find the error


print("program over")
print("program begins")

Semantic error - message "program over" will


appear before message "program begins"

Using Python
We want to launch the IDLE Python 2.7

programming environment from the Start Menu.


All Programs Development Tools Python 2.7

IDLE (Python GUI)


Or search for Python

Python 2 vs Python 3

Python 3 is a newer version, but it is not backward
compaQble with Python 2. That means if you write a
program using Python 2, it may not work on Python 3.

The Command Line

A First Python Statement

Understanding the Integrated


DeveLopment Environment (IDLE)
You can create any Python applicaQon you want using

just a text editor however there are InteracQve


Development Environments (IDEs) that can make the
process easier.

IDLE is the IDE that comes with Python.


IDLE provides the funcQonality to:
Write Python Code
Perform simple and code-based ediQng
Save and Open Python (lename.py) les
Perform simple debugging task, and more.

Creating your rst application

Creating a le

Running your application

Python on your own device


Python works on a number of platorms

(combinaQon of computer hardware and operaQng


systems so^ware).

To get the right version visit:

h[p://www.python.org/download/ (you will need


to scroll down to the download secQon). Most Mac
computers will already have Python installed.

We are using version 2.7 in the compuQng labs

The Record Book


Contents any paper other than copies of text books
Printed notes (lecture and tutorial)
Your additional notes (lecture and tutorial)
Program listings, assignments, old exam papers and

revision
Uses include
Exam this is the ONLY material that you may take into

the examination

Resources
Unit MyLO Web site
lecture recordings
tutorial and activity work
assessment information
Python Web sites
http://docs.python.org/2/tutorial/
http://wiki.python.org/moni/BeginnersGuide/
Programmers
http://www.learnpython.org

Vous aimerez peut-être aussi