Does Python code need to be indented

Python recognizes code blocks through indentation.

indentation

The most distinctive feature of Python is the use of indentation to indicate blocks of code. Let me take the if selection structure as an example. If is followed by a condition, if the condition is true, a code block belonging to if is executed.

First look at the expression of C language (note that this is C, not Python!)

if( i   0){
 x =1;
 y =2;}

If i is 0, we will perform the two assignment operations included in the brackets. What is contained in the brackets is the block operation, which belongs to if.

In Python, for the same purpose, this passage is like this

if i   0:
 x =1
 y =2

In Python, the parentheses around i 0 are removed, and the semicolon at the end of each sentence is removed, and the curly braces representing the block have also disappeared.

There is an extra: (colon) after if ..., and the indentation with four spaces before x = 1 and y = 2. Through indentation, Python recognizes that these two sentences belong to if. The reason why Python is designed this way is purely for the beauty of the program.

Example extension:

Python code indentation

Python functions have no obvious begin and end, and no curly braces to indicate the beginning and end of the function. The only separator is a colon (: ), and then the code itself is indented.

For example: indent buil dCon necti onStr ing function

def buildConnectionString(params):"""Build a connection string from a dictionary of parameters.
Returns string."""
return";".join(["%s=%s"%(k, v)for k, v in params.items()])

Code blocks are defined by their indentation. By "code block" I mean: functions, if statements, for loops, while loops, and so on. The start indentation indicates the beginning of the block, and the cancellation of the indentation indicates the end of the block. There are no obvious brackets, braces or keywords. This means that whitespace is important and consistent. In this example, the function code (including the doc string) is indented 4 spaces. It does not have to be 4, as long as they are consistent. The first line that is not indented is considered to be outside the function body.

Recommended Posts

Does Python code need to be indented
Do python programs need to be compiled
Python code to find bugs (2)
How to comment python code
Python code to find bugs(4)
Python code to find bugs (3)
Python code to find bugs(6)
Python code to find bugs (1)
Python code to find bugs(8)
Python code to find bugs(5)
python how to view webpage code
How to wrap in python code
Python handles operation code to execl
Python tricks and tricks-continue to be updated...
200 lines of Python code to achieve snake
Can python code be made into software
Actually very simple | Python code to find bugs (12)
01. Introduction to Python
How to use code running assistant in python
How to set code auto prompt in python
Python how to move the code collectively right
Introduction to Python
How does Python store data to json files
How does python handle the program cannot be opened
Centos 6.4 python 2.6 upgrade to 2.7
How long does it take to learn python by myself?
Minimalism is the soul of Python | Python code to find bugs (10)