Sie sind auf Seite 1von 52

KIT001

ì

Programming Preparation

Dr. Erin Roehrer

     

Lecture Two: Why Program? Introductory Programming Concepts

ì Reminders for this week
ì
Reminders for this week

PUBLIC HOLIDAY Monday 9 th of March 2015 This means there is NO lecture next Monday. Lecture is rescheduled for Thursday, 12 th 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
Lecture Objectives

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

4

Computing Tools & Terms
Computing Tools & Terms

ì

Components of a computer

ì

How those components interact

ì

How computers store and manipulate information

ì

Operating System

5

Hardware & Software
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

6

Hardware – CPU and Main Memory
Hardware – CPU and Main Memory
Central! Processing! Unit"
Central!
Processing!
Unit"

Chip that executes program commands

Primary storage area for programs and data that are in acQve use Main! Memory!
Primary storage area for
programs and data that
are in acQve use
Main!
Memory!

CPU interacts with main memory

7

Hardware – Secondary Memory Devices
Hardware – Secondary Memory Devices
Central! Processing! InformaQon is moved between main memory and secondary memory as needed Unit! Hard Disk!
Central!
Processing!
InformaQon is moved
between main memory
and secondary memory
as needed
Unit!
Hard Disk!
Main!
Memory!
Secondary memory
devices provide
long-term storage
USB Disk!
DVD/CD!

8

Typical Hardware Configuration
Typical Hardware Configuration

Monitor!

Keyboard!

Central! Processing! Unit!
Central!
Processing!
Unit!

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

Main! Memory!
Main!
Memory!
8 Typical Hardware Configuration Monitor ! Keyboard ! Central! Processing! Unit! I/O (Input Output) devices for
8 Typical Hardware Configuration Monitor ! Keyboard ! Central! Processing! Unit! I/O (Input Output) devices for
Hard Disk!
Hard Disk!
USB Disk! DVD/CD!
USB Disk!
DVD/CD!

9

The Central Processing Unit (CPU)
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

9 The Central Processing Unit (CPU) A CPU is also called a microprocessor or core It/they

execute!

fetch!

9 The Central Processing Unit (CPU) A CPU is also called a microprocessor or core It/they

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

Carry out the instrucQon - access and modify data if instructed to
9 The Central Processing Unit (CPU) A CPU is also called a microprocessor or core It/they

decode!

Determine what the instrucQon is

10

Software Categories
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
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
How computers store data

ìAll data that is stored within a computer is converted to 0’s and 1’s

ì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 0’s and 1’s)

13

Memory
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 idenQfies it, o^en

these are numbers in

hexadecimal, e.g. 9a3f

13 Memory 9278 9279 9280 9281 9282 9283 9284 9285 9286 Main memory is divided into

14

Storing Information
Storing Information
 

9278!

 

10011010!

9279!

 

9280!

 

9281!

 

9282!

 

9283!

 

9284!

 

9285!

 

9286!

 
 

Each memory cell stores a set

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

Each memory cell stores a set number of bits (usually 8 bits, or one byte) called

Large values are

stored in consecuQve

memory locaQons

15

Storing Information
Storing Information
 

9278!

   

9279!

10011010!

9280!

 

9281!

 

9282!

 

9283!

 

9284!

 

9285!

 

9286!

 
 

firstName!

firstName is called a

variable.
variable.

To help us remember where

a value is, memory locaQons

can be given a name

(idenQfier)

16

Storing Information
Storing Information
 

9278!

   

9279!

10011010!

9280!

 

9281!

 

9282!

 

9283!

 

9284!

 

9285!

 

9286!

 
 

firstName (String)!

firstName is a variable of

type String.

To help us understand the

value, variables are

associated with a type

Why Program?
Why Program?

ìComputers are very fast, but stupid machines

ìThings that humans might find hard, or boring, computers are actually good at

ìRelevant to most fields of experQse:

