How does one know how to correctly indent a code in Python? I'm new to Python and not very proficient at it. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How does one know how to correctly indent a code in Python? I'm new to Python and not very proficient at it.

4th Nov 2016, 3:06 PM
Nicholas Christian
Nicholas Christian - avatar
5 Answers
+ 4
Indentation indicates a relationship between the indented lines and the 'parent' line. Look at it as an immediately local context / like a closed book; it has content but it may not be relevant to what you're doing right now. Take the 'if' statement. You only want to do something if a test is true, or if it isn't true...like if the book is a romance novel you read it, otherwise you put it away. Each time a test can pass/fail, you 'set aside' (indent) code that runs only in that case. Similarly, code indented in a loop only applies to that loop; the last indented line is the 'bottom' of the loop. while DirtyDishes == True: list of tasks if NoDishesLeft: DirtyDishes=False If "DirtyDishes=False" was not indented under the testing 'if', you'd do one dish before stopping (and it would be a logical 'bug', i.e., bad logic).
4th Nov 2016, 3:59 PM
Kirk Schafer
Kirk Schafer - avatar
+ 3
U need reed this: https://www.python.org/dev/peps/pep-0008/
4th Nov 2016, 3:12 PM
Red Raccoon
Red Raccoon - avatar
+ 3
Thank you Alex, great resource. Also Kirk, wonderfully explained, thank you.
4th Nov 2016, 4:18 PM
Eduard Alexandru
Eduard Alexandru - avatar
+ 2
Thanks
4th Nov 2016, 3:44 PM
Nicholas Christian
Nicholas Christian - avatar
+ 2
Thanks Kirk. Great help
4th Nov 2016, 4:03 PM
Nicholas Christian
Nicholas Christian - avatar