Presentation is loading. Please wait.

Presentation is loading. Please wait.

Invitation to Computer Science 5th Edition

Similar presentations


Presentation on theme: "Invitation to Computer Science 5th Edition"— Presentation transcript:

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

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 Invitation to Computer Science, 5th Edition

3 Objectives (continued)
Managing complexity Object-oriented programming Graphical programming Invitation to Computer Science, 5th Edition

4 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 4

5 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 5

6 Figure 1 A Simple Python Program
Invitation to Computer Science, 5th Edition

7 Figure 2 The Program of Figure 1 (line numbers added for reference)
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 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 Invitation to Computer Science, 5th Edition

10 Virtual Data Storage Identifiers Constants Variables
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 Invitation to Computer Science, 5th Edition

11 Figure 3 Some of the Python Data Types
Invitation to Computer Science, 5th Edition

12 Figure 4 A 4-Element List roster
Invitation to Computer Science, 5th Edition

13 Statement Types Input statement Output statement Assignment 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 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 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 Invitation to Computer Science, 5th Edition

16 The Assignment Statement
Pseudocode operation Set the value of “variable” to “arithmetic expression” Python equivalent variable = expression Basic arithmetic operations + Addition - Subtraction * Multiplication / Division Invitation to Computer Science, 5th Edition

17 Control Statements Control mechanisms Boolean condition
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 Invitation to Computer Science, 5th Edition

18 Figure 5 Sequential Flow of Control
Invitation to Computer Science, 5th Edition

19 Figure 6 Python Comparison Operators
Invitation to Computer Science, 5th Edition

20 Figure 7 Python Boolean Operators
Invitation to Computer Science, 5th Edition

21 Figure 8 Conditional Flow of Control (if-else)
Invitation to Computer Science, 5th Edition

22 Figure 9 If-Else with Empty Else
Invitation to Computer Science, 5th Edition

23 Control Statements (continued)
Compound statement Can be used anywhere a single statement is allowed Block example if 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

24 Figure 10 The TravelPlanner Program with a Conditional Statement
Invitation to Computer Science, 5th Edition

25 Figure 11 While Loop Invitation to Computer Science, 5th Edition

26 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

27 Figure 12 The TravelPlanner Program with Looping
Invitation to Computer Science, 5th Edition

28 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

29 Figure 13 A Pseudocode Version of the SportsWorld Program
Invitation to Computer Science, 5th Edition

30 Figure 14 The SportsWorld Program
Invitation to Computer Science, 5th Edition

31 Figure 15 A Sample Session Using the Program of Figure 14
Invitation to Computer Science, 5th Edition

32 Figure 15 A Sample Session Using the Program of Figure 14 (continued)
Invitation to Computer Science, 5th Edition

33 Managing Complexity Divide and conquer Figure 16(a)
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

34 Figure 16 Structure Charts
Invitation to Computer Science, 5th Edition

35 Using and Writing 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

36 Figure 17 Structure Chart for the SportsWorld Task
Invitation to Computer Science, 5th Edition

37 Figure 18 A High-Level Modular View of the SportsWorld Program
Invitation to Computer Science, 5th Edition

38 Figure 19 A Modularized SportsWorld Program, Version 1
Invitation to Computer Science, 5th Edition

39 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

40 Figure 20 A Modularized SportsWorld Program, Version 2
Invitation to Computer Science, 5th Edition

41 Figure 21 A Modularized SportsWorld Program, Version 3
Invitation to Computer Science, 5th Edition

42 Figure 21 A Modularized SportsWorld Program, Version 3 (continued)
Invitation to Computer Science, 5th Edition

43 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

44 Figure 22 A Modularized SportsWorld Program, Version 4
Invitation to Computer Science, 5th Edition

45 Figure 22 A Modularized SportsWorld Program, Version 4 (continued)
Invitation to Computer Science, 5th Edition

46 Figure 23 Data Flow in and out of Python Functions
Invitation to Computer Science, 5th Edition

47 Figure 24 Parameter Passing and Return Statements
Invitation to Computer Science, 5th Edition

48 Figure 25 Output from the Program of Figure 24
Invitation to Computer Science, 5th Edition

49 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

50 Figure 26 Three Key Elements of OOP
Invitation to Computer Science, 5th Edition

51 Python and OOP Methods Objects
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

52 Figure 27 An Object-Oriented SportsWorld Program
Invitation to Computer Science, 5th Edition

53 Figure 27 An Object-Oriented SportsWorld Program (continued)
Invitation to Computer Science, 5th Edition

54 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

55 Figure 28 Python Program with Polymorphism and Inheritance
Invitation to Computer Science, 5th Edition

56 Figure 28 A Python Program with Polymorphism and Inheritance
(continued) Invitation to Computer Science, 5th Edition

57 Figure 28 A Python Program with Polymorphism and Inheritance (continued)
Invitation to Computer Science, 5th Edition

58 Figure 29 Figure 29 Output from the Program of Figure 28
Invitation to Computer Science, 5th Edition

59 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

60 What Have We Gained? Reasons why OOP is a popular way to program
Software reuse A more natural “worldview” Useful class that has been implemented and tested becomes a component available for use in future software development Invitation to Computer Science, 5th Edition

61 Figure 30 A Hierarchy of Geometric Classes
Invitation to Computer Science, 5th Edition

62 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

63 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

64 Figure 31 An Example of the Use of Graphics to Simplify Machine Operation
Invitation to Computer Science, 5th Edition

65 Graphics Hardware Bitmapped display High-resolution terminals
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

66 Figure 32 Pixel-Numbering System in a Bitmapped Display
Invitation to Computer Science, 5th Edition

67 Figure 33 Display of Information on the Terminal
Invitation to Computer Science, 5th Edition

68 Graphics Software Graphics library Figure 34
Collection of software routines that are part of a special package Figure 34 Shows the complete Python program Invitation to Computer Science, 5th Edition

69 Figure 34 Python Program for Graphics Window
Invitation to Computer Science, 5th Edition

70 Summary Prologue comment Syntax Creating and running a Python program
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

71 Summary (continued) Input statement Output statement Escape sequence
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

72 Summary (continued) Control mechanisms Compound statement
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


Download ppt "Invitation to Computer Science 5th Edition"

Similar presentations


Ads by Google