- This event has passed.
Python Loop
May 14, 2024 @ 8:00 am - 5:00 pm
Loops in Python
In programming, loops are used to execute a block of code repeatedly. Python supports two main types of loops: for loops and while loops.
- For Loop:
- The
forloop in Python is used to iterate over a sequence (such as a list, tuple, string, or range). - It executes a block of code for each item in the sequence.
- for item in sequence:
# Code block to be executed - Example
- for i in range(1, 6):
print(i)
- for item in sequence:
- The
- While Loop:
- The
whileloop in Python is used to execute a block of code as long as a condition is true. - It repeatedly executes the code block until the condition becomes false.
- The
-
-
- while condition:
# Code block to be executed - Example
- i = 1
while i <= 5:
print(i)
i += 1
- while condition:
-
- Nested Loops:
- In Python, you can also have loops inside other loops, known as nested loops.
- They are used to execute a loop within another loop.
- Example
- for i in range(1, 4):
for j in range(1, 4):
print(i, j)
