Python Loops: for and while
1 min readLoops in Python are used to execute a block of code repeatedly. The two primary types of loops are `for` and `while`.
**For Loops**
`for` loops are used to iterate over a sequence (like a list, tuple, or string):
for item in [1, 2, 3, 4]:
print(item)
**While Loops**
`while` loops execute as long as a condition is true:
count = 0
while count < 5:
print(count)
count += 1
**Break and Continue**
- `break` exits the loop prematurely.
- `continue` skips the rest of the code inside the loop for the current iteration.
**Example**:
for i in range(5):
if i == 3:
break
print(i)
**Conclusion**
Loops are crucial for executing repetitive tasks and iterating over collections. Understanding how to use `for` and `while` loops effectively is essential for programming.