72
Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Embed Size (px)

Citation preview

Page 1: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science 5th Edition

Chapter Python

Programming in Python

Page 2: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 2

Objectives

In this chapter, you will learn about:

• Creating and running a simple Python program

• Virtual data storage

• Statement types

• An example of a complete program

Page 3: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 3

Objectives (continued)

• Managing complexity

• Object-oriented programming

• Graphical programming

Page 4: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Introduction to Python

• In this chapter:– You will get a sense of what programming in a high-

level language is like

Invitation to Computer Science, 5th Edition 44

Page 5: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

A Simple Python Program

• Comments– Anything appearing on a line after the double slash

symbol (//)– Ignored by the compiler

• Prologue comment– Introductory comment

• Blank lines in Python programs– Ignored and used like comments

Invitation to Computer Science, 5th Edition 55

Page 6: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 6

Figure 1 A Simple Python Program

Page 7: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 7

Figure 2 The Program of Figure 1 (line numbers added for reference)

Page 8: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 8

A Simple Python Program (continued)

• Syntax– The correct form for each component of the

language

• Case-sensitive language– Uppercase letters are distinguished from lowercase

letters, and the instruction is print, not Print

Page 9: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 9

Creating and Running a Python Program

• First step – Type the program into a text editor

• Second step– Execute the program

• Integrated Development Environment (IDE)– Lets the programmer perform a number of tasks

within the shell of a single application program– Usually has a GUI (graphical user interface) with

menu choices for the different task

Page 10: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 10

Virtual Data Storage

• Identifiers– Names in a programming language– Cannot be one of the few words, such as “while,”

that have a special meaning in Python

• Constants– Values are fixed and known ahead of time

• Variables– Values that change as the program executes

Page 11: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 11

Figure 3 Some of the Python Data Types

Page 12: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 12

Figure 4 A 4-Element List roster

Page 13: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 13

Statement Types

• Input statement – Collects a specific value from the user for a variable

within the program

• Output statement – Writes a message or the value of a program variable

to the user’s screen

• Assignment statement– Assigns a value to a program variable

Page 14: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 14

Statement Types (continued)

• Control statements– Affect the order in which instructions are executed

• Flow of control in the program– The path through the program that is traced by

following the currently executing statement

Page 15: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 15

Input/Output Statements

• User prompt– Alerts the user that the program is waiting for some

input

• Escape sequence – Consists of a backslash (\) followed by a single

character

• Concatenation operator– Represented by a + sign

Page 16: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 16

The Assignment Statement

• Pseudocode operationSet the value of “variable” to “arithmetic expression”

– Python equivalent

variable = expression

• Basic arithmetic operations+ Addition

- Subtraction

* Multiplication

/ Division

Page 17: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 17

Control Statements

• Control mechanisms– Sequential: instructions are executed in order– Conditional: which instruction executes next

depends on some condition– Looping: a group of instructions may be executed

many times

• Boolean condition– Can be either true or false– Often involves comparing the values of two

expressions and determining whether they are equal

Page 18: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 18

Figure 5 Sequential Flow of Control

Page 19: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 19

Figure 6 Python Comparison Operators

Page 20: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 20

Figure 7 Python Boolean Operators

Page 21: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 21

Figure 8 Conditional Flow ofControl (if-else)

Page 22: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 22

Figure 9 If-Else with Empty Else

Page 23: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Control Statements (continued)

• Compound statement – Can be used anywhere a single statement is allowed

• Block exampleif snack == “pb & j”:

print(“yummy”)

print(“sticky”)

print(“gooey”)

else:print(“Must be pizza”)

print(“That‘s All, Folks”)

Invitation to Computer Science, 5th Edition 23

Page 24: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 24

Figure 10 The TravelPlanner Program with a Conditional Statement

Page 25: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 25

Figure 11 While Loop

Page 26: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Control Statements (continued)

• Sentinel value– One extra integer that is not part of the legitimate

data but is instead a signal that there are no more data

• Infinite loop– The condition, once true, would remain true forever,

and the loop body would be endlessly executed

Invitation to Computer Science, 5th Edition 26

Page 27: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 27

Figure 12 The TravelPlanner Program with Looping

Page 28: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Another Example

• Module – Collection of useful code that you can make

available to your Python program by using the import statement

Invitation to Computer Science, 5th Edition 28

Page 29: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 29

Figure 13 A Pseudocode Version of the SportsWorld Program

Page 30: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 30

Figure 14 The SportsWorld Program

Page 31: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 31

Figure 15 A Sample Session Using the Program of Figure 14

Page 32: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 32

Figure 15 A Sample Session Using the Program of Figure 14 (continued)

Page 33: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Managing Complexity

• Divide and conquer – Problem-solving approach and not just a computer

programming technique

• Figure 16(a)– An example of a structure chart (structure

diagram)

Invitation to Computer Science, 5th Edition 33

Page 34: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 34

Figure 16 Structure Charts

Page 35: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Using and Writing Functions

• Functions– Each function in a program should do one and only

one subtask

• A simple function in Python has the following form:

def function identifier():

body of the function

Invitation to Computer Science, 5th Edition 35

Page 36: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 36

Figure 17 Structure Chart for the SportsWorld Task

Page 37: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 37

Figure 18 A High-Level Modular View of the SportsWorld Program

Page 38: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 38

Figure 19 A Modularized SportsWorld Program, Version 1

Page 39: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Using and Writing Functions (continued)

• Global variable– Known throughout the program

• Local variable– Variable created within a function– Not known anywhere else in the program

Invitation to Computer Science, 5th Edition 39

Page 40: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 40

Figure 20 A Modularized SportsWorldProgram, Version 2

Page 41: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 41

Figure 21 A Modularized SportsWorld Program, Version 3

Page 42: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 42

Figure 21 A Modularized SportsWorld Program, Version 3 (continued)

Page 43: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Writing Functions (continued)

• Return statement with an empty expression list – Would simply cause an exit from the function in

which it appears

• Argument list – Will pass values to the function that are pertinent to

that function’s task

• Parameter list– List of variables local to the function that will receive

their values

Invitation to Computer Science, 5th Edition 43

Page 44: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 44

Figure 22 A Modularized SportsWorld Program, Version 4

Page 45: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 45

Figure 22 A Modularized SportsWorld Program, Version 4 (continued)

Page 46: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 46

Figure 23 Data Flow in and out of Python Functions

Page 47: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 47

Figure 24 Parameter Passing and Return Statements

Page 48: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 48

Figure 25 Output from the Program of Figure 24

Page 49: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Object-Oriented Programming

• A program is considered a simulation of some part of the world that is the domain of interest– “Objects” populate this domain

• Terms associated with object-oriented programming– Encapsulation– Inheritance– Polymorphism

Invitation to Computer Science, 5th Edition 49

Page 50: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 50

Figure 26 Three Key Elements of OOP

Page 51: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Python and OOP

• Methods– Functions associated with a class

• Objects– Instances of classes

• Class definition in Python has the following form: class class_identifier:

body of the class

Invitation to Computer Science, 5th Edition 51

Page 52: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 52

Figure 27 An Object-Oriented SportsWorld Program

Page 53: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 53

Figure 27 An Object-Oriented SportsWorld Program (continued)

Page 54: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

One More Example

• In Figure 28– The Circle object has a radius property– The Rectangle object has a width attribute and a

height attribute – Any Circle object can set the value of its radius and

can compute its area– A Square object has a side property– The Square2 object doesn’t have any attributes or

any way to compute its area

Invitation to Computer Science, 5th Edition 54

Page 55: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 55

Figure 28 Python Program with Polymorphism and Inheritance

Page 56: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 56

Figure 28 A Python Program withPolymorphism and Inheritance(continued)

Page 57: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 57

Figure 28 A Python Program with Polymorphism and Inheritance (continued)

Page 58: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 58

Figure 29 Output from the Program of Figure 28Figure 29

Page 59: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

One More Example (continued)

• Square class– Stand-alone class with a side property and a doArea

function

• Square2 class– Recognizes the fact that squares are special kinds of

rectangles– Subclass of the Rectangle class– Inherits the setWidth, setHeight, getWidth,

getHeight, and doArea methods

Invitation to Computer Science, 5th Edition 59

Page 60: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

What Have We Gained?

• Reasons why OOP is a popular way to program– Software reuse– A more natural “worldview”

• Software reuse – Useful class that has been implemented and tested

becomes a component available for use in future software development

Invitation to Computer Science, 5th Edition 60

Page 61: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 61

Figure 30 A Hierarchy of Geometric Classes

Page 62: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

A More “Natural” Worldview

• Object-oriented programming– Recognizes that in the “real world,” tasks are done

by entities (objects)– Allows the programmer to come closer to modeling

or simulating the world as we see it

Invitation to Computer Science, 5th Edition 62

Page 63: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Graphical Programming

• Graphics – Make it easier to manage tasks of the operating

system– Can help us visualize and make sense of massive

amounts of output produced by programs that model complex physical, social, and mathematical systems

Invitation to Computer Science, 5th Edition 63

Page 64: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 64

Figure 31 An Example of the Use of Graphics to Simplify Machine Operation

Page 65: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Graphics Hardware

• Bitmapped display– Screen is made up of thousands of individual picture

elements or pixels, laid out in a two-dimensional grid

• High-resolution terminals– Terminals with a high density of pixels

• Frame buffer– Memory that stores the actual screen image

Invitation to Computer Science, 5th Edition 65

Page 66: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 66

Figure 32 Pixel-Numbering System in a Bitmapped Display

Page 67: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 67

Figure 33 Display of Information on the Terminal

Page 68: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Graphics Software

• Graphics library– Collection of software routines that are part of a

special package

• Figure 34 – Shows the complete Python program

Invitation to Computer Science, 5th Edition 68

Page 69: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Invitation to Computer Science, 5th Edition 69

Figure 34 Python Program for Graphics Window

Page 70: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Summary

• Prologue comment– Introductory comment

• Syntax– The correct form for each component of the

language• Creating and running a Python program

– First step: type the program into a text editor– Second step: to execute the program

• Identifiers– Cannot be one of the few words, such as “while,”

that have a special meaning in Python

Invitation to Computer Science, 5th Edition 70

Page 71: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Summary (continued)

• Input statement – Collects a specific value from the user for a variable

within the program

• Output statement – Writes a message or the value of a program variable

to the user’s screen

• Escape sequence – Consists of a backslash (\) followed by a single

character

Invitation to Computer Science, 5th Edition 71

Page 72: Invitation to Computer Science 5 th Edition Chapter Python Programming in Python

Summary (continued)

• Control mechanisms– Sequential, conditional, and looping

• Compound statement – Can be used anywhere a single statement is allowed

• Terms associated with object-oriented programming– Encapsulation, inheritance, and polymorphism

Invitation to Computer Science, 5th Edition 72