15.04.2018 Views

programming-for-dummies

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Retrieving Data from a Variable 151<br />

When storing data in variables, make sure the variable is either empty or<br />

contains data you can af<strong>for</strong>d to lose. Variables can hold only one item at a<br />

time, so storing a new value in a variable wipes out the old value, as shown<br />

in Figure 2-4 and in the following BASIC code:<br />

Salary = 25000<br />

Salary = 17000 + 500<br />

PRINT “This is the value of the Salary variable = “, Salary<br />

Salary = 20000<br />

Stores<br />

20000 in the<br />

Salary variable<br />

20000<br />

Salary<br />

variable<br />

Book II<br />

Chapter 2<br />

Salary = 17000 + 500<br />

17500<br />

Salary<br />

variable<br />

Variables,<br />

Data Types,<br />

and Constants<br />

Figure 2-4:<br />

Assigning a<br />

new value<br />

to a variable<br />

wipes out<br />

the old<br />

value.<br />

Adds 17000 + 500 (17500), erases the old value in<br />

the Salary variable (20000), and stores 17500 in the<br />

Salary variable<br />

PRINT “This is the value of the Salary variable = ”, Salary<br />

This is the value of the Salary variable = 17500<br />

If you store a value in a variable and then immediately store a second value<br />

in that same value, you wipe out the first value in that variable. Obviously,<br />

there’s no point in storing data in a variable only to wipe it out later, so when<br />

you store a value in a variable, you’ll eventually want to use that value again.<br />

(Otherwise, there’s no point in storing that data in the first place.)<br />

Retrieving Data from a Variable<br />

After you store data in a variable, you can treat that variable exactly like a<br />

fixed value. In this first Python language example, the print command just<br />

prints, “This is when I plan on retiring = 65”.<br />

print “This is when I plan on retiring = “, 65

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

Saved successfully!

Ooh no, something went wrong!