27.07.2013 Views

Deitel - Python, How To Program.pdf

Deitel - Python, How To Program.pdf

Deitel - Python, How To Program.pdf

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

pythonhtp1_02.fm Page 43 Wednesday, December 12, 2001 12:12 PM<br />

Chapter 2 Introduction to <strong>Python</strong> <strong>Program</strong>ming 43<br />

integer1 "45"<br />

Fig. Fig. 2.9 2.9 Memory location showing value of a variable and the name bound to<br />

the value.<br />

integer1<br />

Fig. Fig. 2.10 2.10 Memory location showing the name and value of a variable.<br />

Returning to our addition program, when the statements<br />

integer2 = raw_input( "Enter second integer:\n" )<br />

integer2 = int( integer2 )<br />

execute, suppose the user enters the string "72". After the program converts this value to<br />

the integer value 72 and places the value into a memory location to which integer2 is<br />

bound, memory appears as in Fig. 2.11. Note that the locations of these objects are not necessarily<br />

adjacent in memory.<br />

Once the program has obtained values for integer1 and integer2, the program<br />

adds these values and assigns the sum to variable sum. After the statement<br />

sum = integer1 + integer2<br />

"45"<br />

performs the addition, memory appears as in Fig. 2.12. Note that the values of integer1<br />

and integer2 appear exactly as they did before they were used in the calculation of<br />

sum. These values were used, but not modified, as the computer performed the calculation.<br />

Thus, when a value is read out of a memory location, the value is not changed.<br />

Fig. Fig. 2.11 2.11 Memory locations after values for two variables have been input.<br />

45<br />

integer1 45<br />

integer2 72

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!