Jigsaw Puzzle (Parson’s Problem) Programming Example¶
Rearrange the following statements so that a random DNA base is printed and its index in the string. Not all statements may be needed. Feel free to use/add intermediate variables.
bases="ACTTGCTTGAC"
import math
import random
___ = random.randrange(n_bases)
___ = len(bases)
print("random base ", bases[___], "base index", ___)
{: .language-python}
Solution¶
import math
import random
bases = "ACTTGCTTGAC"
n_bases = len(bases)
idx = random.randrange(n_bases)
print("random base", bases[idx], "base index", idx)
{: .language-python} {: .solution} {: .challenge}
When Is Help Available?¶
When a colleague of yours types help(math)
,
Python reports an error:
NameError: name 'math' is not defined
{: .error}
What has your colleague forgotten to do?
Solution¶
Importing the math module (import math
)
{: .solution}
{: .challenge}
Importing With Aliases¶
Fill in the blanks so that the program below prints
90.0
.Rewrite the program so that it uses
import
withoutas
.Which form do you find easier to read?
import math as m
angle = ____.degrees(____.pi / 2)
print(____)
{: .language-python}
Solution¶
import math as m
angle = m.degrees(m.pi / 2)
print(angle)
{: .language-python}
can be written as
import math
angle = math.degrees(math.pi / 2)
print(angle)
{: .language-python}
Since you just wrote the code and are familiar with it, you might actually find the first version easier to read. But when trying to read a huge piece of code written by someone else, or when getting back to your own huge piece of code after several months, non-abbreviated names are often easier, except where there are clear abbreviation conventions. {: .solution} {: .challenge}
There Are Many Ways To Import Libraries!¶
Match the following print statements with the appropriate library calls.
Print commands:
print("sin(pi/2) =", sin(pi/2))
print("sin(pi/2) =", m.sin(m.pi/2))
print("sin(pi/2) =", math.sin(math.pi/2))
Library calls:
from math import sin, pi
import math
import math as m
from math import *
Solution¶
Library calls 1 and 4. In order to directly refer to
sin
andpi
without the library name as prefix, you need to use thefrom ... import ...
statement. Whereas library call 1 specifically imports the two functionssin
andpi
, library call 4 imports all functions in themath
module.Library call 3. Here
sin
andpi
are referred to with a shortened library namem
instead ofmath
. Library call 3 does exactly that using theimport ... as ...
syntax - it creates an alias formath
in the form of the shortened namem
.Library call 2. Here
sin
andpi
are referred to with the regular library namemath
, so the regularimport ...
call suffices. {: .solution} {: .challenge}
Importing Specific Items¶
Fill in the blanks so that the program below prints
90.0
.Do you find this version easier to read than preceding ones?
Why wouldn’t programmers always use this form of
import
?
____ math import ____, ____
angle = degrees(pi / 2)
print(angle)
{: .language-python}
Solution¶
from math import degrees, pi
angle = degrees(pi / 2)
print(angle)
{: .language-python}
Most likely you find this version easier to read since it’s less dense.
The main reason not to use this form of import is to avoid name clashes.
For instance, you wouldn’t import degrees
this way if you also wanted to
use the name degrees
for a variable or function of your own. Or if you
were to also import a function named degrees
from another library.
{: .solution}
{: .challenge}
Reading Error Messages¶
Read the code below and try to identify what the errors are without running it.
Run the code, and read the error message. What type of error is it?
from math import log
log(0)
{: .language-python}
Solution¶
The logarithm of
x
is only defined forx > 0
, so 0 is outside the domain of the function.You get an error of type “ValueError”, indicating that the function received an inappropriate argument value. The additional message “math domain error” makes it clearer what the problem is. {: .solution} {: .challenge}