The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Python FAQ. Why does Python use indentation for grouping of statements? Python IndentationError: unexpected indent (How to Fix This Stupid Bug)
By Guest on 8th November 2022 01:17:32 AM | Syntax: PYTHON | Views: 133



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1.  
  2. Why does Python use indentation for grouping of statements?
  3. ===========================================================
  4.  
  5. Guido van Rossum believes that using indentation for grouping is
  6. extremely elegant and contributes a lot to the clarity of the average
  7. Python program. Most people learn to love this feature after a while.
  8.  
  9. Since there are no begin/end brackets there cannot be a disagreement
  10. between grouping perceived by the parser and the human reader.
  11. Occasionally C programmers will encounter a fragment of code like
  12. this:
  13.  
  14.    if (x <= y)
  15.            x++;
  16.            y--;
  17.    z++;
  18.  
  19. Only the "x++" statement is executed if the condition is true, but the
  20. indentation leads many to believe otherwise.  Even experienced C
  21. programmers will sometimes stare at it a long time wondering as to why
  22. "y" is being decremented even for "x > y".
  23.  
  24. Because there are no begin/end brackets, Python is much less prone to
  25. coding-style conflicts.  In C there are many different ways to place
  26. the braces. After becoming used to reading and writing code using a
  27. particular style, it is normal to feel somewhat uneasy when reading
  28. (or being required to write) in a different one.
  29.  
  30. Many coding styles place begin/end brackets on a line by themselves.
  31. This makes programs considerably longer and wastes valuable screen
  32. space, making it harder to get a good overview of a program.  Ideally,
  33. a function should fit on one screen (say, 20--30 lines).  20 lines of
  34. Python can do a lot more work than 20 lines of C.  This is not solely
  35. due to the lack of begin/end brackets -- the lack of declarations and
  36. the high-level data types are also responsible -- but the indentation-
  37. based syntax certainly helps.
  38.  
  39. Python IndentationError: unexpected indent (How to Fix This Stupid Bug)
  40. https://www.youtube.com/watch?v=ceDih3Mb5yE
  41.  
  42. Also see:
  43. Should you use TABS or SPACES for indentation in Python?
  44. https://www.youtube.com/watch?v=hCLf6cNYnCk
  45.  
  46. [embed]
















Python software and documentation are licensed under the PSF License Agreement.
Starting with Python 3.8.6, examples, recipes, and other code in the documentation are dual licensed under the PSF License Agreement and the Zero-Clause BSD license.
Some software incorporated into Python is under different licenses. The licenses are listed with code falling under that license. See Licenses and Acknowledgements for Incorporated Software for an incomplete list of these licenses.

Python and it's documentation is:
Copyright © 2001-2022 Python Software Foundation. All rights reserved.
Copyright © 2000 BeOpen.com. All rights reserved.
Copyright © 1995-2000 Corporation for National Research Initiatives. All rights reserved.
Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved.

See History and License for complete license and permissions information:
https://docs.python.org/3/license.html#psf-license
  • Recent Pastes