Python Programming for Biology: Bioinformatics and Beyond



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

Conditional execution

The ‘if’ statement

A  conditional  statement,  or  ‘if’  statement,  is  used  to  specify  that  some  block  of  code

should  only  be  executed  if  some  associated  test  is  upheld;  a  conditional  expression

evaluates to True. This might also involve subsidiary checks using the ‘elif’ statement (the

English equivalent of which might be ‘or else if the following’) to control an alternative

block  if  the  previous  expression  turns  out  to  be  False.  There  can  even  be  a  final  ‘else’

statement (in English ‘otherwise if all else fails’) to do something if none of the checks are

passed.


To  give  a  solid  example,  the  following  uses  statements  that  test  whether  a  number  is

less  than  zero,  greater  than  zero  or  otherwise  equal  to  zero  and  will  print  out  a  different

message in each case:

if x > 0:

print("Value is positive")

elif x < 0:

print("Value is negative")

else:


print("Value is zero")

The general form of writing out such combined conditional statements is as follows:

if conditionalExpression1:

# codeBlock1

elif conditionalExpression2:

# codeBlock2

plus any number of additional elif statements, then finally:

else:


# codeBlockEnd

The elif statements are optional and independently the else statement is also optional, so

you can just have an isolated if statement, which is a fairly common situation. Each block

has to be indented relative to the if/elif/else. Note also the colon (‘:’) that appears after the

if/else/else statements. This is a mandatory part of the syntax, though it is easy to forget.



Each  block  must  contain  at  least  one  line  of  code,  but  can  have  more.  Python  has  a

‘pass’  statement  that  qualifies  as  a  line  of  code  but  does  nothing.  So  when  you  are  first

setting out your code, you can use this as a placeholder until you get around to adding the

actual desired code:

if someExpression:

pass # fill code in later




Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   42   43   44   45   46   47   48   49   ...   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