Practical programming for the normal person: Variables

Syda Productions/Shutterstock

Practical programming for the normal person: Variables

By Benjamin Williams, KSL.com Contributor | Posted - May 7, 2015 at 11:45 a.m.



This archived news story is available only for your personal, non-commercial use. Information in the story may be outdated or superseded by additional information. Reading or replaying the story in its archived form does not constitute a republication of the story.

SALT LAKE CITY — Now that we know how to use Python as a basic calculator (see [last week's article](http://www.ksl.com/?nid=210&sid=34330950) if you missed it), we are ready to learn how to do things that most calculators cannot do — or at least cannot do easily.

If you remember high school algebra, this will be especially easy. Those who want a refresher course could use the free online Khan Academy lesson.

Most hand held calculators have some kind of memory. A button marked "M+" is used to store the currently displayed number and a button marked "MR" is used to retrieve it. Many calculators also have additional memory functions. More advanced calculators can store multiple values. Python can store values as well, and it is more flexible and easy to use than most calculators.

First in the series

Start by opening a terminal and running Python. If you recall algebra, you will remember unknowns are typically represented by letters (most frequently x). Unknowns could be used literally as placeholders for unknown values, or they could be used as variable values in a formula.

We can use unknowns as variables in Python, but we cannot use them as literal unknowns. Before we can use a variable, we have to assign it a value. To do this, use the equal sign. Variable names can have letters, numbers, and underscores, but they cannot start with numbers.

For now, we will use x as our variable name. To assign a value to x, type in x = 12. Press return, and now you can use x as the value 12 in any math equation. Try running x + 2, and Python will display the result 14. If you run x - 4, it will display 8. You can also change the value of x by assigning it a different value, like x = 4.

Variables are valuable tools for many tasks. When you need to store a number from one calculation to use in another one, you can assign the result to a variable.

For example, maybe you need to calculate the unit price of a product. A 32-ounce soft drink might cost $1.50, so the unit price would be 1.50 / 32 dollars per ounce. Now, maybe you want to find out what you would be saving buying a 48 ounce soft drink that costs $1.80. You can start by running x = 1.50 / 32. Python will not automatically display the result, but you can make it display it by running only x or print x. The variable x now contains the cost per ounce of a 32 ounce soft drink.


Watch for part three of our Practical Programming series to go online next Thursday.

You can calculate what a 48-ounce drink would cost at that price by running 48 * x. The result is 2.25. Now you can run 2.25 - 1.80 to find the difference. It turns out the 48-ounce price saves you $0.45 (though, it would probably be healthier to buy only the 32-ounce drink at the higher unit price).

Python can store any number of values in any number of variables, so long as your computer has enough memory (modern computers have plenty). Names can be as long as you want, so long as they contain only numbers, letters, and underscores and do not start with a number.

While this is a trivial concern when using Python as a calculator, it is good practice to use variables names that tell what they are storing. In the problem above, we might have named the variable unit_price. If we were going to store unit prices for several products, we might name it soda_unit_price. One more important thing to keep in mind is that just like your hand held calculator, when you exit Python, all stored variables are lost. It is possible to save them to a file, but this is far beyond the scope of this series.

Next week we will look at storing calculations using functions. Functions can save a significant amount of typing and they can allow us to construct and reuse more complex equations in ways that are easier to understand.


Benjamin Williams is currently a computer science major at BYU-I. He has been programming computers for 20 years and playing with electronics for 10. He has many hobbies, including blacksmithing, model rocketry and writing.

Related Links

Related Stories

SIGN UP FOR THE KSL.COM NEWSLETTER

Catch up on the top news and features from KSL.com, sent weekly.
By subscribing, you acknowledge and agree to KSL.com's Terms of Use and Privacy Policy.

KSL Weather Forecast