For loop in Python Notes for Class 11 and 12

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.

💡 Exam tip: Loop tracing questions (predict the output) are the most common exam question type for the for loop. The best way to master them is to trace each loop iteration on paper — write down the value of every variable after each step. Practise every example in Python IDLE or the online compiler.

Quick Overview — What You Will Learn

#TopicKey ConceptsExam Weight
1What is a for Loop?Definition, purpose, when to use⭐⭐
2Syntax and Flowfor…in, indentation, loop body⭐⭐⭐
3range() Functionrange(stop), range(start,stop), range(start,stop,step)⭐⭐⭐
4Iterating SequencesStrings, Lists, Tuples, Dictionaries⭐⭐⭐
5else with for Loopelse block executes when loop finishes normally⭐⭐
6Nested for LoopLoop inside a loop, pattern printing⭐⭐⭐
7Jump Statementsbreak, continue, pass⭐⭐⭐
8Pattern ProgramsStar, number, and character patterns⭐⭐⭐
9Common ProgramsSum, factorial, prime, Fibonacci, etc.⭐⭐⭐

Section 1Introduction ⭐⭐

🔄 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.

Definite LoopFixed IterationsIterates over sequencesMost used loop in Python

Section 2Syntax ⭐⭐⭐

📝 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

  1. Python looks at the sequence provided.
  2. It takes the first item from the sequence and assigns it to the loop variable.
  3. It executes all the statements in the loop body.
  4. It goes back, takes the next item, assigns it to the loop variable, and runs the body again.
  5. This continues until all items have been processed.
  6. When the sequence is exhausted, the loop ends and Python moves to the next statement after the loop.
EXAMPLE 1 — Simplest for loop for i in [1, 2, 3, 4, 5]: print(i)# Output: # 1 # 2 # 3 # 4 # 5EXAMPLE 2 — Loop variable takes each value automatically for fruit in [“Apple”, “Banana”, “Mango”]: print(“I like”, fruit)# Output: # I like Apple # I like Banana # I like Mango
Important: The loop variable (i, fruit, etc.) can be named anything you like. After the loop finishes, it retains the value of the last item in the sequence.
for…inLoop variable auto-updatesIndentation is mandatoryRuns once per item

Section 3range() ⭐⭐⭐

🔢 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
FormSyntaxGeneratesExampleOutput
1range(stop)0 to stop−1range(5)0 1 2 3 4
2range(start, stop)start to stop−1range(2, 6)2 3 4 5
3range(start, stop, step)start to stop−1, by steprange(1, 10, 2)1 3 5 7 9
⚠️ Stop value is NEVER included. 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.
💡 How many times will the loop run?
  • range(n) → loop runs n times (0 to n-1)
  • range(a, b) → loop runs b – a times
  • range(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)
range(stop)range(start, stop)range(start, stop, step)Stop always excludedNegative step counts down

Section 4Iterating Sequences ⭐⭐⭐

📋 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) # 3

Iterating 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) # 439

Iterating Over a Tuple

EXAMPLES days = (“Mon”, “Tue”, “Wed”, “Thu”, “Fri”)for day in days: print(day, end=” “) # Output: Mon Tue Wed Thu Fri

Iterating 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
Iterates stringsIterates listsIterates tuplesIterates dictsrange(len()) for index

Section 5else with for ⭐⭐

🔚 else Clause with for Loop

Python allows an optional else block after a for loop. The else block runs only when the loop completes normally — that is, when it finishes all its iterations without being interrupted by a break statement. If the loop is stopped by a break, the else block is skipped.
SYNTAX 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”
else runs after normal completionelse skipped on breakUseful for search loops

Section 6Jump Statements ⭐⭐⭐

⏭️ Jump Statements — break, continue, pass

Jump statements change the normal flow of a loop. Python has three jump statements — break, continue, and pass. Output tracing questions on these are asked in almost every CBSE board exam.

break — Exit the Loop Immediately

break terminates the loop immediately when executed. The remaining iterations are skipped, and Python jumps to the first statement after the loop body. The else block (if present) is also skipped.
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: 12

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 5

pass — 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
StatementWhat it DoesLoop Continues?else block runs?
breakExits the loop completelyNo — loop endsNo — skipped
continueSkips current iteration onlyYes — next iterationYes — after loop ends
passDoes nothing — placeholderYes — unaffectedYes — after loop ends
break → exits loopcontinue → skip iterationpass → placeholder

Section 7Nested Loops ⭐⭐⭐

🔁 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
Total iterations of nested loop = (iterations of outer loop) × (iterations of inner loop)
Example: outer range(3) × inner range(4) = 3 × 4 = 12 total iterations
Inner loop completes fully for each outer iterationTotal = outer × inner

Section 8Pattern Programs ⭐⭐⭐

⭐ 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 5

Pattern 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 5

Pattern 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 E

Pattern 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 * * * * * * * * * * * * * * * *
Section 9Programs ⭐⭐⭐

💻 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 = 120

Program 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 13

Program 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 = 10

Program 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) # 89

Program 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 20

Frequently Asked Questions

What is the difference between for loop and while loop?
A for loop is a definite loop — it is used when the number of iterations is known in advance. It iterates over a sequence or a range. A while loop is an indefinite loop — it keeps running as long as a condition is True, and is used when the number of repetitions is not known beforehand. Both loops can use break, continue, and else.
What is the difference between range(5) and range(1, 6)?
Both generate exactly 5 numbers but different ones. 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.
What happens when break is used inside a for loop that has an else block?
When 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”.
What is the difference between break and continue?
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.
What does range(10, 0, -1) generate?
It generates: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1. The step is −1 so it counts backwards from 10 down to 1. The stop value 0 is excluded. This is used to print countdowns or to iterate in reverse order.
How many times does a nested loop run?
The total number of iterations = (iterations of outer loop) × (iterations of inner loop). For example, if the outer loop has range(4) (4 iterations) and the inner loop has range(3) (3 iterations), the inner loop body runs 4 × 3 = 12 times in total.

Quick Practice Links

Explore More Class 11 CS Notes

Jitendra Singh
✔ Verified Educator

Jitendra Singh

Founder of CBSEPython.in

I help CBSE Class 9–12 students learn Python, Information Technology, Artificial Intelligence and Computer Science through easy notes, quizzes, MCQs and sample papers.

Read More About Me →

Leave a Comment

error: Content is protected !!