Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet111/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   107   108   109   110   111   112   113   114   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

The Python ‘

math

’ module

The  math  module  is  most  useful  for  trigonometric,  exponential  and  logarithmic

functionality.  All  of  the  examples  in  this  section  assume  that  the  math  module

functionality is imported, usually in its entirety with the following statement:

import math

So that, for example, you could to the following, and access the logarithm, square root

and exponential functions using the ‘dot’ notation:

a = math.log(x) # logarithm, base e, of x

b = math.sqrt(x) # square root of x

c = math.exp(x) # e to the power of x

Alternatively, the functions of the math module may be imported individually:



from math import log, sqrt, exp

This method avoids the ‘dot’ notation, and consequently is faster to execute, though you

now have to remember where the function was imported from:

a = log(x)

b = sqrt(x)

c = exp(x)

See

Appendix 3



for a full list of the math module functionality.

As  a  slightly  more  complex  example,  the  following  takes  a  collection  of  angles  in

degrees  and  makes  a  list  containing  their  sines.  This  involves  two  math  operations:

math.sin()  and  math.radians().  Unsurprisingly,  the  former  calculates  the  sine  of  an  angle

that is passed in, but the latter is also required because the sine calculation requires that the

angles  are  in  units  of  radians  (i.e.  2π  for  a  full  circle),  not  degrees.  Hence,  if  we  pass

angles in degrees, math.radians() is used to convert the angle values to radians.

angles = [0, 30, 45, 60, 90, −90]

sines = [math.sin(math.radians(angle)) for angle in angles]

# [0.000, 0.500, 0.707, 0.866, 1.000, −1.000] (rounded to 3 places)

Note that in the above code we use a list comprehension to build a list (sines) from the

inside,  according  to  another  list  (angles);  this  is  more  compact  and  quicker  than  using  a

conventional  for  loop.  Going  a  little  further,  if  the  inverse  operation  (valid  in  the  range

−90 to 90 degrees) were needed then the following implementation would do that, using

math.degrees()  to  convert  from  radians  and  math.asin()  for  the  inverse  sine  operation

(arcsine):

angles = [math.degrees(math.asin(sine)) for sine in sines]


Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   107   108   109   110   111   112   113   114   ...   514




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish