Most of langauge use if-else
, but don’t use for-else
. But, python assist for-else
. In for-else
, else
is executed when the for statement
is executed to the end without being broken by a break in the middle.
In other language, if you confirm that for
is excuted without beign broken, you have to use other variable. But in python, you don’t have to do that. Here is the example.
data = [9, 42, 11, 30]
is_bigger_that_50 = False
for i in data:
if i > 50:
is_bigger_that_50 = True
break
if is_bigger_that_50 == True:
print('There is no number bigger than 50')
data = [2, 4, 5, 11, 3]
for i in data:
if i > 50:
break
else:
print('There is no number bigger than 50')
Those 2 code work samely. You can see the ‘There is no number bigger than 50’ in both.
'There is no number bigger than 50'
PREVIOUS[프로그래머스] 스킬트리 풀이