# Installing Python and Writing your first Script

### Install python

We will need python and will make use of the numpy (for performing calculations) and matplotlib (for drawing graphs) packages. Anaconda contains all the python packages you will need (and more). Install it from the website (choose version 3.6 so we'll all be using the same version) and allow it to add itself to your system's PATH when requested during the install process. That's it! You now have everything you need.

### The Python Interpreter

You should now be able to run python from the console. Type "console" into the windows menu to bring up a console and then type "python" to start the python interpreter. You can play around with this if you like and "exit()" when you're done. We won't be using the interpreter again as we'll be working with self contained python scripts.

### Writing a python Script

Open notepad (or a better text editor, I recommend notepad++ or sublime) and type/copy the following: [Note that if you're using python 2.7 you'll need to omit the parentheses]

print("My first python program!")

Save the program as p1.py (you may need to set the file type to *All Files* if using notepad). "p1" is just a name for the file - this could be whatever you like. ".py" tells the computer that the file contains a python script.

### Running the script

You could double click on the file in Windows explorer to run it, but this will probably only flash up a console window for a second which will then disappear. This because when the script finishes running (after just one line) the program is exited causing the console to close. Instead, open a console like you did earlier and navigate to the folder in which you saved the script. You can use the 'cd' command to Change Directory, or you can just right click on the folder in explorer and select open console here. Now, simply type the name of the file and hit enter. If all goes to plan you should be staring at the glorious words "My first python program!", or whatever you changed it to because you're ahead of the game and I can't tell you what to do.

### Editing the Script

Now, switch back to your editor and change the printed message. Save it. Switch back to the console and run the script again. Voila. You'll be doing this a lot in the tutorial so it's important to be comfortable editing and rerunning a script.

### Using numpy and matplotlib

The final thing to do is check that numpy and matplotlib are installed correctly. Copy the following code (it does not matter if it doesn't make any sense to you at this point) and run it like you did before.

import numpy as np
import matplotlib.pyplot as plt

theta = np.arange(0, 2*np.pi + 0.01, 0.01)
r = 1 + np.sin(60 * theta)

ax = plt.subplot(111, projection='polar')
ax.plot(theta, r)
ax.axis('off')
plt.show()


# Some Simple Exercises (not necessary for the tutorial but will definitely help!)

### Variables and Simple Maths

x = 7
y = 3
print(x)


The preceding creates a variable 'x' and assigns it the value 7. It also creates a variable 'y' and assigns it the value 3. Printing x outputs the value currently assigned to x, in this case: 7.

1. Modify the print command so that it prints out the sum of the two variables.

2. Create a new variable z equal to the product of x and y and output the value of z. (The product of x and y can be calculated using x * y and division as x/y)

3. Change the value of x and see if the output value also changes.

4. Output x to the power of y. Note: there is no way to guess how to do this with the knowledge you already have. This is a test of google skills and an early reminder that google is your friend when learning how to program anything.

5. Output the square root of x. You can use numpy's sqrt function for this (np.sqrt(x)). Make sure you remember to include the import line we used above so that the program loads and has access to numpy.

### Simple Functions

def sum(a, b):
return a + b

x = 7
y = 3
print(sum(x,y))
print(sum(100, -23))


This program introduces two important features. First, the ability to define a function that can later be called repeatedly. In this case the function only calculates the sum of two numbers, something we already know how to do in a much simpler way. However, a function could be much more complicated than this and it may be really useful as a way of separating an important task from the rest of the code and allowing it to be called multiple times and with multiple different inputs.

The second feature is the use of indentation to denote control blocks. Most programming languages make extensive use of parenthesis ('{','}'), but python does not. How does it know that x = 7 is not part of the function? Python uses indentation to mark the scope of a particular control statement.

To break this down further. "def" signals that we're defining a function. "sum" is the name we've given to the function. This name will be used every time the function is called. (a, b) are the arguments to the function. The function takes two arguments, and whatever name they have when they are sent to the function, they will be referred to as a and b in the code for the function itself. The colon marks that it's time to start a new control block (and the next line will be indented). Finally, return tells the function to stop and return the specified value.

6. Change the function to perform a different simple mathematical calculation. Make sure you also change its name to be representative of what the function does.

7. Putting together everything so far it should be possible to write a function that calculates the quadratic formula (for this question just return one of the answers). You will need 3 arguments for the function. You will also need to make good use of brackets to ensure the correct order of mathematical operations. Test it for the equation $3x^2 + 4x - 1 = 0$. You should get the answer 0.215 or -1.548.