help(math)
we see that we’ve got pow(x,y)
in addition to sqrt(x)
, so we could use pow(x, 0.5)
to find a square root.sqrt(x)
function is arguably more readable than pow(x, 0.5)
when implementing equations. Readability is a cornerstone of good programming, so it makes sense to provide a special function for this specific common case.Also the math
libraries are likely to be written in an optimsed way.
Using the specific function sqrt
is therefore likely to be more efficient than pow
.
The random module seems like it could help you.
The string has 11 characters, each having a positional index from 0 to 10.
You could use random.randrange
function (or the alias random.randint
if you find that easier to remember) to get a random integer between 0
and 10
, and then pick out the character at that position:
from random import randrange
random_index = randrange(len(bases))
print(bases[random_index])
C
This can be compacted into just
from random import randrange
print(bases[randrange(len(bases))])
G
Perhaps you found the random.sample
function?
It allows for slightly less typing:
from random import sample
print(sample(bases, 1)[0])
C
Note that this function returns a list of values. There are also other functions you could use, but with more convoluted code as a result.
What we need to do is figure out the length of the string bases
, which we will call n_bases
.
Then, we just need to pick a random index from the choices we have, which are [0, 1, 2, ..., n_bases]
.
import math
import random
bases = "ACTTGCTTGAC"
n_bases = len(bases)
idx = random.randrange(n_bases)
print("random base", bases[idx], "base index", idx)
random base T base index 3
You may have used different variable names in your successful solution!
They have forgotten to import the module import math
, the module still needs to be imported before you can get help for it!
The following code will do the job just fine:
import math
help(math)
Filling in the blanks should get you
from math import degrees, pi
angle = degrees(pi / 2)
print(angle)
90.0
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 going to import
a function named degrees
from another library.