All Tips!
Else in for:
When having a condition in loop block, we may need to have a variable to set either the condition has been passed or not. Instead of that, Python provides "else" keyword to handle that.
Else will only be executed, when the loop is executed completely without breaking out using "break" statement.
eg.,
Finding the prime number in the range
eg., This can be done as follows
for n in range (2,10):
for x in range (2,n):
if n%x == 0:
break
else:
print (" n is prime);
When having a condition in loop block, we may need to have a variable to set either the condition has been passed or not. Instead of that, Python provides "else" keyword to handle that.
Else will only be executed, when the loop is executed completely without breaking out using "break" statement.
eg.,
Finding the prime number in the range
eg., This can be done as follows
for n in range (2,10):
for x in range (2,n):
if n%x == 0:
break
else:
print (" n is prime);
Comments
Post a Comment