Problem with for loop execution in Python

Problem with for loop execution in Python

Question Body: Hi, I'm trying to write a for loop in Python, but it only runs once. Here's my code:

for i in range(5):
    print(i)
    break

How can I make it print all the numbers from 0 to 4? (Extra info): I don't understand why it only prints 0. I'd appreciate any help. Thanks!

Answer

After print(i), you have given break which breaks the loop. If you want this loop to print from 0 to 4, Remove break. it should be:

for i in range(5):
    print(i)

Enjoyed this article?

Check out more content on our blog or follow us on social media.

Browse more articles