事实上
行首的空白是重要的。它称为
缩进。在逻辑行首的空白(空格和制表符)用来决定逻辑行的缩进层次,从而用来决定语句的分组。这意味着同一层次的语句
必须有相同的缩进。每一组这样的语句称为一个
块。
-
#!/usr/bin/python
-
# Filename: if.py
-
-
number = 23
-
guess = int(raw_input('Enter an integer : '))
-
-
if guess == number:
-
print 'Congratulations, you guessed it.' # New block starts here
-
print "(but you do not win any prizes!)" # New block ends here
-
elif guess < number:
-
print 'No, it is a little higher than that' # Another block
-
# You can do whatever you want in a block ...
-
else:
-
print 'No, it is a little lower than that'
-
# you must have guess > number to reach here
-
-
print 'Done'
-
# This last statement is always executed, after the if statement is executed
输出
$ python if.py
Enter an integer : 50
No, it is a little lower than that
Done
$ python if.py
Enter an integer : 22
No, it is a little higher than that
Done
$ python if.py
Enter an integer : 23
Congratulations, you guessed it.
(but you do not win any prizes!)
Done
def someFunction():
pass
pass语句在Python中表示一个空的语句块。
空格(行)使用
-
使用 4 个空格缩进。
-
不要使用制表符。
-
不要将制表符和空格混合使用。
-
IDEL和Emacs的Python的都支持 spaces模式。
-
每个函数之间应该有一个空行。
-
每一个 Class 之间应该有两个空行。
阅读(693) | 评论(0) | 转发(1) |