Last updated on July 8th, 2026 at 09:26 pm
For loop in Python
Welcome to the complete for Loop in Python — Class 11 Notes. The for loop is one of the most important topics in CBSE Class 11 Computer Science and it appears in every board paper — either as an output-tracing question or a program-writing question. These notes cover everything in the CBSE syllabus — syntax, range(), nested loops, break, continue, pass, the else clause, and pattern printing programs — with clear explanations, examples, and exam-style questions.
Quick Overview — What You Will Learn
| # | Topic | Key Concepts | Exam Weight |
|---|---|---|---|
| 1 | What is a for Loop? | Definition, purpose, when to use | ⭐⭐ |
| 2 | Syntax and Flow | for…in, indentation, loop body | ⭐⭐⭐ |
| 3 | range() Function | range(stop), range(start,stop), range(start,stop,step) | ⭐⭐⭐ |
| 4 | Iterating Sequences | Strings, Lists, Tuples, Dictionaries | ⭐⭐⭐ |
| 5 | else with for Loop | else block executes when loop finishes normally | ⭐⭐ |
| 6 | Nested for Loop | Loop inside a loop, pattern printing | ⭐⭐⭐ |
| 7 | Jump Statements | break, continue, pass | ⭐⭐⭐ |
| 8 | Pattern Programs | Star, number, and character patterns | ⭐⭐⭐ |
| 9 | Common Programs | Sum, factorial, prime, Fibonacci, etc. | ⭐⭐⭐ |
🔄 What is a for Loop?
A for loop in Python is used to repeat a block of code a fixed number of times — once for each item in a sequence (like a string, list, tuple, or range of numbers). It is called a definite loop because the number of iterations is known before the loop starts.
Think of it like this — imagine you have to write your name 10 times on a page. Without a loop, you would write 10 separate print() statements. With a for loop, you write just 2 lines and Python does the repetition for you.
The for loop is used when you know exactly how many times you want the loop to run, or when you want to process each item in a sequence one by one.
📝 Syntax and Flow of the for Loop
SYNTAX for variable in sequence: statement1 # loop body — must be indented statement2 …- variable — Called the loop variable or iterator variable. It automatically takes the value of each item in the sequence, one at a time, on each iteration.
- sequence — Any iterable — a string, list, tuple, dictionary, or a
range()object. - loop body — The indented block of statements that runs once for every item in the sequence.
- Indentation — Python uses indentation (4 spaces or 1 tab) to mark the loop body. This is not optional — wrong indentation causes an
IndentationError.
How the for Loop Works — Step by Step
- Python looks at the sequence provided.
- It takes the first item from the sequence and assigns it to the loop variable.
- It executes all the statements in the loop body.
- It goes back, takes the next item, assigns it to the loop variable, and runs the body again.
- This continues until all items have been processed.
- When the sequence is exhausted, the loop ends and Python moves to the next statement after the loop.
i, fruit, etc.) can be named anything you like. After the loop finishes, it retains the value of the last item in the sequence.🔢 The range() Function
The range() function generates a sequence of numbers and is used with the for loop when you want to repeat something a specific number of times. It does not create a list — it generates numbers one at a time, saving memory. It has three forms.Form 1 — range(stop)
Generates numbers from 0 to stop-1. The stop value is not included.EXAMPLES for i in range(5): print(i, end=” “) # Output: 0 1 2 3 4for i in range(3): print(“Hello”) # Output: # Hello # Hello # Hello
Form 2 — range(start, stop)
Generates numbers from start to stop-1. Both start and stop are provided. Stop is always excluded.EXAMPLES for i in range(1, 6): print(i, end=” “) # Output: 1 2 3 4 5for i in range(5, 11): print(i, end=” “) # Output: 5 6 7 8 9 10for i in range(2, 2): print(i) # No output — start == stop means empty range
Form 3 — range(start, stop, step)
Generates numbers from start to stop-1, jumping by step each time. Step can be positive (count up) or negative (count down).EXAMPLES # Even numbers 2 to 10 for i in range(2, 11, 2): print(i, end=” “) # Output: 2 4 6 8 10# Odd numbers 1 to 9 for i in range(1, 10, 2): print(i, end=” “) # Output: 1 3 5 7 9# Count down — negative step for i in range(10, 0, –1): print(i, end=” “) # Output: 10 9 8 7 6 5 4 3 2 1# Count down by 2 for i in range(10, 0, –2): print(i, end=” “) # Output: 10 8 6 4 2# Multiples of 3 from 3 to 30 for i in range(3, 31, 3): print(i, end=” “) # Output: 3 6 9 12 15 18 21 24 27 30
| Form | Syntax | Generates | Example | Output |
|---|---|---|---|---|
| 1 | range(stop) | 0 to stop−1 | range(5) | 0 1 2 3 4 |
| 2 | range(start, stop) | start to stop−1 | range(2, 6) | 2 3 4 5 |
| 3 | range(start, stop, step) | start to stop−1, by step | range(1, 10, 2) | 1 3 5 7 9 |
range(1, 5) gives 1, 2, 3, 4 — not 5. This is one of the most common mistakes in board exams. Always remember: stop is excluded.range(n)→ loop runs n times (0 to n-1)range(a, b)→ loop runs b – a timesrange(a, b, s)→ loop runs approximately (b – a) / s times- If step is positive and start >= stop → zero iterations (empty range)
- If step is negative and start <= stop → zero iterations (empty range)
📋 Iterating Over Sequences
The for loop can iterate directly over any sequence — string, list, tuple, or dictionary — without needing range(). The loop variable automatically takes the value of each item in the sequence, one at a time.Iterating Over a String
EXAMPLES # Print each character for ch in “Python”: print(ch, end=” “) # Output: P y t h o n# Count vowels s = “Hello World” count = 0 for ch in s: if ch.lower() in “aeiou”: count += 1 print(“Vowels:”, count) # 3Iterating Over a List
EXAMPLES marks = [85, 92, 78, 96, 88]for m in marks: print(m, end=” “) # Output: 85 92 78 96 88# Find sum of list total = 0 for m in marks: total += m print(“Total:”, total) # 439Iterating Over a Tuple
EXAMPLES days = (“Mon”, “Tue”, “Wed”, “Thu”, “Fri”)for day in days: print(day, end=” “) # Output: Mon Tue Wed Thu FriIterating Over a Dictionary
EXAMPLES student = {“name”: “Arjun”, “class”: 11, “marks”: 92}# Iterates over KEYS by default for key in student: print(key, “→”, student[key]) # Output: # name → Arjun # class → 11 # marks → 92# Iterate over keys explicitly for key in student.keys(): print(key)# Iterate over values for val in student.values(): print(val)# Iterate over key-value pairs for key, val in student.items(): print(key, “=”, val)Using range() with len() to Access Index
EXAMPLES fruits = [“Apple”, “Banana”, “Mango”]for i in range(len(fruits)): print(i, “→”, fruits[i]) # Output: # 0 → Apple # 1 → Banana # 2 → Mango🔚 else Clause with for Loop
Python allows an optionalSYNTAX for variable in sequence: loop body else: # runs when loop finishes normally (no break) statementEXAMPLE 1 — else runs when loop finishes normally for i in range(1, 6): print(i, end=” “) else: print(“\nLoop finished!”)# Output: # 1 2 3 4 5 # Loop finished!EXAMPLE 2 — else is SKIPPED when break is used for i in range(1, 6): if i == 3: break print(i, end=” “) else: print(“Loop finished!”) # This will NOT print# Output: # 1 2EXAMPLE 3 — Practical use: check if a number is prime n = int(input(“Enter a number: “)) for i in range(2, n): if n % i == 0: print(n, “is NOT prime”) break else: print(n, “is PRIME”) # If no factor found, loop ends normally → else runs → “is PRIME” # If a factor is found, break fires → else is skipped → “is NOT prime”elseblock after a for loop. Theelseblock runs only when the loop completes normally — that is, when it finishes all its iterations without being interrupted by abreakstatement. If the loop is stopped by abreak, theelseblock is skipped.
⏭️ Jump Statements — break, continue, pass
Jump statements change the normal flow of a loop. Python has three jump statements —break,continue, andpass. Output tracing questions on these are asked in almost every CBSE board exam.
break — Exit the Loop Immediately
EXAMPLE 1 for i in range(1, 11): if i == 5: break print(i, end=” “) # Output: 1 2 3 4 # When i=5, break fires — loop exits immediatelyEXAMPLE 2 — Search and stop nums = [3, 7, 12, 5, 9] for n in nums: if n % 2 == 0: print(“First even number:”, n) break # Output: First even number: 12breakterminates the loop immediately when executed. The remaining iterations are skipped, and Python jumps to the first statement after the loop body. Theelseblock (if present) is also skipped.
continue — Skip Current Iteration
continue skips the rest of the current iteration and jumps back to the top of the loop for the next iteration. It does not exit the loop — the loop keeps running.EXAMPLE 1
for i in range(1, 11):
if i % 2 == 0:
continue # skip even numbers
print(i, end=” “)
# Output: 1 3 5 7 9EXAMPLE 2 — Skip a specific value
for i in range(1, 6):
if i == 3:
continue
print(i, end=” “)
# Output: 1 2 4 5
# 3 is skipped, but loop continues to 4 and 5pass — Do Nothing (Placeholder)
pass is a null statement — it does nothing. It is used as a placeholder when a statement is syntactically required but you do not want any action to happen. It does not affect loop flow at all.EXAMPLE
for i in range(1, 6):
if i == 3:
pass # does nothing — loop continues normally
print(i, end=” “)
# Output: 1 2 3 4 5
# 3 is printed normally — pass has no effect on output| Statement | What it Does | Loop Continues? | else block runs? |
|---|---|---|---|
| break | Exits the loop completely | No — loop ends | No — skipped |
| continue | Skips current iteration only | Yes — next iteration | Yes — after loop ends |
| pass | Does nothing — placeholder | Yes — unaffected | Yes — after loop ends |
🔁 Nested for Loop
A nested loop is a loop inside another loop. The outer loop runs once, and for each single iteration of the outer loop, the inner loop runs completely from start to finish. Nested loops are used to print patterns, work with 2D data, and generate tables.SYNTAX for i in outer_sequence: # outer loop for j in inner_sequence: # inner loop statement # runs for every (i,j) combinationEXAMPLE 1 — Simple nested loop tracing for i in range(1, 4): # i = 1, 2, 3 for j in range(1, 4): # j = 1, 2, 3 for each i print(i, j, end=” “) print() # newline after each row # Output: # 1 1 1 2 1 3 # 2 1 2 2 2 3 # 3 1 3 2 3 3EXAMPLE 2 — Multiplication table using nested loop for i in range(1, 4): for j in range(1, 11): print(i, “x”, j, “=”, i*j) print() # Prints tables of 1, 2, 3
Example: outer
range(3) × inner range(4) = 3 × 4 = 12 total iterations⭐ Pattern Printing Programs
Pattern programs using nested loops are a very popular question type in CBSE Class 11 board exams. The outer loop controls the number of rows, and the inner loop controls the number of columns (characters printed per row). Study each pattern carefully and trace it row by row.
Pattern 1 — Right-angle triangle of stars
PROGRAM n = 5 for i in range(1, n+1): print(“* “ * i)OUTPUT * * * * * * * * * * * * * * *Pattern 2 — Inverted triangle of stars
PROGRAM n = 5 for i in range(n, 0, –1): print(“* “ * i)OUTPUT * * * * * * * * * * * * * * *Pattern 3 — Number triangle
PROGRAM n = 5 for i in range(1, n+1): for j in range(1, i+1): print(j, end=” “) print()OUTPUT 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5Pattern 4 — Same number on each row
PROGRAM n = 5 for i in range(1, n+1): for j in range(1, i+1): print(i, end=” “) print()OUTPUT 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5Pattern 5 — Pyramid of stars (centered)
PROGRAM n = 5 for i in range(1, n+1): print(” “ * (n – i) + “* “ * i)OUTPUT * * * * * * * * * * * * * * *Pattern 6 — Alphabet triangle
PROGRAM n = 5 for i in range(1, n+1): for j in range(65, 65+i): # 65 is ASCII of ‘A’ print(chr(j), end=” “) print()OUTPUT A A B A B C A B C D A B C D EPattern 7 — Hollow square
PROGRAM n = 5 for i in range(1, n+1): for j in range(1, n+1): if i == 1 or i == n or j == 1 or j == n: print(“*”, end=” “) else: print(” “, end=” “) print()OUTPUT * * * * * * * * * * * * * * * *💻 Common Programs for CBSE Exams and Practicals
Program 1 — Print multiplication table
PROGRAM n = int(input(“Enter a number: “)) for i in range(1, 11): print(n, “x”, i, “=”, n*i)Program 2 — Sum and average of first n natural numbers
PROGRAM n = int(input(“Enter n: “)) total = 0 for i in range(1, n+1): total += i print(“Sum:”, total) print(“Average:”, total/n)Program 3 — Factorial of a number
PROGRAM n = int(input(“Enter a number: “)) fact = 1 for i in range(1, n+1): fact *= i print(“Factorial of”, n, “=”, fact) # 5! = 1×2×3×4×5 = 120Program 4 — Check prime number
PROGRAM n = int(input(“Enter a number: “)) for i in range(2, n): if n % i == 0: print(n, “is NOT prime”) break else: print(n, “is PRIME”)Program 5 — Print all prime numbers up to n
PROGRAM n = int(input(“Enter n: “)) print(“Prime numbers up to”, n, “:”) for num in range(2, n+1): for i in range(2, num): if num % i == 0: break else: print(num, end=” “)Program 6 — Fibonacci series
PROGRAM n = int(input(“How many terms? “)) a, b = 0, 1 for i in range(n): print(a, end=” “) a, b = b, a + b # Output for n=8: 0 1 1 2 3 5 8 13Program 7 — Sum of digits of a number
PROGRAM n = input(“Enter a number: “) total = 0 for ch in n: total += int(ch) print(“Sum of digits:”, total) # “1234” → 1+2+3+4 = 10Program 8 — Count digits, letters, and special characters
PROGRAM s = input(“Enter a string: “) digits = letters = special = 0 for ch in s: if ch.isdigit(): digits += 1 elif ch.isalpha(): letters += 1 else: special += 1 print(“Digits:”, digits) print(“Letters:”, letters) print(“Special characters:”, special)Program 9 — Find largest number in a list
PROGRAM nums = [45, 12, 89, 33, 67, 5] largest = nums[0] for n in nums: if n > largest: largest = n print(“Largest:”, largest) # 89Program 10 — Print even numbers using continue
PROGRAM print(“Even numbers from 1 to 20:”) for i in range(1, 21): if i % 2 != 0: continue # skip odd numbers print(i, end=” “) # Output: 2 4 6 8 10 12 14 16 18 20Frequently Asked Questions
range(5) generates 0, 1, 2, 3, 4 (starts from 0 by default). range(1, 6) generates 1, 2, 3, 4, 5 (starts from 1). The stop value (5 and 6 respectively) is always excluded in both.break exits the loop, the else block is skipped. The else block only runs when the loop completes all its iterations normally — without being stopped by break. This is very useful for search programs — if the item is found (break fires), else is skipped; if not found (loop finishes normally), else runs and reports “not found”.break exits the loop completely — no more iterations happen. continue skips only the current iteration and jumps to the next one — the loop keeps running. Think of it this way: break is like pressing the STOP button on a playlist; continue is like pressing SKIP to jump to the next song.range(4) (4 iterations) and the inner loop has range(3) (3 iterations), the inner loop body runs 4 × 3 = 12 times in total.