Home > Backend Development > Python Tutorial > Is indentation mandatory in Python?

Is indentation mandatory in Python?

anonymity
Release: 2019-06-14 16:24:11
Original
7752 people have browsed it

Whitespace is important in Python. In fact, the white space at the beginning of the line is important. It's called indentation. The whitespace (spaces and tabs) at the beginning of a logical line is used to determine the indentation level of the logical line, and thus the grouping of statements. This means that statements at the same level must have the same indentation. Each group of such statements is called a block. So indentation is mandatory

Is indentation mandatory in Python?

How to indent

Don’t mix tabs and spaces for indentation because this It doesn't work properly when crossing different platforms. I strongly recommend that you use a single tab or two or four spaces at each indentation level.

Choose one of these three indentation styles. More importantly, choose a style and use it consistently, that is, only use that one style.

Python's indentation should be an advantage. It may not be very accustomed to you at first, but you will get used to it slowly!

When you look at other people's Python code, you will deeply feel the benefits of indentation.

In addition, Python's indentation can prevent a large amount of code from stacking together to a certain extent, objectively promoting the formation of a good coding style

Py's philosophy is different from Perl/ruby. It won't give you many functions, and then warns you that it's better not to use this and this, and you should pay attention to that. It believes that whatever it provides you is for your use, and you can use it however you want as long as it conforms to its grammatical specifications. If you can write bad code, there's something wrong with the design. This is a responsible attitude.

{} can allow programmers to write bad code, which is inconsistent with Py's philosophy. Therefore, Py is designed to use indentation instead of {} to represent program blocks.

There is nothing wrong with this, in most cases the indentation is clearer than {}.

Example:

One thing you need to remember is that incorrect indentation will cause errors

i = 5
print 'Value is', i # Error! Notice a single space at the start of the line
print 'I repeat, the value is', i
Copy after login

When you run this program, You will get the following error:

File "whitespace.py", line 4
print 'Value is', i # Error! Notice a single space at the start of the line
^
SyntaxError: invalid syntax
Copy after login

The above is the detailed content of Is indentation mandatory in Python?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template