Bil*_*ljk 62 python if-statement
在python中,说是否有区别:
if text == 'sometext':
print(text)
if text == 'nottext':
print("notanytext")
Run Code Online (Sandbox Code Playgroud)
和
if text == 'sometext':
print(text)
elif text == 'nottext':
print("notanytext")
Run Code Online (Sandbox Code Playgroud)
只是想知道多个ifs是否会导致任何不必要的问题,以及是否更好的做法使用elifs.
小智 35
另一个简单的方法来看看使用if和elif之间的区别就是这个例子:
def analyzeAge( age ):
if age < 21:
print "You are a child"
if age >= 21: #Greater than or equal to
print "You are an adult"
else: #Handle all cases where 'age' is negative
print "The age must be a positive integer!"
analyzeAge( 18 ) #Calling the function
>You are a child
>The age must be a positive integer!
Run Code Online (Sandbox Code Playgroud)
在这里你可以看到,当18用作输入时,答案是(令人惊讶的)2个句子.那是错的.它应该只是第一句话.
那是因为BOTH if语句正在评估中.计算机将它们视为两个单独的语句:
该ELIF解决这个问题并且让两个if语句"粘在一起"作为一个:
def analyzeAge( age ):
if age < 21:
print "You are a child"
elif age > 21:
print "You are an adult"
else: #Handle all cases where 'age' is negative
print "The age must be a positive integer!"
analyzeAge( 18 ) #Calling the function
>You are a child
Run Code Online (Sandbox Code Playgroud)
def multipleif(text):
if text == 'sometext':
print(text)
if text == 'nottext':
print("notanytext")
def eliftest(text):
if text == 'sometext':
print(text)
elif text == 'nottext':
print("notanytext")
text = "sometext"
timeit multipleif(text)
100000 loops, best of 3: 5.22 us per loop
timeit eliftest(text)
100000 loops, best of 3: 5.13 us per loop
Run Code Online (Sandbox Code Playgroud)
你可以看到elif稍快一些.如果有更多的ifs和更多的elifs,这将更加明显.
这是另一种思考方式:
假设您有两个特定条件,if/else catchall 结构是不够的:
例子:
我有一个 3 X 3 井字棋盘,我想打印两条对角线的坐标,而不是其余方块的坐标。
我决定改用 if/elif 结构......
for row in range(3):
for col in range(3):
if row == col:
print('diagonal1', '(%s, %s)' % (i, j))
elif col == 2 - row:
print('\t' * 6 + 'diagonal2', '(%s, %s)' % (i, j))
Run Code Online (Sandbox Code Playgroud)
输出是:
diagonal1 (0, 0)
diagonal2 (0, 2)
diagonal1 (1, 1)
diagonal2 (2, 0)
diagonal1 (2, 2)
Run Code Online (Sandbox Code Playgroud)
可是等等!我想包括 diagonal2 的所有三个坐标,因为 (1, 1) 也是对角线 2 的一部分。
The 'elif' caused a dependency with the 'if' so that if the original 'if' was satisfied the 'elif' will not initiate even if the 'elif' logic satisfied the condition as well.
Let's change the second 'elif' to an 'if' instead.
for row in range(3):
for col in range(3):
if row == col:
print('diagonal1', '(%s, %s)' % (i, j))
if col == 2 - row:
print('\t' * 6 + 'diagonal2', '(%s, %s)' % (i, j))
Run Code Online (Sandbox Code Playgroud)
I now get the output that I wanted because the two 'if' statements are mutually exclusive.
diagonal1 (0, 0)
diagonal2 (0, 2)
diagonal1 (1, 1)
diagonal2 (1, 1)
diagonal2 (2, 0)
diagonal1 (2, 2)
Run Code Online (Sandbox Code Playgroud)
Ultimately knowing what kind or result you want to achieve will determine what type of conditional relationship/structure you code.
elif只是一种奇特的表达方式else: if,
多个if测试后执行多个分支,而elif互斥,测试后实际执行一个分支。
以user2333594为例
def analyzeAge( age ):
if age < 21:
print "You are a child"
elif age > 21:
print "You are an adult"
else: #Handle all cases were 'age' is negative
print "The age must be a positive integer!"
Run Code Online (Sandbox Code Playgroud)
可以改写为:
def analyzeAge( age ):
if age < 21:
print "You are a child"
else:
if age > 21:
print "You are an adult"
else: #Handle all cases were 'age' is negative
print "The age must be a positive integer!"
Run Code Online (Sandbox Code Playgroud)
另一个例子可能是:
def analyzeAge( age ):
if age < 21:
print "You are a child"
else: pass #the if end
if age > 21:
print "You are an adult"
else: #Handle all cases were 'age' is negative
print "The age must be a positive integer!"
Run Code Online (Sandbox Code Playgroud)