In [1]:
for fIndex, y in enumerate(range(2, 5)):
countdown = y
yFactorial = 1
wIndex = 0
while countdown > 1:
yFactorial *= countdown
##### CHECKPOINT! #####
countdown -= 1
wIndex += 1
print("RESULT: %d! = %d" % (y, yFactorial))
In [2]:
# Question 3
print("%s %s %s %s %s" % ("fIndex", "y", "wIndex", "countdown", "yFactorial"))
for fIndex, y in enumerate(range(2, 5)):
countdown = y
yFactorial = 1
wIndex = 0
while countdown > 1:
yFactorial *= countdown
print("%-6i %1i %6i %9i %10i" % (fIndex, y, wIndex, countdown, yFactorial))
countdown -= 1
wIndex += 1
#print "RESULT: %d! = %d" % (y, yFactorial)
In [3]:
# create our favorite massRatios:
massRatios = list(range(10))
massRatios
Out[3]:
In [6]:
massRatios[2:7]
Out[6]:
This is called slicing
and the 2 parameters required are separated by a colon :
.
Similar to the parameters for the range()
function, the starting number is inclusive
while the ending number is exclusive
.
When the 1st parameter is left out, the slice starts at the beginning of the list, when the last parameter is left out it goes until the end:
In [7]:
print(massRatios[:4])
print(massRatios[4:])
Note how in the first case, the length returned is the same as the value of the index you provide, thanks to 0-based indexing.
Note, also, that thanks to the asymmetry of inclusivity
for the start parameter vs exclusivity
for the end parameter, you can use the same number twice to get both ends of a list, thisk creates easier to read code as well.
In [8]:
i = 5
print(massRatios[:i])
print(massRatios[i:])
In [9]:
v1 = [0,1,2]
v2 = [7,8,9]
In [10]:
vectors = [v1, v2]
vectors
Out[10]:
When accessing elements, you can also just nest the indexing:
In [11]:
vectors[0][1]
Out[11]:
In [12]:
vectors[1][-1]
Out[12]:
$B_{\lambda}(T) = \frac{2 h c^2}{\lambda^5 \left[\exp\left(\frac{h c}{\lambda k T}\right) - 1 \right]}$
where $h$ is Planck's constant, $c$ is the speed of light, $k$ is Boltzmann's constant, $T$ is the blackbody temperature, and $\lambda$ is the wavelength.
In [13]:
# First, define the physical constants:
h = 6.626e-34 # J s, Planck's constant
k = 1.38e-23 # J K^-1, Boltzmann constant
c = 3.00e8 # m s^-1, speed of light
# Conversion between angstroms and meters
angPerM = 1e10
# The Planck function is a function of two variables;
# for now, we'll set T = 5,800 K, the photospheric temperature of the Sun
# and allow the wavelength to vary.
temp = 5800.0
from math import exp
# Define the function using def:
def intensity1(waveAng): # Function header
waveM = waveAng / angPerM # Will convert Angstroms to meters
B = 2 * h * c**2 / (waveM**5 * (exp(h * c / (waveM * k * temp)) - 1))
return B
# Units will be W / m^2 / m / ster
In [16]:
print('%e' % intensity1(5000.0)) # note the %e formatting string for exponent notation
In [18]:
def funcNoReturn(x):
print("Answer:", x + 5)
return x+5
y = funcNoReturn(6)
print("y =", y)
In [19]:
waveList = [3000 + 100 * i for i in range(41)]
In [20]:
waveList
Out[20]:
In [22]:
intensityList = [intensity1(wave) for wave in waveList]
intensityList
Out[22]:
In [23]:
for index in range(len(waveList)):
print('wavelength (Angstrom) = {} Intensity (W m^-3 ster^-1) = {:.2e}'\
.format(waveList[index], intensityList[index]))
In [26]:
def intensity1(waveAng): # Function header
waveM = waveAng / angPerM # Will convert Angstroms to meters
B = 2 * h * c**2 / (waveM**5 * (exp(h * c / (waveM * k * temp)) - 1))
return B
In [25]:
B
In [27]:
waveM
In [34]:
g = 10
def f(x):
g = 11
return x + g
f(5), g
Out[34]:
In [37]:
g = 10
def f(x):
global g # Now "g" inside the function references the global variable
g = 11 # Give that variable a new value
return x + g
f(5), g
Out[37]:
In [39]:
def intensity2(waveAng, temp): # 2nd version of function Intensity
waveM = waveAng / angPerM
B = 2 * h * c**2 / (waveM**5 * (exp(h * c / (waveM * k * temp)) - 1))
return B
In [40]:
intensity2(5000.0, 5800.0)
Out[40]:
In [42]:
intensity2(temp=5800.0, waveAng=5000.0)
Out[42]:
In [43]:
intensity2(waveAng=5000.0, temp=5800.0)
Out[43]:
In [44]:
intensity2(5800.0, 5000.0)
Out[44]:
In [45]:
def waveListGen(minWave, maxWave, delta):
waveList = []
wave = minWave
while wave <= maxWave:
waveList.append(wave)
wave += delta
return waveList
In [46]:
waveList = waveListGen(3000, 5000, 200)
waveList
Out[46]:
In [47]:
list(range(3000, 5001, 200))
Out[47]:
In [49]:
# (Defined h, c, k above and imported math)
def intensity3(waveAng, temp): # 3rd version of function Intensity
waveM = waveAng / angPerM
B = 2 * h * c**2 / (waveM**5 * (exp(h * c / (waveM * k * temp)) - 1))
return (waveAng, B)
In [50]:
temp = 10000.0 # Hot A star or cool B star; brighter than a G star
waveAng, intensity = intensity3(6000.0, temp=temp)
waveAng, intensity # notice the automatic packing of Python again
Out[50]:
In [51]:
result = intensity3(6000.0, 10000.0)
print(result)
type(result)
Out[51]:
In [67]:
for wave in waveListGen(3e3, 4e3, 100):
print('Wavelength (Angstroms) = %-10i Intensity (W m^-3 ster^-1) = %.2e'\
% intensity3(wave, 1e4))
Doc strings are used to document functions. They generally include:
A description of the functionality of the function
A list of arguments
A description of outputs (returned values)
And, they serve as the help documentation!
They go right after the function header and are enclosed within triple quotes.
In [52]:
def force(mass1, mass2, radius):
"""
Compute the gravitational force between two bodies.
Parameters
----------
mass1 : int, float
Mass of the first body, in kilograms.
mass2 : int, float
Mass of the second body, in kilograms.
radius : int, float
Separation of the bodies, in meters.
Example
-------
To compute force between Earth and the Sun:
>>> F = force(5.97e24, 1.99e30, 1.5e11)
Returns
-------
Force in Newtons : float
"""
G = 6.67e-11
return G * mass1 * mass2 / radius**2
In [53]:
result = force(5.97e24, 2.00e30, 1.5e11)
result
Out[53]:
In [54]:
# To see the documentation for a function, use help:
help(force)
or with the subwindow:
In [55]:
force?
In [73]:
# a = [] initialize an empty list
# a = [1., 2] initialize a list
# a.append(elem) add the elem object to the end of the list
# a + [5, 4] concatenate (join) two lists
# a.insert(i, e) insert element e at index i
# a[5] acess the value of the element at index 5
# a[-1] get the last list element value
# a[4:7] slice list a
# del a[i] delete list element with index i
# a.remove(e) remove list element with value e (not index e)
# a.index('test') find the index where the element has the value 'test'
# 4 in a find out whether 4 is in a
# a.count(4) count how many times 4 is in a
# len(a) return the number of elements in a
# min(a) return the smallest element in a
# max(a) return the largest element in a
# sum(a) add all the elements in a
# sorted(a) return a sorted version of list a
# reversed(a) return a reversed version of list a
# a[1][0][4] nested list indexing (3 dimensional list in this case)