52
KIT001 Programming Preparation Dr. Erin Roehrer Lecture Two: Why Program? Introductory Programming Concepts

KIT001 Lecture Two

Embed Size (px)

DESCRIPTION

gergaer

Citation preview

  • 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

  • 4

    Computing Tools & Terms

    Components of a computer

    How those components interact

    How computers store and manipulate information

    Operating System

  • 5

    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

    Central!Processing!

    Unit"

    Main!Memory!

    Chip that executes program commands

    Primary storage area for programs and data that

    are in acQve use

    CPU interacts with main memory

  • 7

    Hardware Secondary Memory Devices

    Main!Memory!

    Central!Processing!

    Unit!

    Secondary memory devices provide long-term storage

    InformaQon is moved between main memory and secondary memory as needed

    USB Disk!

    Hard Disk!

    DVD/CD!

  • 8

    Typical Hardware Conguration Monitor!

    Keyboard!

    Main!Memory!

    Central!Processing!

    Unit!

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

    USB Disk!

    Hard Disk!

    DVD/CD!

  • 9

    The Central Processing Unit (CPU) A CPU is also called a microprocessor or core

    It/they conQnuously follow(s) the fetch-decode-execute cycle:

    fetch!

    Retrieve an instrucQon from main memory

    decode!Determine what the instrucQon is

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

  • 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

    Main memory is divided into many memory locaQons (or cells)

    9278 9279 9280 9281 9282 9283 9284 9285 9286

    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!

    Large values are stored in consecuQve memory locaQons

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

  • 15

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

    10011010!

    To help us remember where a value is, memory locaQons can be given a name (idenQer)

    firstName!rstName is called a variable.

  • 16

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

    10011010!

    To help us understand the value, variables are associated with a type

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

  • 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 ( key) Reserved words

  • 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

    31

    block !containing!code

    # declarations # executable statements # and/or comments

    Structure of a Program

  • 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

    Interpreted Purely Interpreted

    Directly converted to byte code

    Indirectly converted to byte code

    C, C++ Java, C# Python 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

    PRINT("Hello World!")

    Syntax Error Python is case sensitive

    Find the error

  • 41

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

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

    Find the error

  • 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