+ 4

Python. What is an indentation? What is a white space? How many spaces do we use?

29th Apr 2019, 9:20 AM
JohnSia2005
9 Antworten
+ 5
You can use as many spaces as you want to indent as long as everything belonging to one block is indented equally. You can do it differently in every block if you like. You can also use spaces or tabs. Really, you got a lot of leeway (see that code down there). From a practical perspective (readability, maintainability and such) it makes sense to use always the same indentation width (most common: 4) and to prefer spaces over tabs (because what you see is what you get). https://code.sololearn.com/c9a0cG9dpVUr/?ref=app
29th Apr 2019, 9:43 AM
HonFu
HonFu - avatar
+ 5
There are certain default values for different languages. C# uses 4 spaces. Haskell uses 8 spaces. For python you can google for the pep-8 standart. i think it had suggested 2 or 4 spaces as intendation. I personally tend to use 2 spaces. But the most commen one i found is 4 spaces. In the end it has to be enough spaces to clearly show what belongs to which scope.
29th Apr 2019, 9:53 AM
Dragonxiv
Dragonxiv - avatar
+ 4
If I get you correctly, you are asking how many spaces are in your indented code. if a == 1: print(1) The number of spaces in each indent is 4. A Whitespace is a newline or an indent. It is also a space.
29th Apr 2019, 9:32 AM
Edwin Pratt
Edwin Pratt - avatar
+ 2
JohnSia2005 it is a pleasure 🙂
29th Apr 2019, 9:37 AM
Edwin Pratt
Edwin Pratt - avatar
+ 1
Edwin Pratt Thx bro, you got me.
29th Apr 2019, 9:33 AM
JohnSia2005
+ 1
I tend to use 2 spaces here because I want to keep my text width as narrow as possible (if anyone like me looks at the codes here on a smartphone). When I write on and for the PC, I rather use 4 spaces.
29th Apr 2019, 10:02 AM
HonFu
HonFu - avatar
+ 1
Yeah. On pc i am using vscode to switch fluently from 2 to 4 spaces and back without a problem.
29th Apr 2019, 10:05 AM
Dragonxiv
Dragonxiv - avatar
+ 1
Indentation can be understood as spaces, in python a block of code is identified as having the same indentation. Eg. def example (): Print("hello") Print('hi') Here hi and hello form a block of code as they are at same indent level. Mostly people tend to use tab space(4 spaces) for indentation. Also python many of times ignore single spaces. I.e. for i in range (5,7): for i in range(5,7): Their is no difference in the above code as per python interpreter
30th Apr 2019, 2:05 AM
Akash
Akash - avatar