Solve the following problems.
See https://asu-compmethodsphysics-phy494.github.io/ASU-PHY494/2021/02/09/04_Python_4/ for help.
(Note that the last activity on class inheritance is optional — you don't need the final 10 points to get 100% on this activity even though GitHub autograding will still show the tests as failing. Don't worry about that: if you have 47 points you have 100%)
-
Create a module
constants.pywith values for the mathematical constant π (namedpi, see also OEIS A000796) and the physical constantscfor the speed of light in vaccuum (in ms/s) and Planck's constanth(in J s). -
In a file
problem1.py, import your constants module and compute the reduced Planck's constant ħ = h/2π and store it in variablehbar. Print it to all 9 significant figures in scientific notation withprint("hbar = {hbar:.9e}".format(hbar))
Use your myfuncs module from the last lesson in a file problem2.py and
-
Compute Heaviside(n) for the integers -10 ≤ n ≤ 10, assign the list to a variable
y, and print it withprint(f"Heaviside: {y}")
(this
printuses an f-string, available since Python 3.6 as an improvement over format representations; both use the Format Specification Mini-Language) -
Compute the temperature of the boiling point of water under standard conditions in degrees Celsius from the value in Kelvin, Tboil = 373.15 K. Assign it to a variable
t_boiland print it withprint(f"Water boils at {t_boil:.2f} C")
Import the math
module and the os
module from the Python Standard
Library in a file
problem3.py. Look in these modules for functions and attributes to
solve the following problems:
-
Compute
y_full_circle = sin(tau). (Optionally, you can also print the value.) -
Define the cumulative distribution function of the normal distribution with mean µ and standard deviation σ
Phi(x, µ, σ) = 1/2 [1 + erf( (x-µ)/(σ sqrt(2)) )]as a Python function
Phi(x, mu=0., sigma=1.0).For a random variable X that is normally distributed with mean µ=88.6 and standard deviation σ=6.3 compute the probability P(X>95) to observe a value of X > 95, using
P(X > x) = 1 - Phi(x, µ, σ)Assign your result to a variable
P_aplusand print it withprint(f"P(X > 95) = {100 * P_aplus:.1f}%")
-
Get the current working directory as a string and assign to the variable
cwdand print it withprint(f"cwd = '{cwd}'")
Given the sentence sentence = "MAY THE FORCE BE WITH YOU!" in file problem4.py
- test if it is lower case (assign the boolean value to a variable
all_lower) - make it lower case (store in
l_sentence)
Create a file bodies.py that contains the class Sphere.
- A
Sphereinstance is initialized with its position (Cartesian coordinates a list or tuple with three floating point numbers) and optionally its radius; the default for radius is 1.0. - Store the position in an attribute
posand the radius in attributeradius. - The class has a method
volume()that returns the volume of the sphere. - The class has a method
translate(t), that changes the position of the sphere by adding the translation vectort(tuple with three floats) to the current position.
Create a file balls.py in which you
- create an object representing a ball by instantiating a
Sphereat position(0, 0, 10)with radius 2 and assigning it to variableball - change the position of the ball to
(-5, 0, 0), - assign the volume of the ball to a variable
volumeand print it to 3 decimals, - translate the ball by
(5, 0, 0)
After each step, print the position of the ball.
Create a file ball_oons.py in which you
- create a ball (as a
Sphere) at position(0, 0, 10)with radius 2 (variableball), - create a balloon (as a
Sphere) at position(0, 0, 10)with radius 6 (variableballoon), - change the ball's position to
(-1, -1, 0) - compare the balloon's and the ball's position by printing
print(f"ball at {ball.pos} != balloon at {balloon.pos}")
In file astronomy.py, define a class Planet that is derived from
Sphere and is instantiated as Planet(name, pos, mass, radius).
Planet must have a method Planet.density() that returns the
density of the planet, mass/volume.
Use your class to represent Earth (quantities from http://www.wolframalpha.com)
# lengths in m and mass in kg
earth = Planet("Earth", (1.4959802296e11 , 0, 0), 5.9721986e24, 6371e3)
print(earth.density())and print the density.