lab1

Topics: Python functions with one and more arguments, if-then-else, math module

Relevant Socratica videos:

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

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

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 floats and ints) 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

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

triangle_area(a, b, c)

Write a function triangle_area(a, b, c) that computes and returns the area \(A\) of a triangle with edge lengths \(a\), \(b\), and \(c\). You can use the equation

\[A = \sqrt{s(s-a)(s-b)(s-c)} \qquad \mathrm{with} \qquad s = \frac{a + b + c}{2}\]

The sqrt() function is available in the math module.

Examples:

In [ ]: triangle_area(1, 1, 1)
Out[ ]: 0.4330127018922193

In [ ]: triangle_area(2, 1, 1)
Out[ ]: 0.0

In [ ]: triangle_area(6, 5, 5)
Out[ ]: 12.0

signum(x)

Write a function signum(x) that:

  • returns 1 if x \(> 0\)

  • returns 0 if x \(= 0\)

  • returns -1 if x \(< 0\)

Example:

In [ ]: signum(42)
Out[ ]: 1

seconds2days(n)

Implement a function seconds2days(n) which accepts a number n of seconds (either as int or float) and converts the number of seconds in the corresponding number of days. The function should return the number of days as a floating point variable.

Example:

In [ ]: seconds2days(43200)
Out[ ]: 0.5

For your entertainment: how many days are 10! seconds? (10!=3628800).


Once you have completed the tasks by implementing the solutions in a file with name lab1.py, please submit this by email. (You could also do this before you have completed all tasks if you want some earlier fedback on a particular question.)

To submit your your file by email, you attach the file to an email addressed to learn@mpsd.mpg.de and use lab1 as the subject line.

(This pattern for the file name and the subject line is the same for all subsequent days and labs.)

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 teaching staff 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).

Additional (voluntary) tasks are available in lab1-extra.

End of lab1.