Practical programming for the normal person

Practical programming for the normal person

(Spectral-Design/Shutterstock)


Save Story
Leer en español

Estimated read time: 3-4 minutes

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 — We live in a unique time. At no other time in history have so many people used technology that they understand so little. Computers are so ubiquitous in the U.S. that a vast majority of households own at least one, and nearly everyone has access to one.

Modern computers are extremely powerful, but often that power is underutilized. This series aims to help normal people learn just enough basic programming to take advantage of the power and capabilities provided by their computers.

We will be using the Python programming language in this series. It is easy to learn and use, and it comes with an interface that makes it good for practical everyday use. It comes with most modern Mac and Linux installations, and it is free to download and use for Windows (see installation instructions on the second page of this article). Once it is installed, launch the Python command line interface by running "python" from a terminal or a run dialog.

Python makes a good calculator — an extremely good calculator. For simple calculations, it is easier to use than most calculator applications, and for complex calculations, it can do far more. When the Python interface starts, you will be presented with a prompt that looks like this: ">>>". This is where you type your commands. To run a command, press return. To begin, try typing a simple math problem, like 2 + 2. When you press return, the answer will be displayed.


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

As with normal math, multiplication and division take precedence over addition and subtraction, so a problem like 2 + 2 * 2 will result in 6, not 8. Parenthesis can be used to override precedence: (2 + 2) * 2 = 8 and (2 + 4) / 2 = 3. The exponent operator is **, so 2**2 = 4 and 2**3 = 8 (for a simple square root, use 0.5 as the exponent). When operator precedence is equal, it goes from left to right: 12 / 2 * 2 = 12 but 12 / (2 * 2) = 3.

This is all just basic math and for the most part in Python, math works the same as it does in arithmetic and algebra. There is one important thing to know: computers differentiate between integer math and floating point (decimal) math. If you try 1 / 2, you will get 0, instead of 0.5. This is because both numbers are integers, so the result will be an integer. It will be rounded down to the nearest integer (thus -1/2 will return -1).

If you need floating point results, at least one of the numbers in the division must be typed with a decimal point: 1 / 2.0 = 0.5, and 1.0 / 2 = 0.5. (If this bothers you, you can run this arcane looking command each time you run Python: "from __future__ import division".)

There is an advantage to working in integers though. In Python, integers can scale as large as necessary without loosing anything. Floating point numbers will start to loose accuracy as they get very large or very small.

This just brushes the surface. Future articles will discuss things like storing values and calculations for future use and rapidly doing calculations multiple times with different values.


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

Most recent Utah stories

Related topics

Utah

STAY IN THE KNOW

Get informative articles and interesting stories delivered to your inbox weekly. Subscribe to the KSL.com Trending 5.
By subscribing, you acknowledge and agree to KSL.com's Terms of Use and Privacy Policy.

KSL Weather Forecast