Python loops
For & While
+91-7260058093
[Link]
For loop: Used for iterating over a
sequence (such as a list, tuple, or
string) or executing a block of code a
specific number of times.
While loop: Used for repeating a block
of code as long as a specified
condition is true, allowing for indefinite
iteration until the condition becomes
false.
Use while loops when:
1. You need to repeat a block of code until
a
certain condition is met.
2. You don't know the number of iterations
in advance.
3. You want to create a loop that runs
indefinitely until you manually break it or a
condition becomes false.
Use for loops when:
1. You know the number of iterations in
advance or have a sequence to iterate
through (e.g., lists, tuples, strings, or
ranges).
2. You want to iterate over each item in a
collection.
3. You want to execute a block of code a
specific number of times.