3.5 Arithmetic

The programs you write will nearly always use numbers. Python can manipulate numbers in much the same way as a calculator (as well as in the much more complex and powerful ways you'll use later). Basic arithmetic calculations are expressed using Python's (mostly obvious) arithmetic operators.

    >>> a = 2               # Set up some variables to play with
    >>> b = 5
    >>> print a + b
    7
    >>> print a - b         # Negative numbers are displayed as expected
    -3
    >>> print a * b         # Multiplication is done with a *
    10
    >>> print a / b         # Division is with a forward slash /
    0.4
    >>> print a ** b        # The power operation is done with **
    32
    >>> print b % a         # The % operator finds the remainder of a division
    1
    >>> print 4.5 % 2       # The % operator works with floats too
    0.5

The above session at the interactive interpreter also illustrates comments. This is explanatory text added to programs to help anyone (including yourself!) understand your programs. When Python sees the # symbol it ignores the rest of the line. Here we used comments at the interactive interpreter, which is not something one would normally do, as nothing gets saved. When writing modules you should comment your programs comprehensively, though succinctly. They should describe your program in sufficient detail so that someone who is not familiar with the details of the problem but who understands programming (though not necessarily in Python), can understand how your program works. Examples in this handbook should demonstrate good practice.

Although you should write comments from the point of view of someone else reading your program, it is in your own interest to do it effectively. You will often come back to read a program you have written some time later. Well written comments will save you a lot of time. Furthermore, the demonstrators will be able to understand your program more quickly (and therefore mark you more quickly too!).

The rules of precedence are much the same as with calculators. ie. Python generally evaluates expressions from left to right, but things enclosed in brackets are calculated first, followed by multiplications and divisions, followed by additions and subtractions. If in doubt add some parentheses:

    >>> print 2 + 3 * 4
    14
    >>> print 2 + (3 * 4)
    14
    >>> print (2 + 3) * 4
    20

Parentheses may also be nested, in which case the innermost expressions are evaluated first; ie.

    >>> print (2 * (3 - 1)) * 4
    16

EXERCISE 3.5
Play around with the interactive interpreter for a little while until you are sure you understand how Python deals with arithmetic: ensure that you understand the evaluation precedence of operators. Ask a demonstrator if you need more help with this.

Write a program to read the radius of a circle from the keyboard and print its area and circumference to the screen. You need only use an approximate value for $\pi$. Don't forget comments, and make sure the program's output is descriptive, i.e. the person running the program is not just left with two numbers but with some explanatory text. Again, think about whether to use input() or raw_input().