cmp2412 programming principles

40
Programming Principles CMP 2421

Upload: nikanor-thomas

Post on 14-Apr-2017

155 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Cmp2412 programming principles

Programming PrinciplesCMP 2421

Page 2: Cmp2412 programming principles

Programming Principles

Four lectures a week for 14 weeksClass: 12h30, Tuesday

12h30, Wednesday 09h30-11h25, Friday

Practical session: Thursday 16h30Test: 2 minimum, assignment, presentation, Quiz

Page 3: Cmp2412 programming principles

Ms. Ntinda M.N Assistance: Mathe and Paulus Light talks (5 min max) Install:

Windows OS Python 3…. gedit

Day1

Page 4: Cmp2412 programming principles

Is to teach you to think like a computer Scientist

This way of thinking combines some of the best features of mathematics, engineering and natural science

Goal

Page 5: Cmp2412 programming principles

Like Mathematicians, Computer scientists use formal languages to denote ideas(Specifically computation)

Like Engineers, they design things, assembling components into systems and evaluating tradeoffs among alternatives

Like Scientists, they observe the behavior of complex systems, form hypotheses and test predictions

Goal

Page 6: Cmp2412 programming principles

Natural Language Formal Language

Full of ambiguity which people deal with by using contextual clues and other information

Designed to be nearly or completely unambiguous, which means that any statement has exactly one meaning, regardless of context

In order to make up for ambiguity and reduce misunderstanding, natural languages employ lots of redundancy

Less redundant and more concise

Literalness are full of idiom and metaphor

Mean exactly what they say

Natural language vs Formal Language

Page 7: Cmp2412 programming principles

The single most important skill for a computer Scientist is problem solving

Goal

Page 8: Cmp2412 programming principles

The ability to formulate problems, think creatively about solutions and express a solution clearly and accurately.

The process of learning to program is an excellent opportunity to practice problem solving skills

Problem Solving means:

Page 9: Cmp2412 programming principles

Is an artificial language used to write instructions that can be translated into machine language and then executed by a computer. Examples:

Python Visual basics Java C# C++ Pearl

What is a programming Language

Page 10: Cmp2412 programming principles

The programming Language you will be learning in this course is Python

Python is an example of a high level programming language.

Other High level programming languages: C,C++,Perl and Java

Programming Language

Page 11: Cmp2412 programming principles

There are also low-level programming languages, sometimes referred to as “machine Languages” or “Assembly languages”

Example of low level programming Languages: Machine language and Assembly language

Programming Languages

Page 12: Cmp2412 programming principles

Programming Languages

Page 13: Cmp2412 programming principles

High level  enable a programmer to write programs that are more or less independent of a particular type of computer. 

Such languages are considered high-level because they are closer to human languages and further from machine languages.

In contrast, assembly languages are considered low-level because they are very close to machine languages.

Define: High level language, Low level languages

Page 14: Cmp2412 programming principles

Computers can only execute programs written in low level languages.

Programs written in high level language have to be processed before they can run.

This extra processing takes some time, which is a small disadvantage of high level languages.

Page 15: Cmp2412 programming principles

A related meaning "to execute" refers to the specific action of a user starting (or launching or invoking) a program, as in "Please run the ... application.“

“RUN”

Page 16: Cmp2412 programming principles

High level Low levelIt is much easier to program Difficult to program Takes less time to write, shorter and easier to read and more likely to be correct

Take longer to write and not easy to read

Portable(they can run on different kinds of computers with or no modifications)

Low level programs can run on only one kind of computer and have to be rewritten to run on another

High level vs low level languages

Page 17: Cmp2412 programming principles

Two kinds of programs process high-level languages into low-level languages:

Interpreters and Compilers

High-level program languages

Page 18: Cmp2412 programming principles

A compiler reads the program and translate it completely before the program starts running.

Compiler

Compiler Object code

Executor Output

Page 19: Cmp2412 programming principles

In this case, a high-level language is called, a source code and the translated program is called the object code or the executable.

Once a program is compiled, you can execute it repeatedly without further translation

Compiler continue……

Page 20: Cmp2412 programming principles

An interpreter reads a high-level program and executes it, meaning that it does what the program says, it processes the program a little at a time, alternatively reading times and performing computations

Interpreter

OutputInterpreterSourceCode

Page 21: Cmp2412 programming principles

Python is considered to be an interpreted language because python programs are executed by an interpreter

Interpreter

Page 22: Cmp2412 programming principles

A program is a sequence of instructions that specifies how to perform a computation.

The computation might be something mathematical such as solving a system of equations or finding the roots of a polynomial, but it can also be a symbolic computation such as searching and replacing text in a document

What Is a program?

Page 23: Cmp2412 programming principles

Input: get data from the keyboard, a file or some other device

Output: Display data on the screen or send data to a file or other device

Math: perform basic mathematical operations like addition and multiplication

Conditional execution: Check for certain condition and execute the

appropriate sequence of statement. Perform some repeatedly, usually with some variation

