Trying to understand nested loops

Mats Wichmann mats at wichmann.us
Fri Aug 5 16:57:41 EDT 2022


On 8/5/22 03:56, GB wrote:
> On 05/08/2022 08:56, Frank Millman wrote:
> 
>> BTW, there is an indentation error in your original post - line 5
>> should line up with line 4. 
> 
> As a Python beginner, I find that Python is annoyingly picky about
> indents.  And, the significance of indents is a bit of a minefield for
> beginners.
> 
> For example, in the above code, the indent of the final line very
> significantly affects the results:
> 
> print(var)
>     print(var)
>         print(var)
> 
> These are all different.

Yes, very picky.  Python has chosen to use a (consistent) indent to
indicate a code block, as opposed to using extra syntactical characters
(curly braces, etc.) to delimit blocks.  It's just a choice that was
made about how to instruct the interpreter what you mean, and there's
some argument that it improves readability later, when you go look at
your code months later, or someone else's code: the requirement to have
consistent indents means your brain can trust that the way the code is
indented is meaningful, rather than arbitrary. Also note that most
(all?) code formatters for other languages will enforce consistent
indenting too, In Python it just happens to be part of the language
rather than optional. So: you need to be clear about what you mean by
indenting correctly.   All good code editors that understand Python
understand about this and will help you as much as they can.


More information about the Python-list mailing list