47
Python Programming, 1/e 1 Introduction to Programming Computers and Programs

Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

Python Programming, 1/e 1

Introduction to Programming

Computers and Programs

Page 2: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

2

The Universal Machine

� What is a computer program?

� A detailed, step-by-step set of instructions telling a computer what to do.

� If we change the program, the computer performs a different set of actions or a different task.

� The machine stays the same, but the program changes!

� Programs are executed, or carried out.

� All computers have the same power, with suitable programming, i.e. each computer can do the things any other computer can do.

Page 3: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

3

Program Power

� Software (programs) rule the hardware (the physical machine).

� The process of creating this software is called programming.

� Why learn to program?

� Fundamental part of computer science

� Having an understanding of programming helps you have an understanding of the strengths and limitations of computers.

Page 4: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

4

Types of Programs

� Information Systems- UI portal, payroll

� Embedded Systems – Dedicated systems- programs built for specific devices-

� A program embedded in an elevator to open and close the door

� Heart monitors- tracks patient heartbeat and reports status

� Critical Systems-

� Safety Critical-Aircraft control towers

� Economic critical- program controlling All ATM should crash.

� Health critical – Program controlling insulin pump- gives overdose and patient dies

Page 5: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

5

Programming Languages

� Natural language has ambiguity and imprecision problems when used to describe complex algorithms. � Programs expressed in an unambiguous ,

precise way using programming languages.

� Every structure in programming language has a precise form, called its syntax

� Every structure in programming language has a precise meaning, called its semantics.

Page 6: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

6

Programming Languages

� Programming language like a code for writing the instructions the computer will follow.

� Programmers will often refer to their program as computer code.

� Process of writing an algorithm in a programming language often called coding.

Page 7: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

7

Machine Languages, Assembly Languages, and High-level Languages

� Three types of programming languages

� Machine languages

� Strings of numbers giving machine specific instructions

� Example:

+10001110 (these would really be in binary)

+10011111

+11101101

� Assembly languages

� English-like abbreviations representing elementary computer operations (translated via assemblers)

� Example:

LOAD BASEPAY

ADD OVERPAY

STORE GROSSPAY 7

Page 8: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

8

Machine Languages, Assembly Languages, and High-level Languages

� High-level languages

� Instructions closer to everyday English

English is a natural language. Although high level programming languages are closer to natural languages, it is difficult to get too close due to the ambiguities in natural languages (a statement in English can mean different things to different people – obviously that is unacceptable for computer programming). However, this is a big research area of computer science.

� Use mathematical notations (translated via compilers)

� Example:

grossPay = basePay + overTimePay

� Interpreter – Executes high level language programs without compilation.

8

Page 9: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

9

Programming Languages

� High-level language c = a + b

� This needs to be translated into machine language that the computer can execute.

� Compilers convert programs written in a high-level language into the machine language of some computer.

Page 10: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

10

Programming Languages

� Interpreters simulate a computer that understands a high-level language.

� The source program is not translated into machine language all at once.

� An interpreter analyzes and executes the source code instruction by instruction.

Page 11: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

11

Programming Languages

� Compiling vs. Interpreting � Once program is compiled, it can be

executed over and over without the source code or compiler. If it is interpreted, the source code and interpreter are needed each time the program runs

� Compiled programs generally run faster since the translation of the source code happens only once.

Page 12: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

12

Programming Languages

� Interpreted languages are part of a more flexible programming environment since they can be developed and run interactively

� Interpreted programs are more portable, meaning the executable code produced from a compiler for a Pentium won’t run on a Mac, without recompiling. If a suitable interpreter already exists, the interpreted code can be run with no modifications.

Page 13: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

13

The Key Software Trend: Procedural High-level Languages

� A procedural programming paradigm is a programming technique that works in a step by step method. In order for this method to work you have to create a sequence of instructions of steps, thus telling the computer what to do first and then second. Typical procedural programming languages are C, FORTRAN, pascal, and BASIC

Page 14: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

14

Procedural Languages

Page 15: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

15

Questions

As useful as internet is to us, the following enumerates the disadvantages except

A) Lack of data security B)It can become addictive C)Research information for school projects and business D)Moral abuse through pornographic pictures

ISP stands for -------- A) Internetwork Standard Procedure

B) Intranet Secure Protocol C) Intranet Service Provider

D) Internet Service Provider

Page 16: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

16

The Key Software Trend: Object-Oriented programming

� object oriented programming is a programming language model organized around objects rather than actions and around data rather than logic. Object is best described as its own class in programming and it has two key areas, state and behaviour. Oriented programming is also a type of programming, not only does it describe the database of a data structure but it also describes the types of operations. Most languages are actually developed using the OOP concept. C++ and Java are examples of OOP

Page 17: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

17

Oriented programming

� Objects

� Reusable software components that model items in the real world

� Meaningful software units e.g ui portal � Student objects, course objects, objects, invoice objects, audio

objects, video objects, file objectsetc.

� Any noun can be represented as an object

� Very reusable

