Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet129/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   125   126   127   128   129   130   131   132   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

A compendium of tips

After  more  than  enough  moralising  about  coding  style,  we  get  to  some  simple  tips  of

things in Python that might not be so obvious, especially for newcomers to Python. Some

of the points have been covered in earlier chapters, but we reproduce them here to give a

single resource. This really is just a big list, although we have attempted to split things up

into vague subject categories.

Many  of  the  examples  we  give  here  are  simply  aspects  of  Python  that  are  commonly

overlooked. This may be because non-standard options are easily overlooked, because of



recent additions to the language or because the code looks a bit scary. An example of the

latter would be the following, where zip(*) is used to extract separate lists of the first and

second elements from a list of 2-tuples. So instead of something like:

data = [(2,5),(6,7),(8,7),(8,4),(2,5),(6,4),(3,2)]

xList = [pair[0] for pair in data]

yList = [pair[1] for pair in data]

You might see:

xList, yList = zip(*data)

Without wishing to suggest anything about whether the zip(*) approach is better, it is a

proper part of the language and is something you see in others’ code, so it is good to be

aware of it.

Python  has  many  features  and  it  is  easy  to  overlook  some  of  them,  even  though  they

might be useful. A very simple example of this is the following for calculating logarithms

in any base, which might be done naturally as soon as one learns about the log() function:

from math import log

y = 1e7


x = log(y) / log(10)

As it happens the base for the logarithm is the second argument to the log() function, so

instead we can do:

from math import log

y = 1e7

x = log(y, 10)

This shows the value of carefully reading the Python documentation.


Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   125   126   127   128   129   130   131   132   ...   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