Invitation to Computer Science 5th Edition

Slides:



Advertisements
Similar presentations
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Advertisements

Programming Logic and Design Fourth Edition, Introductory
CHAPTER 1: AN OVERVIEW OF COMPUTERS AND LOGIC. Objectives 2  Understand computer components and operations  Describe the steps involved in the programming.
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
High-Level Programming Languages
Chapter 8: Introduction to High-Level Language Programming Invitation to Computer Science, Java Version, Third Edition.
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
Chapter 8 High-Level Programming Languages Nell Dale John Lewis.
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Introduction to C Programming
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Chapter 8: Introduction to High-Level Language Programming Invitation to Computer Science, C++ Version, Fourth Edition.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Fundamentals of Python: From First Programs Through Data Structures
COMPUTER SCIENCE I C++ INTRODUCTION
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
Chapter 2 Classes and Methods I Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
A First Program Using C#
Fundamentals of Python: First Programs
Chapter 8 High-Level Programming Languages (modified by Erin Chambers)
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
High-Level Programming Languages: C++
Microsoft Visual Basic 2005: Reloaded Second Edition
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
Invitation to Computer Science 5 th Edition Chapter C# Programming in C#
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
Input, Output, and Processing
Java Programming, Second Edition Chapter One Creating Your First Java Program.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Invitation to Computer Science 5 th Edition Chapter C++ Programming in C++
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Pseudocode. Simple Program Design, Fourth Edition Chapter 2 2 Objectives In this chapter you will be able to: Introduce common words, keywords, and meaningful.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Chapter 8: Introduction to High-level Language Programming Invitation to Computer Science, C++ Version, Third Edition.
Chapter 8: Introduction to High-Level Language Programming Invitation to Computer Science, Java Version, Third Edition.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
8-1 Compilers Compiler A program that translates a high-level language program into machine code High-level languages provide a richer set of instructions.
Chapter 8 High-Level Programming Languages. 2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Introduction to OOP CPS235: Introduction.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Chapter 7 Introduction to High-Level Language Programming.
Invitation to Computer Science 5 th Edition Chapter Ada Programming in Ada.
Programming Logic and Design Seventh Edition Chapter 1 An Overview of Computers and Programming.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Fundamentals of Programming I Overview of Programming
Topics Introduction to Functions Defining and Calling a Void Function
Introduction to the C Language
Chapter 3 Syntax, Errors, and Debugging
The Selection Structure
Chapter 8: Introduction to High-Level Language Programming
Introduction to the C Language
Introduction to C++ Programming
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Presentation transcript:

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