Playing with Snakes

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:


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.

  1. a=5
  2. b=10
  3. 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:

  1. 3.0/2
  2. 3/2.0
  3. 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.

  1. a=3
  2. b=2
  3. print a/b #Will print 1
  4. 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.

  1. 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.

  1. quest = raw_input("What is your quest? ")
  2. colour = raw_input("What is your favorite colour? ")
  3. velocity = raw_input("What is the airspeed velocity of an unladen swallow? ")
  4. print "Your quest is:", quest
  5. print "Your favorite colour is:", colour
  6. 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 18th birthday.

  1. age = raw_input("How old are you? ")
  2. 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:

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.

  1. age = int(raw_input("How old are you? "))
  2. 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