Python Programming for Biology: Bioinformatics and Beyond


Figure 4.2.  The indentation of code blocks within a Python program using spaces



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

Figure 4.2.  The indentation of code blocks within a Python program using spaces.

In lines of Python code the different program blocks that relate to flow-control statements

like ‘if’, ‘for’ and ‘while’ etc. are indented relative to the start of the line using whitespace.

A flow-control statement in the main part of the program will have no indentation but will

define the start of a program block that is indented to the first level. Any statements that

are indented to this first level will be inside the block of the first statement. A further flow-

control statement at the first level will define a new block at the second indentation level.

Any lines of code that have less indentation than a preceding line are outside the previous

block and are part of the existing block with the same indentation or are outside all blocks

if there is no indentation.

One  distinct  and  important  difference  between  Python  and  many  other  computer

languages is how it deals with the issue of defining execution bocks (that are jumped into).

In  many  programming  languages  (such  as  Java,  Perl,  C,  etc.),  a  block  of  code  generally

has a left curly brace (‘{’) at the start and a right curly brace (‘}’) at the end. In Python the

extent  of  a  block  of  code  is  completely  determined  by  indentation,  i.e.  the  addition  of

blank spaces at the start of the line. The start of a Python code block must be indented by

some  amount  relative  to  whatever  defines  the  block,  such  as  a  function  declaration,  a

conditional statement or loop statement. The content of the block must all have the same

indentation,  except  for  when  there  are  blocks  inside  the  blocks,  which  are  then  indented

further. And finally, the block ends when the indentation ends.

For people moving to Python from other languages the block indentation can take some

getting  used  to.  An  advantage  of  the  Python  block  syntax  is  that  it  removes  clutter  and

makes  the  code  look  cleaner;  there  is  never  a  confusion  of  closing  brackets.  However,



there is one subtle issue: it is generally best to avoid using tab stops in Python code, unless

your  editor  is  set  up  to  automatically  convert  tabs  to  spaces  while  you  are  typing.  The

reason for the potential  problem  is  that  in  Python,  tabs  equate  to  giving  space  out  to  the

column that is the next multiple of 8 counting from the beginning of the line. Many editors

would  show  it  instead  out  to  the  next  multiple  of  4  or  2,  and  so  the  code  would  look

different,  and  this  is  particularly  problematic  if  the  code  mixes  tabs  and  spaces  for

indentation. The number of leading spaces used to indent each block is a matter of taste.

The Python community generally recommends 4 spaces, but in this book we consistently

use 2, allowing all our examples to fit within the width of the pages in a reasonable-size

font.



Download 7,75 Mb.

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