
Python loops provide a way to execute a simple statement repeatedly until a set number of times. You can execute loops in three ways using different syntax and methods. The main types of loops in Python are while loops, for loops, nested loops, and more. In this article, we will learn more about Python Loops and their functionalities along with interactive examples.
| for i in range(5): print (i) |
![]() |
| while expression: code statement (s) |
| # Initialize a variable count = 1 # Create a while loop while count <= 5: print("Count is:", count) count += 1 # Increment the count print("Loop has ended.") |
![]() |
| fruits = ["apple", "banana", "Guava"] for fruit in fruits: print(fruit) |
![]() |
| # Infinite loop example while True: print("This loop will run forever!") |
![]() |
| count = 0 while True: print("This is loop number:", count + 1) count += 1 if count == 5: print("Loop executed 5 times. Breaking out now.") break |
![]() |
| # Print all numbers from 1 to 5 except 3 for i in range(1, 6): if i == 3: continue # Skip printing 3 print(i) |
![]() |
| # Example: Stop loop when number reaches 3 for i in range(1, 6): if i == 3: break # Exit the loop immediately print(i) |
![]() |