Loops in Python
Md. Ayub Islam Prince Contact :
Lab In Charge, CSE ( LAB – E )
[email protected]University Of Global Village
What is a Loop?
• Loops are used to execute a block of code
multiple times.
• Two main types of loops in Python:
1. for loop
2. while loop
The for Loop
• A 'for' loop is used to iterate over a sequence
(list, tuple, dictionary, etc.).
• Example:
The while Loop
• A 'while' loop runs as long as a condition is
True.
• Example:
Loop Control Statements
• Python provides control statements to
manage loops:
• break - Exits the loop early.
• continue - Skips the rest of the current
iteration.
• pass - Placeholder statement.
Example: break and
continue
• Example of break: • Example of continue:
Summary
• Loops allow code to run multiple times.
• Python has 'for' and 'while' loops.
• Control statements like break and continue
help manage loops.