python 学习笔记 缩进(6)

python语言中在行首的空格和制表符用来决定逻辑行的缩进和层次.

如果同一层次的缩进不一致的话, 会产生异常或错误的结果.

例如:

print 'hello world'
print 'hello world'

  以上代码中第一行代码会正常运行, 第二行会抛出异常.

1 i = 3
2 if i == 3:
3 print 'hello world i == 3'
4 print 'hello world'
5 if i < 3:
6 print 'hello world i < 3'
7 print 'Done'

  以上代码中, 如果i为3则打印输出第3,4,7行
  如果 i<3时, 则打印输出6,7行
  

从以上代码可以看到 
相同缩进下的代码会在同一个语句块中.
相同缩进下的代码类似于java中的一对大括号中的内容


 

原文地址:https://www.cnblogs.com/liubin0509/p/2264311.html