Invitation to Computer Science 5th Edition Chapter Python Programming in Python
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
Objectives (continued) Managing complexity Object-oriented programming Graphical programming Invitation to Computer Science, 5th Edition
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
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
Figure 1 A Simple Python Program Invitation to Computer Science, 5th Edition
Figure 2 The Program of Figure 1 (line numbers added for reference) Invitation to Computer Science, 5th Edition
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
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
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
Figure 3 Some of the Python Data Types Invitation to Computer Science, 5th Edition
Figure 4 A 4-Element List roster Invitation to Computer Science, 5th Edition
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
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
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
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
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
Figure 5 Sequential Flow of Control Invitation to Computer Science, 5th Edition
Figure 6 Python Comparison Operators Invitation to Computer Science, 5th Edition
Figure 7 Python Boolean Operators Invitation to Computer Science, 5th Edition
Figure 8 Conditional Flow of Control (if-else) Invitation to Computer Science, 5th Edition
Figure 9 If-Else with Empty Else Invitation to Computer Science, 5th Edition
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
Figure 10 The TravelPlanner Program with a Conditional Statement Invitation to Computer Science, 5th Edition
Figure 11 While Loop Invitation to Computer Science, 5th Edition
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
Figure 12 The TravelPlanner Program with Looping Invitation to Computer Science, 5th Edition
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
Figure 13 A Pseudocode Version of the SportsWorld Program Invitation to Computer Science, 5th Edition
Figure 14 The SportsWorld Program Invitation to Computer Science, 5th Edition
Figure 15 A Sample Session Using the Program of Figure 14 Invitation to Computer Science, 5th Edition
Figure 15 A Sample Session Using the Program of Figure 14 (continued) Invitation to Computer Science, 5th Edition
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
Figure 16 Structure Charts Invitation to Computer Science, 5th Edition
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
Figure 17 Structure Chart for the SportsWorld Task Invitation to Computer Science, 5th Edition
Figure 18 A High-Level Modular View of the SportsWorld Program Invitation to Computer Science, 5th Edition
Figure 19 A Modularized SportsWorld Program, Version 1 Invitation to Computer Science, 5th Edition
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
Figure 20 A Modularized SportsWorld Program, Version 2 Invitation to Computer Science, 5th Edition
Figure 21 A Modularized SportsWorld Program, Version 3 Invitation to Computer Science, 5th Edition
Figure 21 A Modularized SportsWorld Program, Version 3 (continued) Invitation to Computer Science, 5th Edition
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
Figure 22 A Modularized SportsWorld Program, Version 4 Invitation to Computer Science, 5th Edition
Figure 22 A Modularized SportsWorld Program, Version 4 (continued) Invitation to Computer Science, 5th Edition
Figure 23 Data Flow in and out of Python Functions Invitation to Computer Science, 5th Edition
Figure 24 Parameter Passing and Return Statements Invitation to Computer Science, 5th Edition
Figure 25 Output from the Program of Figure 24 Invitation to Computer Science, 5th Edition
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
Figure 26 Three Key Elements of OOP Invitation to Computer Science, 5th Edition
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
Figure 27 An Object-Oriented SportsWorld Program Invitation to Computer Science, 5th Edition
Figure 27 An Object-Oriented SportsWorld Program (continued) Invitation to Computer Science, 5th Edition
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
Figure 28 Python Program with Polymorphism and Inheritance Invitation to Computer Science, 5th Edition
Figure 28 A Python Program with Polymorphism and Inheritance (continued) Invitation to Computer Science, 5th Edition
Figure 28 A Python Program with Polymorphism and Inheritance (continued) Invitation to Computer Science, 5th Edition
Figure 29 Figure 29 Output from the Program of Figure 28 Invitation to Computer Science, 5th Edition
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
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
Figure 30 A Hierarchy of Geometric Classes Invitation to Computer Science, 5th Edition
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
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
Figure 31 An Example of the Use of Graphics to Simplify Machine Operation Invitation to Computer Science, 5th Edition
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
Figure 32 Pixel-Numbering System in a Bitmapped Display Invitation to Computer Science, 5th Edition
Figure 33 Display of Information on the Terminal Invitation to Computer Science, 5th Edition
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
Figure 34 Python Program for Graphics Window Invitation to Computer Science, 5th Edition
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
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
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