Save a copy of the program ball.py
and confirm that the copy runs as the
original. You are now supposed to introduce errors in the code, one by
one. For each error introduced, save and run the program, and comment
how well Python's response corresponds to the actual error. When you
are finished with one error, re-set the program to correct behavior
(and check that it works!) before moving on to the next error.
a)
Insert the word hello on the empty line above the assignment to v0
.
b)
Remove the #
sign in front of the
comment initial velocity.
c)
Remove the =
sign in the assignment to v0
.
d)
Change the reserved word print
into pint
.
e)
Change the calculation of y
to y = v0*t
.
f)
Change the line print y
to print x
.
g) Replace the statement
y = v0*t - 0.5*g*t**2
by
y = v0*t - (1/2)*g*t**2
Filename: testing_ball.py
.
Write a program that computes the volume \( V \) of a cube with sides of length \( L = 4 \) cm and prints the result to the screen. Both \( V \) and \( L \) should be defined as separate variables in the program. Run the program and confirm that the correct result is printed.
Hint.
See ball.py
in the text.
Filename: cube_volume.py
.
Write a program that computes both the circumference \( C \) and the area \( A \) of a circle with radius \( r = 2 \) cm. Let the results be printed to the screen on a single line with an appropriate text. The variables \( C \), \( A \) and \( r \) should all be defined as a separate variables in the program. Run the program and confirm that the correct results are printed.
Filename: circumference_and_area.py
.
We are interested in the volume \( V \) of a cube with length \( L \): \( V=L^3 \), computed for three different values of \( L \).
a)
Use the linspace
function to compute three values
of \( L \), equally spaced on the interval \( [1,3] \).
b) Carry out by hand the computation \( V=L^3 \) if \( L \) is an array with three elements. That is, compute \( V \) for each value of \( L \).
c)
In a program, write out the result V
of V = L**3
when L
is an array with three elements as computed by linspace
in a).
Compare the resulting volumes with your hand calculations.
d)
Make a plot of V
versus L
.
Filename: volume3cubes.py
.
Write a program that stores the sum \( 1+2+3+4+5 \) in one variable and then creates another variable with the average of these five numbers. Print the average to the screen and check that the result is correct.
Filename: average_int.py
.
a) Compute the volume in Exercise 1.2: Volume of a cube by using Python interactively, i.e., do the computations at the command prompt (in a Python shell as we also say). Compare with what you got previously from the written program.
b) Do the same also for Exercise 1.3: Area and circumference of a circle.
Consider the following interactive Python session:
In [1]: x=2; y=4
In [2]: x/y
Out[2]: 0
What is the problem and how can you fix it?
Invoke Python interactively and perform the following steps.
x
to 2.x
. Print out the result.x + 1*2
and (x+1)*2
.
(Observe how parentheses make a difference).x
?
Write a program that defines two variables as x = pi
and y = 2
.
Then let the program compute the product z
of these two variables
and print the result to the screen as
Multiplying 3.14159 and 2 gives 6.283
Filename: formatted_print.py
.
Write a program that prints four random to the screen. The numbers should be drawn from a uniform distribution over the interval \( [0,10) \) (0 inclusive, 10 exclusive). Find the information needed for the task, see for example http://docs.python.org.
Hint.
Python has a module random
that contains a function by the name uniform
.
Filename: drawing_random_numbers.py
.