Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet24/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   20   21   22   23   24   25   26   27   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Control statements

You have more control in a program than just activating all of the written commands once

in  written  order.  You  can  use  control  commands  to  divert  the  flow  of  the  program’s

execution  under  certain  conditions,  to  add  loops  to  repeat  the  execution  of  certain

statements and to jump to a completely different part of the program, run some commands

and then jump back again. It is very common to use all of these techniques, even in simple

programs. As a simple example you might wish to look at all of the elements of a list in

turn by using a repeating loop, performing the same operation on each of the values.

total = 0

numbers = [1,2,3,4,5,6]

for x in numbers:

total = total + x * x

print(total)

An  example  of  a  loop  in  Python,  specified  using  the for keyword,  where  the  indented  command  is

repeated  several  times  with  different  values.  Here  the  total,  which  starts  as  zero,  is  redefined  several

times  by  adding  a  number  squared  (x  *  x)  to  its  previous  value,  so  that  at  the  end  the  total  is

12+22+32+42+52+62.

The ability to jump from executing the program flow in one place to another, execute

some  commands  and  then  jump  back  again  would  be  described  in  Python  jargon  as  a

function. In some older programming languages you can make the order of your program’s

execution  jump  about  by  using  GOTO  commands;  which  simply  says  that  commands

from  now  on  are  executed  from  a  specified  line  of  code  in  the  program.  In  contrast,  a

function  in  Python  is  a  section  of  code  that  is  bundled  together  with  a  name.  When  the

Python  interpreter  reads  the  commands  that  go  together  to  make  a  function  it  does  not

activate  those  commands  immediately.  Only  when  the  name  of  the  function  is  used

appropriately  in  the  main  flow  of  the  program’s  execution  are  the  commands  from  the

function  run.  At  the  end  of  the  function’s  execution  the  program  flow  goes  back  to  the




point where the function was activated, often sending back some data from the function.

def convertToFahrenheit(celsius):

fahrenheit = celsius * 1.8 + 32.0

return fahrenheit

print(convertToFahrenheit(37.0))

A simple example of a Python function: how the function works is defined in an abstract way after

the def keyword, but the operation is only actually performed on real numbers at the last line.

Just as we write Python modules to store groups of commands as separate files that can

be useful in many separate situations, so too functions are written because they perform a

role that is useful in many different parts of a program. It is generally far better to write a

function to do a particular job once, and then activate or call that function wherever that

job needs to be performed, rather than writing several bits of code that do the same thing.

One note of caution with using Python functions is that they can be proportionally slow to

run compared to the regular flow of a Python program; so if speed is an issue things can

often be helped by removing unnecessary calls to functions. Also, functions are generally

only  useful  if  you  use  them  in  more  than  one  place  in  a  program.  If  a  procedure  is  only

ever  going  to  be  run  in  one  part  of  a  program  you  would  usually  put  the  required

commands directly into the program and not bother with a named function.

Although Python functions can exist on their own, they can also be linked to particular

kinds of data structures. A function that is linked to an object becomes a method of that

object  (a  procedure  that  belongs  to  the  object),  and  can  be  executed  in  the  same  way  as

any other method with the dot notation, as discussed earlier.

class UnitConverter:

def metresToFeet(self, m):

f = m / 0.3048

return f


def feetToMetres(self, f):

m = f * 0.3048

return m

converterObj = UnitConverter()

print(converterObj.metresToFeet(1.89))

print(converterObj.feetToMetres(6.0))



The definition of custom Python objects is specified using the class keyword. Here we have defined a

rudimentary  unit  converter  class  of  object,  which  itself  contains  function  definitions.  The  actual

converter  object  is  created  by  using  the  name  of  the  class  and  from  this  object  we  can  access  the

conversion functions.


Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   20   21   22   23   24   25   26   27   ...   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