Python and Indenting

avatar
(Edited)

If you're not at least marginally interested in coding, this blog post will be completely boring for you, trust me!

I'm completely new to Python but not to programming in various languages.

What I find very hard to adapt to is the special meaning Python associates with indenting, like blocks of code in other languages.

The indenting makes sense in conditional statements, loops, functions and probably more I haven't explored yet.

The solution Python has for blocks of code makes it easier to read because it enforces a certain style, but it also seems very inflexible to someone who isn't used to it.

Let's give you some examples:

This code works

a = 5
print(a)


while the following one doesn't:

a = 5
 print(a)


Python interpreter complains about the potentially accidental space in front of print greeting us with an IndentationError. Easy fix though: you just remove the space.

How about this piece of code? What do you think, does it work?

if a > 0:
print(a)


If you answered no, you are right. You either indent it (with exactly 4 spaces), like this:

if a > 0:
    print(a)


or since you only have one function call or operation, you can put it like this:

if a > 0: print(a)


Let's move to another situation, a little more complex to a beginner in Python. Let's say you want to write an if statement on two rows instead of one. For longer conditions it makes sense, in our case it's just an example, normally it would be all on one line. If you write it like this

if a > 0 and
   a < 10:
    print(a)


you get a SyntaxError error pointing at the and. Any idea what that means? It's less obvious than an IndentationError already.

So, what is wrong with that statement is that we missed a \ at the end of the line containing the error. When you break a line of code in Python because it's too long or because you want to format it in a certain way, you need that character at the end of each line, until the end of the statement, in our case the column (:).

Funny thing is that when you break a line, the following lines don't respect the indentation rules, so something like this is perfectly fine for the interpreter:

if a > 0 and \
                                                   a < 10:
    print(a)


Until now all we had were easy to fix errors the interpreter catches and let us know about them. The problem is when you have logic errors, they are harder to track down and fix.

Here's one example, again focusing on how indenting works in Python.

i = 0
while i < 10:
    print(i)
i = i + 1


Any idea how this piece of code will work? How do you fix it?

Hint: it has something to do with indenting...

I see more potential disruptions due to indenting. For example in a code of a function with hundred of lines, accidentally unindenting a "block" within another "block", if it doesn't make the whole thing crash with errors, will completely change the logic of the code. And that through a simple backspace.

On this particular aspect, I still prefer traditional blocks, with parenthesis or even keywords.

But Python is still worth exploring further.



0
0
0.000
14 comments
avatar

Oh, let me guess. It keeps printing zeros?

I also started playing with Python. It's fun, but I wouldn't write program with nested conditions or any complex structure in it.

0
0
0.000
avatar

Yep! It's an infinite loop.

Well, if you do any kind of complex programming, you'd have to use nested conditions, loops, all in a function def, which is another indent.

0
0
0.000
avatar
(Edited)

.

0
0
0.000
avatar

It's worth to have an editor that can increase or decrease the indentation of code blocks, otherwise it might get lengthy to "just" add another if-statement around an existing code block :)

Yeah, for sure, an editor which isn't well suited for python most likely is frustrating.

But it also helps to write more modular/portable code. At a certain indentation level it's easier to factor out code parts in functions than to not get confused with indentation.

Interesting! Haven't thought about this, but you are right!

0
0
0.000
avatar

I like python, and one of the reasons I like it is the indentation. Makes it so much simpler. Of course your while loop wouldn’t work if you don’t increment i within the block. Wouldn’t you have the same problem in other languages?

0
0
0.000
avatar

Wouldn’t you have the same problem in other languages?

Not really, if the block is delimited by "{" and "}" or by "endif" or other constructs, the block ends where the matching "}" is found, not where the unindented line is.

For someone who is used to PHP, Javascript, C++ etc. syntax, indentation and white spaces are simply ignored by the interpreters / compilers. Quite a leap to go to the opposite, where indentation has a meaning in code.

0
0
0.000
avatar

Don’t you end up indenting in those languages for readability anyway?

0
0
0.000
avatar
(Edited)

Sure! I just prefer the visible delimiters of the blocks and, at the first impression, the fact that a little indenting error doesn't mess up my code.

Update: And I'm not saying here I don't like Python. There are things I like and don't like in any language.

0
0
0.000
avatar

Hello, I am new to python too, I have no knowledge of coding at all only few HTML+CSS, and I will return to your codes when I am ready to practice them, but now I wish u check my progress of python coding from here --- > https://www.iel.pw/2020/01/starting-to-learn-python-at-40.html

0
0
0.000
avatar

If you are completely new to coding, you might be luckier than I am. From what I understood, Python is one of the languages highly recommended when you start learning to code. Powerful both for newbies and for experts.

0
0
0.000
avatar
(Edited)

well, you save lines of code - an efficiency measure for those still measuring the outcome of the programmers by counting lines of code :))

0
0
0.000
avatar

I heard that. And that might work well on certain projects, but not on others. Blockchain code is written all in C++ as far as I know.

I guess it depends how you measure efficiency, as you said: how quickly you write any code, how easy you write a certain type of code, how efficient the algorithm is, how quick the execution of the code is, how much resources it uses etc.

0
0
0.000
avatar

The current state of my life thou am a bit inclined to Django for the sake of web.

Indentation has led me to near-hair-plucking trials.

0
0
0.000
avatar
(Edited)

Indentation has led me to near-hair-plucking trials.

Oh, sorry to hear that, but I get you. I hope it gets better with time and practice.

0
0
0.000