Every program you have ever used is made up of the following instructions:

Page 24: Cmp2412 programming principles

Programming is the process of breaking a large complex task into smaller and smaller subtasks until the subtasks are simple enough to be performed with one of these basic instructions.

What is programming

Page 25: Cmp2412 programming principles

Programming is a complex process, and because it is done by human beings, it often leads to errors.

Python can only execute a program if the program is syntactically correct otherwise the process fails and returns an error message.

Three kinds of errors Runtime errors Syntax errors Semantic errors

What is debugging

Page 26: Cmp2412 programming principles

Syntax refers to the structure of a program and the rules about that structure.

Example: In English, a sentence must begin with a

capital letter and end with a period. In English, you can be forgiven when you

have a grammatical error. Python is not forgiving

Syntax errors

Page 27: Cmp2412 programming principles

If there is a single error anywhere in your program, python will print an error message and quit, and you will not be able to run your program

Learn your Syntax Beginners spend more time tracking errors,

make use of Google……with experience, you will make few errors and track them down easier

Syntax errors cont……

Page 28: Cmp2412 programming principles

Runtime errors does not appear until you run the program.

These errors are also called exceptions because they usually indicate that something exceptional(and bad) has happened.

Runtime errors are rare in the simple programs, you will see in the first few examples you might not encounter one

Runtime errors

Page 29: Cmp2412 programming principles

Occurs when the program run successfully, in the sense that the computer will not generate any error messages but it will not do the right thing.

The program will do something else.

Semantic errors

Page 30: Cmp2412 programming principles

Example: We want to add 5+10 = 15 instead we add

3+10 = 13. Identifying semantic errors can be tricky

because it requires you to work backward by looking at the output of the program and trying to figure out what it is doing

Semantic errors cont….

Page 31: Cmp2412 programming principles

Know how to debug For some people, debugging and

programming is the same thing. Programming is the process of gradually

debugging a program until it does what you want.

Debugging

Page 32: Cmp2412 programming principles

Command-line mode and script mode In command-line mode, you type python

programs and the interpreter prints the results In a script mode, you can write a program in a

file and use the interpreter to execute the contents of the file. Such a file is called a script.

Use a text editor to crate a file. As with MsWord file .doc,,,,,,,,python files end

with .py

There are two ways to use an interpreter:

Page 33: Cmp2412 programming principles

Python is a very popular programming language that can be used for creating websites, games, scientific software, graphics

Python originated in the late 1980s and its main goal is to be readable by human beings (not only machines!). 

Python

Page 34: Cmp2412 programming principles

https://www.python.org/downloads/ make sure you scroll down and choose the

"Add python.exe to the Path" option, as shown here: Type python in cmd to Confirm it was successfully installed

Download Python for Windows from the website

Page 35: Cmp2412 programming principles

Gedit is an open-source, free editor, available for all operating systems.

http://ftp.gnome.org/pub/GNOME/binaries/win32/gedit/2.30/

Why are we installing a code editor? Code needs to be plain text Problem with programs like Word and Textedit is that they

don't actually produce plain text, they produce rich text (with fonts and formatting)

 Code editors provide helpful features, like highlighting code with colour according to its meaning, or automatically closing quotes for you.

Code editor: gedit

Page 36: Cmp2412 programming principles

command line or command-line interface is a text-based application for viewing, handling, and

manipulating files on your computer.  Other names for the command line are: cmd,CLI, prompt, console or terminal.

Open the command-line interface Windows

Go to Start menu → All Programs → Accessories → Command Prompt.

Prompt On Windows, it's a > sign, like this: command-line >

Introduction to the command-line interface

Page 37: Cmp2412 programming principles

Current directory (know where we are) > cd C:\Users\nntinda.UNAM.000

List files and directories (what's in it?) > dir Directory of C:\Users\nntinda.UNAM.000 05/06/2015 07:28 PM <DIR> Applications 05/06/2015 07:28 PM <DIR> Desktop 05/06/2015 07:28 PM <DIR> Downloads 05/06/2015 07:28 PM <DIR> Music ...

Basics: Each operating system has a slightly different set of commands for the command line

Page 38: Cmp2412 programming principles

Change current directory (let's go to Desktop directory) > cd Desktop

PRO tip: if you type cd D and then hit tab on your

keyboard, the command line will automatically autofill the rest of the name so you can navigate faster. If there is more than one folder starting with "D", hit the tab button twice to get a list of options.

Basics: Each operating system has a slightly different set of commands for the command line

Page 39: Cmp2412 programming principles

Create directory (folder) title: helloWorld > mkdir helloWorld

PRO tip: If you don't want to type the same commands

over and over, try pressing the up arrow and down arrow on your keyboard to cycle through recently used commands.

Basics: Each operating system has a slightly different set of commands for the command line

Page 40: Cmp2412 programming principles

• Create a folder on your desktop called firstYearDiploma

• In your newly created  firstYearDiploma directory create a directory called name,20150999(student number).

Exercise