Starting out with Python - Chapter 2 PPT

Embed Size (px)

Citation preview

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    1/51

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

    STARTING OUT WITH

      ythonython

    First Edition

    by

    Tony Gaddis

    Chapter 2Input, Processing, and Output

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    2/51

    1-2

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-2

    2.1 Designing a Program

    Concept:

    Programs must be carefullydesigned before they are written.During the design process,programmers use tools such aspseudocode and owcharts to

    create models of programs.

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    3/51

    1-3

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-3

    2.1 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

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    4/51

    1-4

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-4

    2.1 Designing a Program

    More About the Design Process

    The rocess o! designing a rogram comrises t"oimortant stes#

    1.$nderstand the tas% that the rogram is to er!orm

    2.Determine the stes that must be ta%en to er!orm

    the tas%

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    5/51

    1-5

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-5

    2.1 Designing a Program

    More About the Design Process

    1.$nderstand the tas% that the rogram is toer!orm

    a. &nter'ie" the customer 

    b. De'e(o the software requirement (a simple task that

    the program must perform in order to satisfy the ustomer!

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    6/51

    1-6

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-6

    2.1 Designing a Program

    More about the design process

    2. Determine the stes that must be ta%en to er!orm

    the tas%.

    •  De'e(o an a(gorithm) a set o! "e((-de!ined (ogica( stes

    that must be ta%en in order to er!orm a tas%.

    For Exam(e#

    Write a rogram to ca(cu(ate and dis(ay the gross ay !or an hour(y

    aid em(oyee.

    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.

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    7/51

    1-7

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-7

    2.1 Designing a Program

    Pseudocode is an in!orma( (anguage used to de'e(o a

    rogram*s design. &t has no syntax ru(es and is not

    meant to be comi(ed or executed.

    For Exam(e#

    Write a rogram to ca(cu(ate and dis(ay the gross ay !or an hour(y aid

    em(oyee.

    Input the hours worked 

    Input the hourly pay rate

    alculate !ross pay as hours worked multiplied by hourly pay rateDisplay the !ross pay 

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    8/51

    1-8

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-8

    2.1 Designing a Program

    Figure 2-2 Flowchart for the pay

    calculatng program

    + !(o"chart is a too( that grahica((y

    deicts the stes that ta%e (ace in

    a rogram.

    Tyes o! Symbo(s#

    •,'a(saear at the to or bottom

    o! the !(o"chart) and are ca((ed

    termina( symbo(s

    •Para((e(ogramsused as inut and

    outut symbo(s

    •ectang(esused as rocessingsymbo(s

    •+rro"sreresent the !(o" o! the

    rogram

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    9/51

    1-!

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-!

    2.2 &nut) Processing) and ,utut

    Concept:

    Input is data that the program

    receives. When a programreceives data, it usuallyprocesses it by performing someoperation with it. The result ofthe operation is sent out of theprogram as output.

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    10/51

    1-1"

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

    Computer programs perform the following

    three steps:

    1.&nut is recei'ed

    2.Some rocess is er!ormed on the inut

    /.,utut is roduced

    2.2 &nut) Processing) and ,utut

    Figure 2-/ The nput#proce$$ng# and output of

    the pay calculatng program

    2-1"

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    11/51

    1-11

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-11

    2./ Dis(aying ,utut "ith the print 

    Statement

    Concept:

     You use the  print  statementto display output in a Pythonprogram.

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    12/51

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    13/51

    1-13

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-13

    2./ Dis(aying ,utut "ith the print 

    Statement

    Strings and String iterals

    • + string is a se0uence o! characters that is used as

    data.

    • When a string aears in the code o! a rogram) it

    is ca((ed a string (itera(.

    • &n Python) sing(e-0uotes 3 or doub(e-0uotes 43

    can be used to enc(ose string (itera(s.

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    14/51

    1-14

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-14

    2./ Dis(aying ,utut "ith the print 

    Statement

    Program 2-/ %apo$trophe&py'

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    15/51

    1-15

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-15

    2./ Dis(aying ,utut "ith the print 

    Statement

    Program 2-5 %d$play()uote&py'

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    16/51

    1-16

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-16

    2.5 Comments

    Concept:

    omments are notes of

    e!planations that document linesor sections of a program.omments are part of theprogram, but the Pythoninterpreter ignores them. Theyare intended for people who maybe reading the source code.

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    17/51

    1-17

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

    2.5 Comments

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    18/51

    1-18

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-18

    2.6 7ariab(es

    Concept:

    " variable is a name thatrepresents a value stored in the

    computer#s memory.

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    19/51

    1-1!

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12-1!

    2.6 7ariab(es

    Figure 2-5 The age vara*le

    reference$ the value 25

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    20/51

    1-2"

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-2"

    2.6 7ariab(es

    Program 2-8

    %vara*le(demo2&py'

    Figure 2-6

    Two vara*le$

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    21/51

    1-21

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

    2.6 7ariab(es

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    22/51

    1-22

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

    2.6 7ariab(es

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    23/51

    1-23

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-23

    2.6 7ariab(es

    Tab(e 2-1 +ample vara*le name$

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    24/51

    1-24

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-24

    2.6 7ariab(es

    Program 2-9 %vara*le(demo3&py'

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    25/51

    1-25

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-25

    2.6 7ariab(es

    Program 2-1:%vara*le(demo4&py'

    Figure 2-; ,ara*lerea$$gnment n

    rogram 2-1"

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    26/51

    1-26

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

    2.6 7ariab(es

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    27/51

    1-27

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-27

    2.6 7ariab(es

    Program 2-11 %$trng(vara*le&py'

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    28/51

    1-28

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-28

    2.; eading &nut !rom the

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    29/51

    1-2!

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-2!

    2.; eading &nut !rom the

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    30/51

    1-3"

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-3"

    2.; eading &nut !rom the

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    31/51

    1-31

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-31

    !eading Strings with the raw_input "unction

    The raw_input functon retreve$ all .ey*oard nput a$ a

    $trng&

    2.; eading &nut !rom the

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    32/51

    1-32

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-32

    2.= Per!orming Ca(cu(ations

    Concept:

    Python has numerous operatorsthat can be used to perform

    mathematical calculations.

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    33/51

    1-33

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-33

    2.= Per!orming Ca(cu(ations

    Tab(e 2-2 ython math operator$

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    34/51

    1-34

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-34

    2.= Per!orming Ca(cu(ations

    Program 2-16%$ale(prce&py'

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    35/51

    1-35

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-35

    2.= Per!orming Ca(cu(ations

    For Example:num_add = 17 + 5 # the answer will be22num_div_int = 3 2 # the answer is 1! "5is the $ra%tional part  # and it is trun%atednum_div_real = 3" 2" # the answer is

    15

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    36/51

    1-36

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-36

    2.= Per!orming Ca(cu(ations

    1 &perations that are en%losed inparentheses

    2 Exponentiation ''3 (ultipli%ation ') division ) and remainder

    * ,ddition + and subtra%tion -Tab(e 2-/ +ome e/pre$$on$

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    37/51

    1-37

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-37

    2.= Per!orming Ca(cu(ations

    Tab(e 2-5 0ore e/pre$$on$ and ther value$

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    38/51

    1-38

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-38

    2.= Per!orming Ca(cu(ations

    The #$ponent and !emainder %perators>> is the exonent oerator. &ts urose is to raise a

    number to a o"er.

    For Exam(e#

    area ? (ength>>2

    @ is the remainder oerator. &ts urose is to er!orm

    di'ision and return the remainder.

    For Exam(e#

    (e!to'er ? 1= @ / Aremainder is 2

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    39/51

    1-3!

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-3!

    2.= Per!orming Ca(cu(ations

    Tab(e 2-;  lge*rac and programmng e/pre$$on$

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    40/51

    1-4"

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-4"

    2.= Per!orming Ca(cu(ations

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    41/51

    1-41

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-41

    2.= Per!orming Ca(cu(ations

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    42/51

    1-42

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-42

    2.= Per!orming Ca(cu(ations

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    43/51

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    44/51

    1-44

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-44

    2.8 Bore +bout Data ,utut

    Tab(e 2-= +ome of ython$ e$cape character$

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    45/51

    1-45

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-45

    2.8 Bore +bout Data ,utut

    When the is used "ith t"o strings) it er!orms

    string concatenation.

    For Exam(e#

      rint this is* one string.* A one string is aended to another 

      ,utut#

      This is one string

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    46/51

    1-46

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-46

    2.8 Bore +bout Data ,utut

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    47/51

    1-47

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-47

    2.8 Bore +bout Data ,utut

    Figure 2-8 $ng the $trng format operator 

    Program 2-21 %formattng&py'

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    48/51

    1-48

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-48

    2.8 Bore +bout Data ,utut

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    49/51

    1-4!

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-4!

    2.8 Bore +bout Data ,utut

    Program 2-2/ %column$&py'

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    50/51

    1-5"

    Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-5"

    2.8 Bore +bout Data ,utut

  • 8/20/2019 Starting out with Python - Chapter 2 PPT

    51/51

    &'#ST(%)S

    *

    Chapter 2Input, Processing, and Output