� More understandable, better organized, and easier to maintain than procedural programming

� Favor modularity

17

Page 18: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

18

OOP

� Student portal

� Student is an object – attributes are name, matno, dob

� Examples: Java, C++, and Python

Page 19: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

19

The Process Of Creating A

Computer Program

Program Creation

• A person (programmer) writes a

computer program (series of

instructions).

• The program is written and

saved using a text editor.

• The instructions in the

programming language are high

level (look much like a human

language).

Translation

• A special computer program (translator) translates

the program that was just written by the

programmer into the only form that the computer

can understand (machine language/binary)

Execution

• The machine language

instructions can now be

directly executed by the

computer.

Page 20: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

20

Recap: Software development method

1. Specification of needs/requirements specification

2. Problem analysis

3. Design and algorithmic representation

4. Implementation

5. Testing and verification

6. Documentation

Page 21: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

21 21

The Software Development Process

� Implement the Design

� Translate the design into a computer language.

� In this course we will use Python.

Page 22: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

22 22

Debug the Program

� Test/Debug the Program

� Try out your program to see if it worked.

� If there are any errors (bugs), they need to be located and fixed. This process is called debugging.

� Your goal is to find errors, so try everything that might “break” your program! Hacking

Competition

� Types of Programming Errors

Page 23: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

23

Types Of Programming Errors

� 1.Syntax/translation errors

� 2.Runtime errors

� 3.Logic errors

Page 24: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

24

Syntax/ Translation Errors

� 1.Syntax/ Translation Errors

� •Each language has rules about how statements are to be structured.

� •An English sentence is structured by the grammar of the English language:

� -The cat sleeps the sofa.

� Grammatically incorrect:

a preposition is missing . --the sofa‟.

Begin

if score =‘80’

Then print grade=‘7’

end

Page 25: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

25

Runtime Errors

Occur as a program is executing (running).

� •The syntax of the language has not been violated (each statement follows the rules/syntax).

� •During execution a serious error is encountered that causes the execution (running) of the program to cease.

� •With a language like Python where translation occurs just before execution (interpreted) the timing of when runtime errors appear won‟t seem different from a syntax error.

� •But for languages where translation occurs well before execution (compiled) the difference will be quite noticeable.

� •A common example of a runtime error is a division by zero error.

Page 26: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

26

Logic Errors

� •The program has no syntax errors.

� •The program runs from beginning to end with no runtime errors.

� •But the logic of the program is incorrect (it doesn‟t do what it‟s supposed to and may produce an incorrect result).

� •Program name: error_logic.py

� print ("This program will calculate the area of a rectangle“)

� length = int(input("Enter the length: "))

� width = int(input("Enter the width: "))

� area = length + width

� print ("Area: ", area)

Page 27: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

27

Components of Programs: Functions Vs Procedures

Usually we want to execute several statements together

that solve a common problem. One way to do this is to

use a functions or procedures.

Put the statements in a function and call the function in

a main program to use it

Functions must return a value, procedures are called to

execute a series of instructions, they may not return a

value.

Each programing language has a syntax to implement

functions and procedures

Page 28: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

28

Variables

� Set aside a location in memory.

� Used to store information (temporary).

� This location can store one ‘piece’ of

information.

� At most the information will be accessible as

long as the program runs.

� Some of the types of information which can be

stored in variables: studentname=‘Bola’

� DOB = 10/10/2010

Page 29: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

29

Variable Naming Conventions

� Style requirement: The name should be meaningful.

� Style and Python requirement: Names must start with a

letter (Python requirement) and should not begin with

an underscore i.e ( _ ) (style requirement).

� Python requirement: Can't be a keyword (see next

slide).

� Style requirement: Names are case sensitive but avoid

distinguishing variable names only by case.

� Style requirement: Variable names should generally be

all lower case.

� Style requirement: For variable names composed of

multiple words separate each word by capitalizing the

Page 30: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

30

Questions

What are the two major categories of software A)Operating and disk operating software B)System and operating software C)Utility and application software D)System and application software

What are the three phases of computer operations? A) Hardware, software, middleware B) Input, processing, output (C) Data , communication, processing D) Analog, Digital, Hybrid

Page 31: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

31

Python: The Basics

Page 32: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

32

History � Python was created by Guido van Rossum in the late 1980’s at

the National Research Institute for Mathematics and Computer Science in the Netherlands.

� Python is derived from many other languages, including Modula-3, C, C++, Algol-68, SmallTalk, and Unix shell and other scripting languages.

� Python is a general purpose interpreted, interactive, object-oriented, high-level programming language.

� Current release version is version 2.6.? available for UNIX, PC and Mac. Version 3 is under development.

32

Page 33: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

33

What is Python

� Python is a high-level, interpreted, interactive and object oriented-scripting language, designed to be highly readable, commonly uses English keywords.

� Python is Interpreted: This means that it is processed at runtime by the interpreter and you do not need to compile your program before executing it. This is similar to PERL and PHP.

� Python is Interactive: This means that you can actually sit at a Python prompt and interact it directly to write your programs.

