# Chapter 3: Basic Operations

Owning a computer without programming is like having a kitchen and using only the microwave oven

-Charles Petzold

Python supports a number of basic operations. Here's a handy table:

Symbol | Operation |
---|---|

+ | Addition |

- | Subtraction |

* | Multiplication |

/ | Division |

** | Power |

% | Modulus |

Once again, there are other ones, but they're of absolutely no use to use right now.

Hopefully you're familiar with the basics: addition, subtraction, and multiplication. Here's a simple program.

- a=5
- b=10
- c=a*b

c ends up with the value 50.

Now, about division.

Look at the simple problem 3/2. Everybody knows the answer is ½. But Python looks at this differently. Since both 3 and 2 are integers, Python assumes that you must want an integer answer back. So Python will tell you how many times 2 goes into 3 - once. If you'd rather have a decimal answer, you need to make some part of the answer a decimal (a float). All of the following will give a decimal answer:

- 3.0/2
- 3/2.0
- 3.0/2.0

Of course, you can't simply tack on a decimal to the end of a variable name. Instead, you need to convert between integers and floats.

- a=3
- b=2
- print a/b #Will print 1
- print float(a)/b #Will print 1.5

You can use the float(), int(), and str() functions to convert values.

Power should also be pretty self explanatory: 2**2 is 4. The tricky one is Modulus.

Modulus calculates the remainder of a division problem. 3%2 is 1, which is the remainder of 3/2. 12%7 is 5, which is the remainder of 12/7. You'll use Modulus for all sorts of interesting things, including determining if a number is even (If a number % 2 equals 0, that number is even). Modulus is often pronounced mod, as in "Seven mod six is equal to one."

## Order of Operations

Python tends to follow the same order of operations that you learned in math class. Multiplication and division happen before addition or subtraction. But again, we're not going to care. Rather than try to remember what the order of operations is, it's better to just use parenthesis to group operations together.

If you need to write the program answer=5*10+10, you can just throw some parenthesis in there and get rid of any ambiguity.

- answer = (5*10)+10

Now anybody should be able to figure out that the answer is 60 and not 400.

## Getting input from the user

You can get input from the user using the raw_input() function.

- quest = raw_input("What is your quest? ")
- colour = raw_input("What is your favorite colour? ")
- velocity = raw_input("What is the airspeed velocity of an unladen swallow? ")
- print "Your quest is:", quest
- print "Your favorite colour is:", colour
- print "Bird's velocity is:", velocity

Our program will ask the user three questions and then print out the results. The input the user provides the program is in bold:

What is your quest? To learn to program Python! What is your favorite colour? Mauve What is the airspeed velocity of an unladen swallow? African, or European? Your quest is: To learn to program Python! Your favorite colour is: Mauve Bird's velocity is: African, or European?

Take note of what appears to be extra spaces. Up on line 1, 2, and 3 in the program there's an extra space between the question and quotation mark. This way the user can't type their response right next to the question mark.

raw_input() collects input from the user as strings. This is a problem when you need to use the input in a math equation. Let's ask the user for their age, and then print out how far away they are from their 18^{th} birthday.

- age = raw_input("How old are you? ")
- print "You're", abs(18 - age), "years away from your 18th birthday!"

Notice the use of the abs() function. As expected, it takes whatever is in the parenthesis and returns the absolute value.

This program will actually throw an error. You'll see the following:

You're Traceback (most recent call last): File "<stdin>", line 2, in <module> TypeError: unsupported operand type(s) for -: 'int' and 'str'

Python is telling you that there was an error. On the last line of the error message it's even telling you what the problem is. There's an unsupported operand where you're trying to use the minus sign. You're trying to subtract a string from an integer. No good. You instead need to convert the user input to an integer before you can do the math.

- age = int(raw_input("How old are you? "))
- print "You're", abs(18 - age), "years away from your 18th birthday!"

I will not run this program, as I don't want to get depressed. But I encourage you to try it.

This website will be taken offline before the end of 2011