2018-09-18 23:38:34

Hi there.
Note: I know there are a lot of tutorials around online, but I wanted to try and explain one concept that many python blind developers could find difficult at starting which is indentation.
When I first started, I was struggling with indentation as well, and since I've understood it, I'd like to explain it to people who may find it difficult.
1. What is indentation?
Python's indentation is a way in which the developer can divide his code in sections, in the same way as you would do it with blocks, if you're programming with c style languages.
2. Why is python's indentation this important?
The reason is, that the python interpreter won't understand the code not properly indented, giving you errors. Indentation has been introduced for improving code readability.
3. When to use indentation, and how to use it properly?
As I said in point 1, indentation is used for dividing code in blocks. So when do you actually use it?
You use the python indentation after you begin working on a class, function, loop statement, or a simple if statement.
Since this may be difficult to understand, and since my communicating skills suck baddly I'll provide an example.
Since I'm not able to indent properly from the post edit field, consider the space word, as spaces.
name = 'paul'
if name == 'michael':
space print(name)
What a stupid example, right?
Well this is nothing fancy, but I just wanted to show you when to actually use indentation. As you saw, after the if statement got called I encreased the indentation level, and the statements with that indentation level, will come in to place if the if statement will be true.
In this way, when you want to exit from the if statement you just need to type without indenting. An other example
name = 'paul'
if name == 'michael':
space print('this will not be printed unless the evaluation is true')
print('this will be printed in any case')
4. Better 4 spaces indentation as PEP8 says, or tab, or 1 space indentation?
Respecting PEP8 is always good, but I personally do not find my self confortable with 4 spaces indentation. Is up to you, if you want to indent by 4 spaces, 8, or etc.
That's it. If you have questions, feel free to ask below.
As you saw, my tutorial was pretty bad written since my writing skills are not great, so if you didn't like something, or if you think that I said bullshit, feel free to point it out here.
Hope this kind of tutorial will help some one in the future big_smile.

Paul

2018-09-19 03:44:48

You just indent blocks of code. The contents of an if statement, a for loop, or while loop or any other block statement gets indented one level. SO you have nested ifs, however many ifs is however many levels. You do get to decide the what makes up a level of indentation, the interpreter doesn't care, so long as you make it consistent. If you're collaborating though, it's probably best to stick with the style of the project.

People piss me off with their don't use tabs, use four spaces, the interpreter doesn't care, why should you, stop being a pretentious cunt. As long as all your levels of indentation are the same number of tabs or spaces for each of those same levels, you're totally fine.

One other thing to note, when you unindent, i.e. you close an i, a loop, a class, etc. You can do it all in one line, as in, you don't have to have 4 tabs, new line, 3 tabs, new line, 2 tabs, etc. You can just go from 4 to 0 if you're ending a loop, and class, whatever, doesn't matter. The only caveat, the interpreter does expect to see that line with nothing else on it.

Facts with Tom MacDonald, Adam Calhoun, and Dax
End racism
End division
Become united

2018-09-19 06:57:00

Well, for me, 4 spaces indentation is a pain, because when I use the braille display to read code, it sometimes gets nasty in nested ifs.

Paul

2018-09-19 15:37:57

See, the thing is, not every editor lets you go in and bind the tab key to make four spaces rather than a tab. Another thing is, a standard tab is 4 spaces anyway unless you move the tab stops, so I don't know what the damn fuss is about. I could see python developers being like, let's use two spaces, because if you had a lot of nested blocks of code, you'd have to scroll right to see them,and then scroll back left. I get the workflow pain that would be to not do that, but tabs are four spaces apart anyway. I really don't see people coding much on the actual shell itself, like in the interactive interpreter. Even at a basic level, they're going to be using a text editor, even if it is a CLI based one.

Facts with Tom MacDonald, Adam Calhoun, and Dax
End racism
End division
Become united

2018-09-20 06:05:08

Hi, thanks paul for the indentation, i understand it any ways, but it was nice looking at it again.
Just a question: does it really matter if i have blank lines after a statment? like let's say i made a statement and i want to end it, should i really use a blank line to close the code block? or what?

best regards
never give up on what ever you are doing.

2018-09-20 06:27:25

No it doesn't matter, but it improves readability if you leave blank lines for example between classes or function definitions.

Paul

2018-09-20 07:08:30

Nice one paul! Good job! Btw if you use EdSharp or ride you don't really need to worry that much bout indentation thingy.

2018-09-20 07:20:14

@7 that may be true, but I don't use IDES when programming with python.
I code in the notepad, and then execute in the CMD.

Paul

2018-09-20 17:02:26

does it really not matter about the blank lines, it seems if I go from 4 tabs to 3 tabs and immediately insert a line of code on that line after the tabs, it doesn't like it, but if I leave that line blank, and start a new one with the same number of tabs, its fine. Hmm, it could just have been my having screwed up with the code itself. Most of what I've seen people do though, and I've had to dig through 600 lines of python code once, people generally leave a blank line.

Facts with Tom MacDonald, Adam Calhoun, and Dax
End racism
End division
Become united

2018-09-20 17:31:01

ok, i will then use the blank line.
i also saw that people insert a blank line after a while loop

best regards
never give up on what ever you are doing.

2018-09-20 17:47:22

People will do that after any kind of block. If, while, for, class, def, ... they'll all do that after a block. I agree -- it does make code easier to read. But you don't need to -- you can jump down one level, or 4 levels, and start coding from a new level if you want to. The blocks that are compound (form multiple statements, but lexically look like one) are as follows:

  • If... elif... elif... ... else

  • while

  • for

  • try... except... else... finally/try... finally

  • with

  • Function definitions

  • Class definitions

  • Coroutines (asyncdef, async for, and async with)

  • Displays for lists, sets and dictionaries, if multi-lined

The statements that do not require indentation are as follows, as they are simple statements and forma single statement and not a block: Expression statements, Assignment statements, Augmented assignment statements, and Annotated assignment statements, and The assert, pass, del, return, yield, raise, break, continue, import, global, and nonlocal statements.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github