Recording Data Analysing Data Finding Pa[erns Plobng Data

AutomaQng Tasks Comparing Data MulQ-Tasking Image Processing

Why Program?
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?
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
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
Types of Programming Languages

ìMachine Language

ìAssembly Language (Low Level Language)

ìHigh-level Programming Language

Machine Languages
Machine Languages

ìMachine Language

ìA computer’s naQve language – a set of primiQve instrucQons.

ìIn the form of binary code

ìE.g.: 1101101010011010

Assembly Languages
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
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
High-Level Languages

ìPascal

ìC

ìVisual Basic ìJava ìC++ ìC# ìPython

Programming Tools & Terms
Programming Tools & Terms

ìSyntax

ìSemantics

ìStructure of a program

ìProgram development cycle

Syntax and Semantics
Syntax and Semantics

ìA program will only run if it follows EXACTLY the rules of the language (syntax)

ìsyntax rules of a language define how symbols, reserved words, and idenQfiers 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 define 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
Structure of a program

Think of wriQng a le[er or email:

ìA wri[en language specifies

ìwords and punctuaQon that can be used to write a le[er

ìrules to form understandable and recognisable statements

ì‘whitespace’ doesn’t 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

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? other guys in the regiment all have theirs… Thanks.

The

Sincerely,

your loving son Mikey.

Components of a program
Components of a program

ìIdenQfiers

ì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
Structure of a Program

# declarations # executable statements # and/or comments

ìSome languages

31 Structure of a Program # declarations # executable statements # and/or comments ì   Some

block ! containing! code

31 Structure of a Program # declarations # executable statements # and/or comments ì   Some

ì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
Statements

print("Hello World!")

print("Programming is fun!")

Programming Style & Documentation
Programming Style & Documentation

ìAppropriate Comments ìProper IndentaQon and Spacing

Programming Style & Documentation ì   Appropriate Comments ì   Proper IndentaQon and Spacing
The Program Development Cycle
The Program Development Cycle
Correct Correct Design the Write the Test the syntax logic program code program errors errors
Correct
Correct
Design the
Write the
Test the
syntax
logic
program
code
program
errors
errors
What is Python?
What is Python?
What is Python? Developed by Guido van Rossum in 1991. Python is a high-level general-purpose programming

Developed by Guido van Rossum in 1991.

Python is a high-level general-purpose programming language that can be applied to many different 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? Developed by Guido van Rossum in 1991. Python is a high-level general-purpose programming
What is Python? Developed by Guido van Rossum in 1991. Python is a high-level general-purpose programming
What is Python?
What is Python?

Complied

Interpreted

Directly

Directly

Indirectly

Purely

converted

converted to

converted

Interpreted

to machine

byte code

to byte

code

code

Directly Directly Indirectly Purely converted converted to converted Interpreted to machine byte code to byte code
Directly Directly Indirectly Purely converted converted to converted Interpreted to machine byte code to byte code
 

C, C++

Java, C#

Python

Shell, Perl

Who uses python?
Who uses python?

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

Python’s Reserved Words
Python’s Reserved Words

and

else

in

try

as

except

is

while

assert

exec

lambda

with

break

finally

not

yield

class

for

or

continue

from

pass

def

global

print

del

if

raise

elif

import

return

When we don’t follow the rules….
When we don’t 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
Find the error

Syntax Error – Python is case sensitive

40 Find the error Syntax Error – Python is case sensitive PRINT("Hello World!")

PRINT("Hello World!")

41

Find the error print("program over") print("program begins")
Find the error
print("program over")
print("program begins")

Semantic error - message "program over" will appear before message "program begins"

Using Python
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 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
The Command Line
The Command Line
A First Python Statement
A First Python Statement
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 (IDE’s) 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 (filename.py) files

ìPerform simple debugging task, and more.

Creating your first application
Creating your first application
Creating your first application
Creating a file
Creating a file
Creating a file
Running your application
Running your application
Running your application
Python on your own device
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
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
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