# Python Basics

## Python as Calculator

The simplest way to use Python is as a command line. In whatever operating system you use, open a "terminal" or a "command line", and type "Python". You should see a prompt comprising three greater-than signs: ">>>". This is where you can type Python commands interactively. You could immediately use this as a simple calculator:

>>> 3+4*498.52 1997.08

The syntax rules for entering simple arithmetic expressions are very familiar - the same as for most spreadsheet programs. Note that there is an operator precedence: multiplication and division first, then addition and subtraction. Powers are applied using the`pow()`function, or using

`**`to indicate exponentiation:

>>> pow(3,2) 9 >>> 3**2 9

You can run commands you already gave anew by using the up-arrow keys. You can even edit the commands using the arrows to move around, backspace to delete characters, adding characters wherever the cursor points. All this occurs in Python itself, without any need for the special mathematical packages. However, if you want to do anything genuinely mathematical, you will need to load a package or two. In particular, without help, Python knows nothing about trigonometric functions. This is one of the simplest parts of the Numpy package: it provides a library of standard mathematical functions. To load the Numpy package, type the following.

>>> from numpy import *

If this is successful, there will be no numbers returned, no messages, no nothing.
Python does not brag when it gets something right. It does whine when you type
something wrong. Here you can see the general syntax for loading things from packages.
We told it to look in the Numpy package, and the asterisk indicates that it should load
*every* function in that package. If we had wanted only one function, we could
have just typed the name of that function instead of using the wild card "*".
On the other hand, if we want to import the entire package with the name `Numpy`,
we can just type `import Numpy`.
Now we can evaluate e.g. a cosine.

>>> cos(9) -0.91113026188467694 >>> cos(pi) -1.0 >>> pi 3.141592653589793

Note that Python knows the value of π to double precision floating point accuracy. It knows $e$ too.

>>> e**2 7.3890560989306495 >>> exp(2) 7.3890560989306504

Observe that in this case there is one small problem. We know that $\text{exp}\left(x\right)={e}^{x}$ by definition. However, when we actually evaluate the two sides of that equation in floating point arithmetic, we get a more accurate number from the exponential function than from squaring $e$. There is some roundoff error in the latter calculation. Always use the built-in function if you can.## Defining functions

You will often want to define your own functions, to evaluate at various
places. Python makes that easy, albeit less than intuitive.
To define the function, use the `def` keyword, tell the function
name and what arguments you want the function to accept, and then say
what you want to do with those arguments. Let's look at a definition.

>>> def g(x): ... return x*x*x ... >>> g(2) 8 >>> g(2*3) 216

There are several details to attend to here. First, note the colon
at the end of the line with `def` in it. This tells Python that there
is more function definition coming. When you hit <Enter> then Python
types the ellipses (...) for you. These are to help you line up your text
properly, the point being that *indentations are very important*.
We put in a two of leading spaces to indent the contents of our function definition,
then told the function to "return" the cube of the argument. When Python typed the
next set of ellipses, we just hit <Enter> to end the function definition.
Thus,
`x` represents the number where we want to evaluate the function,
and the result of the evaluation is what is returned. After that
`g(2)` evaluates to the cube of 2; `g(2*3)` evaluates
to the cube of 6.

We cannot emphasize enough that the indentation (or "leading spaces", if you prefer) is very important. Not to use an indentation would be an error; using indentations of different lengths would be an error. Here is a definition of a multiline function.

>>> def poly(x):
... y = 2*x
... return y*y+x+1
File "

The first attempt to define the function `poly` failed because we used
two spaces as the indentation for the first line inside the function definition, but
then used four spaces for the next line. The number of spaces you choose for your indentation does not matter,
but once you pick a number, you must use the same sized indentation for every line of
the function. We will always use two spaces for our indentations in this text.

A solution for the
final is available.