In Python, is a single tab equivalent to 2 spaces or 4 spaces, and if so can they be mixed?
Answer
In most code editors, tabs are not the same as 2 spaces or 4 spaces by default.
A tab is stored differently than spaces in the code. Tabs can be seen as a big “jump” in the text, while spaces are always 1 space each. When you move your cursor in the code, you may notice this “jump” when going through tabs as opposed to spaces.
Unlike computers, we can’t easily tell whether an indentation is a tab or spaces. Not knowing this can cause potential errors in the code, and mixing them or wrongly applying them will cause an IndentationError. Because of this, staying consistent with using spaces or tabs in a program can be very important and save a lot of trouble.
Tabs are generally more efficient than spaces because A) the tab character is only a single byte per line whereas spaces can be 2 to 4 bytes per line and B) tabs will be consistent since regardless of how your editor shows you tabs (in 2 or 4 spaces generally but sometimes 3 or even 8 spaces), the compiler still sees a tab character as a tab character and will interpret it the same no matter the environment.
I have experimented couple of indentation combination in below mentioned code
def about_this_computer():
print(“This computer is running on version Everest Puma”)
print(“This is your desktop”)
about_this_computer()
Case 1) If two spaces are used before both the print statement then result is fine
Case 2) If one tab is used before both the print statement then again result is ok
Case 3) If one tab is used for one print statement and 2 spaces are used to another print statement then result is throwing error.
This clearly means either you can use 2 spaces or one tab.
In short only consistency of indentation is important whether you use tab or space.
Continuation lines should align wrapped elements either vertically using Python’s implicit line joining inside parentheses, brackets and braces, or using a hanging indent[7]. When using a hanging indent the following should be considered; there should be no arguments on the first line and further indentation should be used to clearly distinguish itself as a continuation line.’
Tabs should be used solely to remain consistent with code that is already indented with tabs.
Python 3 disallows mixing the use of tabs and spaces for indentation.
Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.
When invoking the Python 2 command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!
Does this mean that one of the first things you need to check when you fork someone’s project or contribute to an open-source project is the indentation used in previously written code?
A single tab may or may not be 2 or 4 spaces, by default it varies from editor or the environment being used, there are also options to set the spaces in tabs, in settings or preferences.
In Codecademy platform a tab is 2 spaces but in IDLE interpreter one space is 4 spaces. So, it depends on the editor or environment used.
I wonder why tabs in Codecademy’s Python course are rendered as 2 tabs rather than 4. It would be a lot easier to read my Python code (as well as to copy it to an editor) if the tabs for Python were rendered as 4 spaces.
Any project like that will use a linter, which will enforce all the silly coding style things, such as not have two empty lines next to each other and the indentation type/size.
Only someone in the know can actually answer that but if I was to guess it would be related to 2 spaces being common in JavaScript and it is a carry over somehow. Or they are just mad and thought 2 was better.