lab1¶
Topics: Python functions with one and more arguments.
Relevant supplementary videos :
Relevant Socratica video:
square_area(a)
¶
Implement a function square_area(a)
that computes and returns the
area \(A = a^2\) of a square with edge lengths \(a\) and
\(a\). In Python, to compute the square of a variable a
you can
write a**2
, or just a*a
.
Example (Python prompt):
>>> square_area(1)
1
>>> square_area(4)
16
Example (IPython prompt):
In [ ]: square_area(1)
Out[ ]: 1
In [ ]: square_area(4)
Out[ ]: 16
rectangle_area(a, b)
¶
Write a function rectangle_area(a, b)
that accepts two arguments
a
and b
. The arguments a
and b
are expected to be
numbers (such as float
s and int
s) that describe the edge
lengths of a rectangle. The function should compute and return the area
of that rectangle (i.e. a*b
).
Examples (IPython prompt)
In [ ]: rectangle_area(1, 2)
Out [ ]: 2
In [ ]: rectangle_area(1.5, 10)
Out [ ]: 15.0
In [ ]: rectangle_area(0, 0.5)
Out [ ]: 0
pyramid_volume(A, h)
¶
Implement a function pyramid_volume(A, h)
that computes and returns
the volume V of a pyramid with base area A and height h. You can use
\(V = \frac{1}{3}Ah\).
Example (Python prompt):
>>> pyramid_volume(1, 2)
0.6666666666666666
Example (IPython prompt):
In [ ]: pyramid_volume(1, 2)
Out[ ]: 0.6666666666666666
distance(a, b)
¶
Write a function distance(a, b)
that returns the distance between
numbers a and b. You can use the abs(x)
function in Python to
compute the absolute value of the number x
.
Examples:
>>> distance(3, 4)
1
>>> distance(3, 1)
2
Examples (IPython):
In [ ]: distance(3, 4)
Out[ ]: 1
In [ ]: distance(3, 1)
Out[ ]: 2
box_volume(a, b, c)
¶
Implement a function box_volume(a, b, c)
that calculates and returns
the volume of a cuboid with edge lengths a
, b
, c
. The volume
\(V\) of a cuboid with edge lengts \(a\), \(b\) and
\(c\) is given by \(V= a b c\).
Examples:
In [ ]: box_volume(1, 1, 1)
Out[ ]: 1
In [ ]: box_volume(1, 2, 3.5)
Out[ ]: 7.0
In [ ]: box_volume(1, 1, 0)
Out[ ]: 0
Once you have completed the tasks by implementing the solutions in a file
with name lab1.py
, please submit this Computing lab1 assignment on
blackboard. (Should the link not work, then start on the blackboard module FEEG1201 and then –> “Semester 1: Computing (Python)” –> “Computing lab1”.)
You should soon (within a few minutes) receive by email a report containing
feedback through an automated analysis of your code. If the automated
analysis says that some test failed, you can change your code and
re-submit the modified lab1.py
as the same assignment. You can repeat
this as often as you want, and the feedback can help you to remove all
errors over time.
We urge you to improve and re-submit your work until you pass all tests. Ask the demonstrators in your scheduled laboratory session for advice to interpret the error messages you may have received in your feedback email if tests have failed. The error messsages are standard Python error messages so it is an important skill to be able to read and understand those (beyond the purpose of this learning activity).
If you do not know how to start, you may find the beginning of Supplementary video 42 helpful (link above).
Additional (voluntary) tasks are available in lab1-extra.
End of lab1.