Python Forum
Natural Logarithm in Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Natural Logarithm in Python (/thread-2599.html)



Natural Logarithm in Python - yg89 - Mar-27-2017

OHello all,

I'm relatively new at Python; I need to input an equation, but I need help with expressing the Natural Logarithm in Python (ln):

The equation is:

(7.35 x E) + [17.34 x ln(Len)] + [4.96 x ln(Conc)] + [0.89 x ln(DNA)] - 25.42



E, Len, Conc, and DNA are the variables.  I will post what I have later, thanks in advance!!


RE: Natural Logarithm in Python - nilamo - Mar-27-2017

>>> import math
>>> help(math.log)
Help on built-in function log in module math:

log(...)
    log(x[, base])

    Return the logarithm of x to the given base.
    If the base not specified, returns the natural logarithm (base e) of x.

>>>
:)


RE: Natural Logarithm in Python - yg89 - Apr-06-2017

Thanks!!  Big Grin


RE: Natural Logarithm in Python - yg89 - May-01-2017

This is what I have so far... I'd appreciate if someone can take a look:

[Image: cRLurQ]


RE: Natural Logarithm in Python - volcano63 - May-01-2017

(May-01-2017, 12:39 AM)yg89 Wrote: This is what I have so far... I'd appreciate if someone can take a look:

[Image: cRLurQ]

Interesting concept - JPEG as IDE  Naughty . There's a novelty concept for you : copy **arrow** paste  **arrow**  Python tags


RE: Natural Logarithm in Python - yg89 - May-03-2017

Sorry, here it goes:

import math
help(math.log)


E = float(input("DNA strength parameter per base:"))
Len = float(input("Length of nucleotide sequence:"))
Conc = float(input("Sodium concentration of the solution:"))
DNA = float(input("Total nucleotide strand concentration:"))

print (7.35 * E) + 17.34 + math.log(Len) + 4.96 * math.log(Conc) + 0.89 * math.log(DNA) - 25.42



RE: Natural Logarithm in Python - AussieSusan - May-03-2017

So what do you get when compared to a manual calculation?
If you get the same thing then what you have written could be OK (I'm not making any judgement on this - trying to show you how you can check your own work)? (And yes - there is a mistake if the formula you wrote in the OP is correct)
If not then try just one term and make sure that is correct. Then try adding another and so on.
Extra credit/learning: it is generally good practice to perform some level of error checking, especially on any value that is being input by a person. Without getting in to complex numbers, what happens if you (accidentally) enter a negative number and pass that to the 'log' function? Is that what you want to happen? If not then, once you have the code doing the correct thing with known valid numbers, what do you want to do when someone enters an invalid number?
Susan


RE: Natural Logarithm in Python - volcano63 - May-03-2017

(May-03-2017, 01:48 AM)yg89 Wrote:
import math 
print (7.35 * E) + 17.34 + math.log(Len) + 4.96 * math.log(Conc) + 0.89 * math.log(DNA) - 25.42
There should be multiplication sign after 17.34

I would also suggest removing brackets around the first expression - they are redundant, and probably assign constants in the expression to variable values with meaningful names.

It is customary to define constants with all-uppercase names - in Python, you can't have it protected as in C++, but it provides a hint to the reader of you code.


RE: Natural Logarithm in Python - yg89 - May-07-2017

Made the suggested changes and it worked! Thank you all for your help!  Big Grin