Python Programming for Biology: Bioinformatics and Beyond



Download 7,75 Mb.
Pdf ko'rish
bet63/514
Sana30.12.2021
Hajmi7,75 Mb.
#91066
1   ...   59   60   61   62   63   64   65   66   ...   514
Bog'liq
[Tim J. Stevens, Wayne Boucher] Python Programming

Return values

There can be more than one return statement in a function, although typically there is only

one,  at  the  bottom.  Consider  the  following  function  to  get  some  text  to  say  whether  a

number  is  positive  or  negative.  It  has  three  return  statements:  the  first  two  return

statements  pass  back  text  strings  but  the  last,  which  would  be  reached  if  the  input  value

were zero, has no explicit return value and thus passes back the Python None object. Any

function code after this final return is ignored.

def getSign(value):

if value > 0:

return 'Positive'

elif value < 0:

return 'Negative'




return

print('Hello world') # This line is ignored

print getSign(33.6) # Result: 'Positive'

print getSign(-7) # Result: 'Negative'

print getSign(0) # Result: None

All of the examples of functions so far have returned only single values; however, it is

possible  to  pass  back  more  than  one  value  via  the  return  statement.  In  the  following

example we define a function that takes two arguments and passes back two values. The

return values are really passed back inside a single tuple, which can be caught as a single

collection  of  values.  However,  if  required  the  returned  values  can  be  extracted

immediately to give a clean syntax.

def calcFunc(x, y):

w = x*x - y*y

z = x*x + y*y

return w, z

values = calcFunc(1.414, 2.0) # Grab output as a whole tuple

print(values) # Result: (-2.000604, 5.999396)

a, b = calcFunc(1.414, 2.0) # Grab individual values

print a # Result: -2.000604

print b # Result: 5.999396



Input arguments

In  the  previous  section  we  saw  that  a  function  can  take  a  specified  number  of  input

arguments.  However,  compared  to  many  programming  languages  Python  is  somewhat

flexible in the way it accepts arguments.




Download 7,75 Mb.

Do'stlaringiz bilan baham:
1   ...   59   60   61   62   63   64   65   66   ...   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