Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,

Slides:



Advertisements
Similar presentations
Designing a Program & the Java Programming Language
Advertisements

Chapter 3: Modularization
Introduction to Flowcharting
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
Chapter 17 Fundamental Concepts Expressed in JavaScript.
Starting Out with C++, 3 rd Edition 1 Chapter 1. Introduction to Computers and Programming.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition by Tony Gaddis, Judy Walters,
Chapter 2: Input, Processing, and Output
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Programming Logic and Design, Introductory, Fourth Edition1 Understanding Computer Components and Operations (continued) A program must be free of syntax.
Chapter 3 Planning Your Solution
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
Computer Science 101 Introduction to Programming.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 3 Variables, Calculations, and Colors Starting Out with Games.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
Programming Logic and Design Sixth Edition Chapter 2 Working with Data, Creating Modules, and Designing High-Quality Programs.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
INTRODUCTION TO ALGORITHMS PROGRAMMING. Objectives Give a definition of the term algorithm Describe the various parts of the pseudocode algorithm or algorithm.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
PYTHON. Python is a high-level, interpreted, interactive and object- oriented scripting language. Python was designed to be highly readable which uses.
Computer Science 101 Introduction to Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 6 Value-Returning.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
1 Introduction to Flowcharting. 2 Writing a program Defining the problem –Write down what the program will do Planning –Write down the steps, draw a flowchart.
Input, Output, and Processing
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 4 Decision.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Program Development C# Programming January 30, 2007 Professor J. Sciame.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fluency with Information Technology Third Edition by Lawrence Snyder Chapter.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Copyright © 2013 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 17 JavaScript.
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Chapter 2: Input, Processing, and Output Starting Out with Programming Logic & Design by Tony Gaddis (with instructor modifications) 1-1.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Topics Designing a Program Input, Processing, and Output
Chapter 6 JavaScript: Introduction to Scripting
Input and Output Upsorn Praphamontripong CS 1110
Chapter 2: Input, Processing, and Output
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
Introduction to C++ Programming
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 2: Input, Processing, and Output
Presentation transcript:

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input, Processing, and Output

1-2 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Designing a Program Concept: Programs must be carefully designed before they are written. During the design process, programmers use tools such as pseudocode and flowcharts to create models of programs.

1-3 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Designing a Program The Program Development Cycle Design the Program Write the Code Correct Syntax Errors Test the Program Correct Logic Errors Figure 2-1 The program development cycle

1-4 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Designing a Program More About the Design Process The process of designing a program comprises two important steps: 1.Understand the task that the program is to perform 2.Determine the steps that must be taken to perform the task

1-5 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Designing a Program More About the Design Process 1.Understand the task that the program is to perform a.Interview the customer b.Develop the software requirement (a simple task that the program must perform in order to satisfy the customer)

1-6 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Designing a Program More about the design process 2. Determine the steps that must be taken to perform the task. Develop an algorithm, a set of well-defined logical steps that must be taken in order to perform a task. For Example: Write a program to calculate and display the gross pay for an hourly paid employee. 1. Get the number of hours worked. 2. Get the hourly pay rate 3. Multiply the number of hours worked by the hourly pay rate. 4. Display the result of the calculation that was performed in step 3.

1-7 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Designing a Program Pseudocode is an informal language used to develop a program’s design. It has no syntax rules and is not meant to be compiled or executed. For Example: Write a program to calculate and display the gross pay for an hourly paid employee. Input the hours worked Input the hourly pay rate Calculate gross pay as hours worked multiplied by hourly pay rate Display the gross pay

1-8 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Designing a Program Figure 2-2 Flowchart for the pay calculating program A flowchart is a tool that graphically depicts the steps that take place in a program. Types of Symbols: Ovals—appear at the top or bottom of the flowchart, and are called terminal symbols Parallelograms—used as input and output symbols Rectangles—used as processing symbols Arrows—represent the flow of the program

1-9 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Input, Processing, and Output Concept: Input is data that the program receives. When a program receives data, it usually processes it by performing some operation with it. The result of the operation is sent out of the program as output.

