image
image
image

Using for loop with else

image

It is possible to include a for loop with else but as an option. The else block will be executed if the items contained in the sequence are exhausted.

Example

Start IDLE.

Navigate to the File menu and click New Window.

Type the following:

marks=[12, 15,17]

for i in marks:

print(i)

else:

print(“No items left”)

Assignment

Write a Python program that prints all prime numbers between 1 and 50.