� Python is Object-Oriented: This means that Python supports Object-Oriented style or technique of programming that encapsulates code within objects.

� Python is Beginner's Language: Python is a great language for the beginner programmers and supports the development of a wide range of applications.

33

Page 34: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

34

Python Features

� Feature highlights include:

� Easy-to-learn: Python has relatively few keywords, simple structure, and a clearly defined syntax. This allows the student to pick up the language in a relatively short period of time.

� Easy-to-read: Python code is clearly defined and if well written visually simple to read and understand.

� Easy-to-maintain: Python's success is that its source code is fairly easy-to-maintain.

� A broad standard library: One of Python's greatest strengths is the bulk of the library is very portable and cross-platform compatible on UNIX, Windows, and Macintosh.

� Interactive Mode: Support for an interactive mode in which you can enter results from a terminal right to the language, allowing interactive testing and debugging of snippets of code.

34

Page 35: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

35

Python Features � Portable: Python can run on a wide variety of hardware

platforms and has the same interface on all platforms.

� Extendable: You can add low-level modules to the Python interpreter. These modules enable programmers to add to or customize their tools to be more efficient.

� Database Aware: Python provides interfaces to all major commercial databases.

� GUI Programming: Python supports GUI applications that can be created and ported to many system calls, libraries, and windows systems, such as Windows MFC, Macintosh, and the X Window system of Unix.

� Scalable: Python provides a better structure and support for large programs than shell scripting.

35

Page 36: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

36

Important Features

� Apart from the above mentioned features, Python has a big list of important structural features that make it an efficient programming tool, few are listed below:

� Built-in high level data types: strings, lists, dictionaries, etc.

� The usual control structures if, if-else, if-elif-else, while plus a powerful (for) iterator.

� It can be used as a scripting language or can be compiled to byte-code for building large applications.

� Supports automatic garbage collection – memory management-it claims memory no longer in use and utilizes it for other processes.

� It can be easily integrated with Fortran, C, C++, CORBA, and Java, etc……

36

Page 37: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

37

Getting Python & Help

� Getting Python:

� The most up-to-date and current source code, binaries, documentation, news, etc. is available at the official website of Python:Python Official Website : http://www.python.org/

� Documentation

� You can download the Python documentation from the following site. The documentation is available in HTML, PDF, and PostScript formats: http://docs.python.org/index.html

� Tutorial

� You should definitely check out the tutorial on the Internet at: http://docs.python.org/tutorial/.

37

Page 38: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

38

Page 39: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

39

Python Identifiers

� A Python identifier is a name used to identify a variable, function, class, module, or other object. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores, and digits (0 to 9).Python does not allow punctuation characters such as @, $, and % within identifiers.

� Python is a case sensitive programming language. Thus Variable and variable are two different identifiers in Python.

39

Page 40: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

40

Variables

� Set aside a location in memory.

� Used to store information (temporary).

� This location can store one ‘piece’ of

information.

� At most the information will be accessible as

long as the program runs.

� Some of the types of information which can be

stored in variables: studentname=‘Bola’

� DOB = 10/10/2010

Page 41: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

41

Variable Naming Conventions

� Style requirement: The name should be meaningful.

� Style and Python requirement: Names must start with a

letter (Python requirement) and should not begin with

an underscore i.e ( _ ) (style requirement).

� Python requirement: Can't be a keyword (see next

slide).

� Style requirement: Names are case sensitive but avoid

distinguishing variable names only by case.

� Style requirement: Variable names should generally be

all lower case.

� Style requirement: For variable names composed of

multiple words separate each word by capitalizing the

Page 42: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

42

Python Reserved Words or keyword

� The following list shows the reserved words in Python. These reserved words may not be used as constant or variable or any other identifier names. Reserved words contain lowercase letters only.

and, exec, not, assert, finally, or, break, for, pass, class, from, print, continue, global, raise, def, if, return, del, import, try, elif, in, while, else, is, with, except, lambda, yield.

42

Page 43: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

43

summary

� General programming concepts

� Programming Paradyms

� Introduction to python

Page 44: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

44

Running Python

� NEXT CLASS WE SHALL RUN OUR FIRST PYTHON PROGRAM!

44

Page 45: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

45

Python Interfaces

� IDLE – Integrated DeveLopment Environment or Integrated Development and Learning Environment- a cross-platform Python development environment

� PythonWin – a Windows only interface to Python

� Python Shell – running 'python' from the Command Line opens this interactive shell

� For this course, we'll use IDLE, but you can try them all and pick a favorite

Page 46: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

46

IDLE – Development Environment

� IDLE helps you program in Python by:

� color-coding your program code

� debugging

� auto-indent

� interactive shell

Page 47: Introduction to Programming · Reusable software components that model items in the real world Meaningful software units e.g ui portal Student objects, course objects, objects, invoice

47

Example Python

� Hello World print “hello world”

� Prints hello world to standard out

� Open IDLE and try it out yourself

� Follow along using IDLE