<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Python Notes
My notes from working through Chapter 2 of Newman's Computational Physics
Step1:  Variable Types
Step2:  You can make a float either by just assiging the variable, or by specifying with the x=float() command.
Step3:  Tips for Formatting Output
Step4:  Inputting stuff
Step5:  Specify the type of the input variable
Step6:  Basic Maths
Step7:  Python Modifiers
Step8:  Dropping a Ball from a Tower
Step9:  Example of Using Math Functions
Step10:  Examples of If and While Statements
Step11:  Now check two intergers
Step12:  Example 2.4
Step13:  Lists and Arrays
Get used to starting counting from zero!
The most basic containier in python is the list - a set of numbers associated with each other. 
Example 1
Step14:  Applying functions to lists
Make use of the meta function "map" which allowes a function to be applied to all elements of a list at once.
The below may seems a bit strange, but it's done to save memory - the map(log,r) does not save, until you put it into a list.
Step15:  Append
Append is a really useful function with a list - allows values to be added to the list, which is something you'll do all the time in physics coding.
Step16:  To make an empty list (must make an empty list before adding elements to it.
Step17:  To remove a value from the end of a list
Step18:  Arrays
Lists can be added to with append, arrays have fixed lengths. 
Lists can have mixed types, arrays are all the same type (good for debugging). 
Arrays can be 2D (e.g. images). 
Arrays behave like vectors or matrices (not true with lists).
Step19:  Making an array with arange
Step20:  Now linspace - which is similar, but not identical
Step21:  Reading from a File
(Something you will do all the time in physics)
Step22:  Arithmetic with arrays
Step23:  For dot products need to use special function
Step24:  Finding the size of an array
Step25:  Slicing
If r is a list, then r[m
Step26:  For Loops
Step27:  Variants of range
Step28:  Range must be integers (so the below won't work)
Step29:  Make use of integer division
Step30:  Example
Step31:  Quicker method using range
<ASSISTANT_TASK:>
Python Code:
print("Hello World")
Explanation: Python Notes
My notes from working through Chapter 2 of Newman's Computational Physics
End of explanation
x=1
print(x)
Explanation: Variable Types
End of explanation
x=1.5
print(x)
x=float(1)
print(x)
x=complex(1.5)
print(x)
x="This is a string"
print(x)
Explanation: You can make a float either by just assiging the variable, or by specifying with the x=float() command.
End of explanation
x=1
y=2
print("The value of x is",x,"and the value of y is",y)
x=1.0
y=2.0
print("The value of x is",x,"and the value of y is",y)
Explanation: Tips for Formatting Output
End of explanation
y = input("Enter the value of y: ")
print(y)
x = input("Enter the value of x: ")
print(x)
Explanation: Inputting stuff
End of explanation
x = float(input("Enter the value of x: "))
print("The value of x is",x)
Explanation: Specify the type of the input variable
End of explanation
a=2
b=2
x=a+b
print(x)
x=a+2*b
print(x)
c=1
x = a + b/c
print(x)
x = (a + b)/c
print(x)
x = a + 2*b - 0.5*(1.618**c + 2/7)
print(x)
Explanation: Basic Maths
End of explanation
x,y = 1,2.5
print(x+y)
x +=1
print(x)
x +=1
print(x)
x,y=y,x
print(x,y)
Explanation: Python Modifiers
End of explanation
h = float(input("Enter the height of the tower: "))
g=9.81
t=(2.0*h/g)**0.5
print("The ball will hit the ground after",t,"seconds")
Explanation: Dropping a Ball from a Tower
End of explanation
from math import sin,cos,pi
print("This program will convert from r,theta to x,y co-ordinates")
r = float(input("Enter r: "))
d = float(input("Enter theta in degrees: "))
theta = d*pi/180
x = r*cos(theta)
y = r*sin(theta)
print("x =",x," y =",y)
Explanation: Example of Using Math Functions
End of explanation
x = int(input("Enter a whole number no greater than ten: "))
while x>10:
            print("This is greater than ten.  Please try again.")
            x = int(input("Enter a whole number no greater than ten: "))
print("Your number is",x)
x = int(input("Enter a whole number no greater than ten: "))
if x>10:
       print("You entered a number greater than ten.")
       print("Let me fix that for you.")
       x = 10
print("Your number is",x)
n = int(input("Enter an Integer: "))
if n%2==0: 
        print("even")
else: 
        print("odd")
Explanation: Examples of If and While Statements
End of explanation
print("Enter two integers, one even, one odd.")
m=int(input("Enter the first integer: "))
n=int(input("Enter the second integer: "))
while (m+n)%2==0:
        print("One must be even the other odd.")
        m=int(input("Enter the first integer: "))
        n=int(input("Enter the second integer: "))
print("The numbers you chose are", m, "and", n)       
Explanation: Now check two intergers
End of explanation
f1 = 1
f2 = 2
next = f1 + f2
while f1<=1000:
    print(f1)
    f1=f2
    f2=next
    next=f1+f2
f1,f2 = 1,1
while f1 <= 1000: 
    print(f1)
    f1,f2 = f2,f1+f2
Explanation: Example 2.4: The Fibonacci Sequence
The sequence of integers in which each is the sum of the previous two. E.g. 1 1 2 3 5 8 etc.
The sequence up to the value of 1000 would take ages to calculate by hand, but it's quick in code
End of explanation
from math import sqrt
r = [1.0, 1.5, -2.2]
length = sqrt(r[0]**2 + r[1]**2 + r[2]**2)
print(length)
total=sum(r)
print(total)
mean=sum(r)/len(r)
print(mean)
Explanation: Lists and Arrays
Get used to starting counting from zero!
The most basic containier in python is the list - a set of numbers associated with each other. 
Example 1: A 3 component vector as a list
End of explanation
from math import log
r=[1.0,1.5,2.2]
logr = list(map(log,r))
print(logr)
Explanation: Applying functions to lists
Make use of the meta function "map" which allowes a function to be applied to all elements of a list at once.
The below may seems a bit strange, but it's done to save memory - the map(log,r) does not save, until you put it into a list.
End of explanation
r.append(6.1)
print(r)
Explanation: Append
Append is a really useful function with a list - allows values to be added to the list, which is something you'll do all the time in physics coding.
End of explanation
r=[]
r.append(1.0)
r.append(1.5)
r.append(-2.2)
print(r)
Explanation: To make an empty list (must make an empty list before adding elements to it.
End of explanation
r.pop()
print(r)
Explanation: To remove a value from the end of a list:
End of explanation
from numpy import zeros
a = zeros(4,float)
print(a)
a=zeros([3,4],float)
print(a)
from numpy import empty
a=empty(4,float)
print(a)
from numpy import zeros
a=zeros([2,2],int)
a[0,1]=1
a[1,0]=-1
print(a)
Explanation: Arrays
Lists can be added to with append, arrays have fixed lengths. 
Lists can have mixed types, arrays are all the same type (good for debugging). 
Arrays can be 2D (e.g. images). 
Arrays behave like vectors or matrices (not true with lists).
End of explanation
from numpy import arange
x=arange(1,11,1)
print(x)
x=arange(1.0,11.0,2.0)
print(x)
Explanation: Making an array with arange:
End of explanation
from numpy import linspace
x=linspace(1,100.0,10)
print(x)
Explanation: Now linspace - which is similar, but not identical
End of explanation
from numpy import loadtxt
a = loadtxt("values.txt",float)
print(a)
Explanation: Reading from a File
(Something you will do all the time in physics)
End of explanation
from numpy import array
a = array([1,2,3,4],int)
print(a+1)
a = array([1,2,3,4],int)
b = array([2,4,6,8],int)
print(a*b)
Explanation: Arithmetic with arrays
End of explanation
from numpy import array,dot
print(dot(a,b))
Explanation: For dot products need to use special function
End of explanation
a = array([[1,2,3],[4,5,6]],int)
print(a.size)
print(a.shape)
Explanation: Finding the size of an array
End of explanation
r=[1,3,5,7,9,11,13,15]
s=r[2:5]
print(s)
Explanation: Slicing
If r is a list, then r[m:n] is a subset of the list starting with element m, and going up to but not including element n.
End of explanation
r=[1,3,5]
for n in r: 
    print(n)
    print(2*n)
print("Finished")
r=range(5)
for n in r:
    print("Hello again")
for n in range(5):
    print(n**2)
Explanation: For Loops
End of explanation
for n in range(5):
    print(n)
for n in range(2,8):
    print(n)
for n in range(2,20,3):
    print(n)
for n in range(20,2,-3):
    print(n)
Explanation: Variants of range:
End of explanation
p=10
q=2
for n in range(p/q):
    print(n)
Explanation: Range must be integers (so the below won't work)
End of explanation
for n in range (p//q):
    print(n)
Explanation: Make use of integer division
End of explanation
R = 1.097e-2
for m in [1,2,3]:
    print("Series for m = ",m)
    for k in [1,2,3,4,5]:
        n=k+m
        invlambda = R*(1/m**2 - 1/n**2)
        print("   ", 1/invlambda, " nm ")
Explanation: Example: The Emission Lines of Hydrogen
Please work through this example to check you understand lists and arrays.
The wavelengths of emission lines in the spectrum of hydrogen are given by the Rydberg Formula: 
$\frac{1}{\lambda} = R ( \frac{1}{m^2} - \frac{1}{n^2} )$
were $R$ is the Rydberg constant, $R = 1.097 \times 10^{-2}$, and $m$ and $n$ are the energy levels of the transition. 
We can write a programme to calculate all the emission lines very easilly. 
First do this using for loops, then using lists and "for" commands.
Format the output to have 3 decimal places.
~
~
~
~
~
~
~
End of explanation
R = 1.097e-2
for m in range(1,4):
    print("Series for m = ",m)
    for n in range(m+1,m+6):
        invlambda = R*(1/m**2 - 1/n**2)
        print("   ",1/invlambda, " nm")
Explanation: Quicker method using range
End of explanation
<END_TASK>