In [1]:
print("Hello World")
In [2]:
x=1
print(x)
You can make a float either by just assiging the variable, or by specifying with the x=float() command.
In [3]:
x=1.5
print(x)
x=float(1)
print(x)
In [4]:
x=complex(1.5)
print(x)
In [5]:
x="This is a string"
print(x)
In [6]:
x=1
y=2
print("The value of x is",x,"and the value of y is",y)
In [7]:
x=1.0
y=2.0
print("The value of x is",x,"and the value of y is",y)
In [1]:
y = input("Enter the value of y: ")
print(y)
In [2]:
x = input("Enter the value of x: ")
print(x)
In [3]:
x = float(input("Enter the value of x: "))
print("The value of x is",x)
In [4]:
a=2
b=2
x=a+b
print(x)
In [5]:
x=a+2*b
print(x)
In [6]:
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)
In [7]:
x,y = 1,2.5
print(x+y)
In [8]:
x +=1
print(x)
In [9]:
x +=1
print(x)
In [ ]:
x,y=y,x
print(x,y)
In [6]:
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")
In [5]:
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)
In [2]:
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)
In [3]:
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)
In [11]:
n = int(input("Enter an Integer: "))
if n%2==0:
print("even")
else:
print("odd")
Now check two intergers
In [12]:
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)
In [13]:
f1 = 1
f2 = 2
next = f1 + f2
while f1<=1000:
print(f1)
f1=f2
f2=next
next=f1+f2
In [14]:
f1,f2 = 1,1
while f1 <= 1000:
print(f1)
f1,f2 = f2,f1+f2
In [16]:
from math import sqrt
r = [1.0, 1.5, -2.2]
length = sqrt(r[0]**2 + r[1]**2 + r[2]**2)
print(length)
In [17]:
total=sum(r)
print(total)
In [18]:
mean=sum(r)/len(r)
print(mean)
In [22]:
from math import log
r=[1.0,1.5,2.2]
logr = list(map(log,r))
print(logr)
In [23]:
r.append(6.1)
print(r)
To make an empty list (must make an empty list before adding elements to it.
In [8]:
r=[]
r.append(1.0)
r.append(1.5)
r.append(-2.2)
print(r)
To remove a value from the end of a list:
In [9]:
r.pop()
print(r)
In [25]:
from numpy import zeros
a = zeros(4,float)
print(a)
In [26]:
a=zeros([3,4],float)
print(a)
In [27]:
from numpy import empty
a=empty(4,float)
print(a)
In [28]:
from numpy import zeros
a=zeros([2,2],int)
a[0,1]=1
a[1,0]=-1
print(a)
Making an array with arange:
In [14]:
from numpy import arange
x=arange(1,11,1)
print(x)
In [15]:
x=arange(1.0,11.0,2.0)
print(x)
Now linspace - which is similar, but not identical
In [17]:
from numpy import linspace
x=linspace(1,100.0,10)
print(x)
In [10]:
from numpy import loadtxt
a = loadtxt("values.txt",float)
print(a)
Arithmetic with arrays
In [32]:
from numpy import array
a = array([1,2,3,4],int)
print(a+1)
In [33]:
a = array([1,2,3,4],int)
b = array([2,4,6,8],int)
print(a*b)
For dot products need to use special function
In [35]:
from numpy import array,dot
print(dot(a,b))
Finding the size of an array
In [37]:
a = array([[1,2,3],[4,5,6]],int)
print(a.size)
print(a.shape)
In [39]:
r=[1,3,5,7,9,11,13,15]
s=r[2:5]
print(s)
In [40]:
r=[1,3,5]
for n in r:
print(n)
print(2*n)
print("Finished")
In [41]:
r=range(5)
for n in r:
print("Hello again")
In [44]:
for n in range(5):
print(n**2)
Variants of range:
In [45]:
for n in range(5):
print(n)
In [47]:
for n in range(2,8):
print(n)
In [48]:
for n in range(2,20,3):
print(n)
In [50]:
for n in range(20,2,-3):
print(n)
Range must be integers (so the below won't work)
In [11]:
p=10
q=2
for n in range(p/q):
print(n)
Make use of integer division
In [12]:
for n in range (p//q):
print(n)
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.
~
~
~
~
~
~
~
In [19]:
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 ")
Quicker method using range
In [20]:
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")
In [ ]: