You are viewing Chapter 1.1: Using Python as a Calculator of the Guide to Learning Python for OpenRPG. You can also go to the table of contents to see the full list of available chapters.

So, as I have introduced, the first section will be performing simple math functions with
Python. So, start by opening up either your Python interpreter. In windows, it is listed
in the program group in your start menu under Start > Programs > Python 2.2 > Python
(command line). You may have a different Python version number, such as 2.3 or 2.4; that's no
problem. In other cases, find your python.exe file, and run it. Either way, you should get
a *console window* (Windows users likely know it as a "DOS box") with three
"greater than" symbols, similar to this:

Python 2.2.2 (#37, Oct 14 2002, 17:02:34) [MSC 32 bit (Intel)] on win32 Type "help", /"copyright", "credits" or "license" for more information. >>>

This is the command prompt for Python, where you can run commands like a program would run them - it's useful for testing things out if you're not sure exactly how they work. You can also get a Python Shell window in IDLE -- this one is a little better looking, since it's colored, and posts all its "replies" to you in blue. Like the IDLE version, I'll be posting the responses you should get in blue.

So, start by typing 1 + 1 and pressing ENTER. The interpreter should print out 2. Amazing. If it didn't, double check your typing, though I'm not sure you could mistype 1+1... By the way, putting spaces in between numbers and functions is a matter of choice; it doesn't matter at all. Anyway, it can do subtraction, multiplication, and division, much as you would expect:

>>> 2-1 1 >>> 2 * 2 4 >>> 4/2 2

However, it is important that you make a note on division. Remember how I said earlier that Python does a good job converting between different types of numbers? There's an exception in division. If you divide two whole numbers (ints), then it makes the result an int, even if it rightly should be a decimal. For example:

```
>>> 5/2
2
```

There is a way around this, however. If one or both of the numbers in the division problem are decimals, the result will be a decimal. For example:

>>> 5.0 / 2 2.5 >>> 5 / 2.0 2.5 >>> 5.0 / 2.0 2.5

Makes some sense, right? It returns the same kind of number as you put in - whole or decimal. Don't make too much of a fuss out of it, though: division is the only time you'll have to worry about this, because the only other functions that return a decimal number require a decimal be input in order to do so. For example:

>>> 1 + .5 1.5 >>> 5 * 1.2 6.0

See how in both cases, there was a decimal entered? (.5 and 1.2, respectively) Anyway, there is also one more math function, the exponent, or "to the power of". It is denoted by **, not ^. So, to do 5 squared, or 5 to the 2nd power, we type:

```
>>> 5**2
25
```

Now, wasn't that easy? Of course, you're probably getting tired of doing something you can do better on your TI-83+, so I'll stop this now and move on to actually creating a program. ;)