CBSE Class 11 Computer Science | Python for Loop | Notes, Syntax, Examples & Important Programs
Class 11 · Computer Science
Python for Loop
Loops are one of the most important control structures in programming. They allow a program to execute a block of code repeatedly without writing the same statements multiple times. In Python, the for loop is used when the number of iterations is known or when we want to iterate over a sequence such as a string, list, tuple, or the values generated by the range() function.
Learning Objectives
- Understand the concept of iteration.
- Learn the syntax of the
forloop. - Execute statements repeatedly using loops.
- Write programs using the
forloop. - Understand the working of loop variables.
What is a Loop?
A loop is a programming construct that repeatedly executes a block of statements until a specified condition is satisfied or until all elements of a sequence have been processed.
Example:
Instead of writing print("Hello") five times, we can use a loop to print it five times automatically.
What is a for Loop?
The for loop is an entry-controlled loop used to iterate over a sequence or a collection of values. It executes the loop body once for every value available in the sequence.
The sequence may be:
- Numbers generated using
range() - String
- List
- Tuple
- Dictionary
Syntax
for variable in sequence:
statements
How does the for Loop Work?
- The sequence is generated.
- The first value is assigned to the loop variable.
- The loop body executes.
- The next value is assigned automatically.
- The process continues until all values are processed.
- When the sequence ends, the loop terminates.
Flowchart
Start
|
Get Sequence
|
More Values Left?
/ \
Yes No
| |
Assign Next End
Value to Variable
|
Execute Statements
|
+-------------+
Example 1: Print Numbers from 1 to 5
for i in range(1,6):
print(i)
Output
1
2
3
4
5
Example 2: Print a Message Five Times
for i in range(5):
print("Welcome to CodeStep Academy")
Example 3: Print Even Numbers
for i in range(2,11,2):
print(i)
Output
2
4
6
8
10
Example 4: Print Odd Numbers
for i in range(1,10,2):
print(i)
Example 5: Find the Square of Numbers
for i in range(1,6):
print(i,"=",i*i)
Example 6: Traverse a String
name="PYTHON"
for ch in name:
print(ch)
Output
P
Y
T
H
O
N
Example 7: Traverse a List
colors=["Red","Blue","Green"]
for c in colors:
print(c)
Dry Run
Program
for i in range(1,4):
print(i)
| Iteration | Value of i | Output |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 3 |
Real-Life Applications
- Printing report cards of all students.
- Processing marks of an entire class.
- Generating multiplication tables.
- Sending reminder emails.
- Displaying menu items.
- Reading data from files.
- Processing lists of employees.
- Displaying product details in online shopping websites.
Advantages of for Loop
- Reduces code repetition.
- Easy to read and understand.
- Suitable when the number of iterations is known.
- Automatically updates the loop variable.
- Improves program efficiency.
Common Programming Mistakes
1. Missing Colon
Wrong
for i in range(5)
print(i)
Correct
for i in range(5):
print(i)
2. Incorrect Indentation
Wrong
for i in range(5):
print(i)
Correct
for i in range(5):
print(i)
3. Incorrect Range
Wrong
for i in range(5,1):
print(i)
Explanation: The loop will not execute because the default step is positive.
CBSE Important Programs
- Print numbers from 1 to 100.
- Print even numbers between 1 and 50.
- Print odd numbers between 1 and 100.
- Print multiplication table of a number.
- Find the square of numbers from 1 to 20.
- Print cubes of first 10 natural numbers.
- Traverse a string.
- Traverse a list.
- Display all vowels in a string.
- Calculate the sum of first N natural numbers.
Viva Questions
- What is a loop?
- What is a
forloop? - When should we use a
forloop? - Can a
forloop iterate over a string? - Can a
forloop iterate over a list? - What is a loop variable?
- What happens after the last iteration?
- Is indentation compulsory in Python?
- Which function is commonly used with a
forloop? - Give any two real-life applications of the
forloop.
Exam Tips
- Use the
forloop when the number of iterations is known. - Always place a colon (
:) after theforstatement. - Maintain proper indentation.
- Use meaningful variable names whenever possible.
- Practice writing programs using different sequences such as strings, lists, and ranges.
Quick Revision
- The
forloop repeats a block of code for every item in a sequence. - It is an entry-controlled loop.
- It works with
range(), strings, lists, tuples, and dictionaries. - Indentation is compulsory.
- It is best suited when the number of iterations is known.