Python Programming for Biology: Bioinformatics and Beyond


Program control and logic



Download 7,75 Mb.
Pdf ko'rish
bet43/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   39   40   41   42   43   44   45   46   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

4

Program control and logic

Contents

Controlling command execution

Code blocks

Conditional execution

The ‘if’ statement

Comparisons and truth

Logic operations

Loops


The ‘

for


’ loop

The ‘


while

’ loop


Skipping and breaking loops

Positional indices

List comprehension

Looping tips

Error exceptions

Catching exceptions

Triggering exceptions

Finally


Further considerations

Stopping a program

Test code

Controlling command execution

On the whole, a program will normally run by executing the stated commands, one after

the  other  in  sequential  order,  like  reading  the  lines  of  a  book.  Frequently,  however,  you

will need the program to deviate from this, to jump to a line that is not the next line. There

are three main ways of diverting from the line-by-line paradigm. The first way is through

the use of functions (subroutines), where the program’s execution jumps from a particular

line  of  code  to  an  entirely  different  spot,  even  in  a  different  file  or  module,  to  do  a  task

before (usually) jumping back again. You can even jump to a function’s subroutine from




inside another function, and do this repeatedly, so that there is a stack of jumps between

the  current  line  and  the  first  jump  point.  Given  their  importance,  functions  will  be

discussed in a dedicated chapter that follows this one.

The  second  way  of  jumping  between  program  lines  is  through  the  use  of  conditional



statements.  Here  you  can  check  if  some  statement  or  expression  is  true,  and  if  it  is  then

you continue on with the following block of code, otherwise you might skip it or execute a

different  bit  of  code.  The  third  way  is  by  performing  repetitive  loops  through  the  same

lines  of  code,  where  each  time  through  the  loop  different  values  may  be  used  for  the

variables. Usually such loops are either done a specific number of times or until something

in particular happens. When you have a collection of items, like a list, a loop can be used

to  consider  all  of  the  items  in  turn;  the  loop  iterates  over  the  items  of  the  collection.  A

significant number of people who are new to programming find this the hardest idea to get

to grips with, although Python’s syntax makes it about as easy as it can be.


Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   39   40   41   42   43   44   45   46   ...   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