KEMBAR78
Class_XI_Python_LoopsLOOPSINPYTHONO.pptx
Python Loops (For Loop & While Loop)
Class XI Computer Science
CBSE Curriculum
Introduction to Loops
What is a loop?
Repeats a set of instructions
Types: for loop, while loop
Need for Loops
• Reduce code repetition
• Automate repetitive tasks
• Example: Print 1 to 100 easily
For Loop Basics
Syntax:
for variable in sequence:
# block of code
Works with lists, strings, range()
For Loop Example
for i in range(5):
print("Hello", i)
Output:
Hello 0 ... Hello 4
Range() Function
Syntax: range(start, stop, step)
Examples:
range(5) → 0..4
range(2,7) → 2..6
range(1,10,2) → 1,3,5,7,9
Iterating Lists & Strings
for x in [10,20,30]:
print(x)
for ch in "PYTHON":
print(ch)
While Loop Basics
Syntax:
while condition:
# block of code
Runs while condition is True
While Loop Example
i = 1
while i <= 5:
print(i)
i += 1
Output: 1 2 3 4 5
For vs While
For loop: when iterations are known
While loop: when iterations unknown
Example: keep asking until 'exit'
Nested Loops
for i in range(1,4):
for j in range(1,4):
print(i, j)
Summary
• Loops = repetition
• For → sequence-based
• While → condition-based
• Use range() for numbers
• Nested loops for patterns

Class_XI_Python_LoopsLOOPSINPYTHONO.pptx