1-10 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Computer programs perform the following three steps: 1.Input is received 2.Some process is performed on the input 3.Output is produced 2.2 Input, Processing, and Output Figure 2-3 The input, processing, and output of the pay calculating program 2-10

1-11 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Displaying Output with the print Statement Concept: You use the print statement to display output in a Python program.

1-12 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Displaying Output with the print Statement Program 2-1 (output.py)

1-13 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Displaying Output with the print Statement Strings and String Literals A string is a sequence of characters that is used as data. When a string appears in the code of a program, it is called a string literal. In Python, single-quotes (‘) or double-quotes (“) can be used to enclose string literals.

1-14 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Displaying Output with the print Statement Program 2-3 (apostrophe.py)

1-15 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Displaying Output with the print Statement Program 2-4 (display_quote.py)

1-16 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Comments Concept: Comments are notes of explanations that document lines or sections of a program. Comments are part of the program, but the Python interpreter ignores them. They are intended for people who may be reading the source code.

1-17 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Comments

1-18 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variables Concept: A variable is a name that represents a value stored in the computer’s memory.

1-19 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variables Figure 2-4 The age variable references the value 25

1-20 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variables Program 2-8 (variable_demo2.py) Figure 2-5 Two variables

1-21 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variables

1-22 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variables

1-23 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variables Table 2-1 Sample variable names

1-24 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variables Program 2-9 (variable_demo3.py)

1-25 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variables Program 2-10 (variable_demo4.py) Figure 2-6 Variable reassignment in Program 2-10

1-26 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variables

1-27 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Variables Program 2-11 (string_variable.py)

1-28 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Reading Input from the Keyboard Concept: Programs commonly need to read input typed by the user on the keyboard. We will use the Python functions to do this.

1-29 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Reading Input from the Keyboard

1-30 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Reading Input from the Keyboard

1-31 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-31 Reading Strings with the raw_input Function The raw_input function retrieves all keyboard input as a string. 2.6 Reading Input from the Keyboard Program 2-13 (string_input.py)

1-32 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Performing Calculations Concept: Python has numerous operators that can be used to perform mathematical calculations.

1-33 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Performing Calculations Table 2-2 Python math operators

1-34 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Performing Calculations Program 2-15 (sale_price.py)

1-35 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Performing Calculations For Example: num_add = # the answer will be 22 num_div_int = 3 / 2 # the answer is 1; 0.5 is the fractional part # and it is truncated. num_div_real = 3.0 / 2.0 # the answer is 1.5

1-36 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Performing Calculations 1. Operations that are enclosed in parentheses. 2. Exponentiation ** 3. Multiplication *, division /, and remainder % 4. Addition + and subtraction - Table 2-3 Some expressions

1-37 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Performing Calculations Table 2-4 More expressions and their values

1-38 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Performing Calculations The Exponent and Remainder Operators ** is the exponent operator. Its purpose is to raise a number to a power. For Example: area = length**2 % is the remainder operator. Its purpose is to perform division and return the remainder. For Example: leftover = 17 % 3#remainder is 2

1-39 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Performing Calculations Table 2-6 Algebraic and programming expressions

1-40 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Performing Calculations

1-41 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Performing Calculations

1-42 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Performing Calculations

1-43 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Data Output The print statement displays a string and then prints an unseen newline character. For Example: print ‘One’ print ‘Two’ print ‘Three’ Output: One Two Three If you do not want to start a new line of output, you can use a trailing comma. For Example: print ‘One’, print ‘Two’, print ‘Three’ Output: One Two Three

1-44 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Data Output Table 2-7 Some of Python’s escape characters

1-45 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Data Output When the + is used with two strings, it performs string concatenation. For Example: print ‘this is’ + ‘one string.’ # one string is appended to another Output: This is one string

1-46 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Data Output

1-47 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Data Output Figure 2-8 Using the string format operator Program 2-21 (formatting.py)

1-48 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Data Output

1-49 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Data Output Program 2-23 (columns.py)

1-50 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More About Data Output

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley QUESTIONS ? Chapter 2 Input, Processing, and Output