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 .
Pages 1