Python indentation in question

Accustomed to using C, C ++, Java programming language of the people, might be the beginning of a python are not used, because the various statements if, while, etc., python code block, unlike C language as with braces to contain, but strictly used to indent the code more concise. emphasis is consistent python code blocks, i.e. each block of statements is the same indentation.


Let's look at a few examples

1. When such situations arise is also being given python

>>>   i =5
SyntaxError: unexpected indent
>>> 
i = 0
 print(i)


 print(i)   #报错
 ^
IndentationError: unexpected indent

2. python indentation in the strict control of a uniform code block, the length is not the same when retracted will be given

i = 0
while i < 5:
    print(123)
  print(456) i += 1 print(456) #报错 ^ IndentationError: unindent does not match any outer indentation level 

 

python spaces and allows a single tab (tab key) to indent, generally indented one tab, i.e. four spaces, will automatically help users PyCharm indentation

i = 0
while i < 3:
    print(123)
    print(456) i += 1 输出的结果为 : 123 456 123 456 123 456
i = 0
while i < 3:
 print(123)
 print(456) i += 1 输出的结果为 : 123 456 123 456 123 456

 

Guess you like

Origin www.cnblogs.com/liangweijiang/p/11815994.html