CBSE Class 11 Computer Science | Python range() Function | Notes, Syntax, Examples & Important Programs
Class 11 · Computer Science
Python range() Function
The range() function is one of the most commonly used built-in functions in Python. It is mainly used with the for loop to generate a sequence of numbers. Instead of writing numbers manually, the range() function automatically generates them according to the specified start, stop, and step values.
Learning Objectives
- Understand the purpose of the
range()function. - Learn different forms of the
range()function. - Generate sequences using start, stop, and step values.
- Use positive and negative step values.
- Write programs using the
range()function.
What is range()?
The range() function generates a sequence of integers. It is commonly used in loops where a block of code needs to execute a specific number of times.
The numbers generated by range() are:
- Ordered
- Immutable
- Generated one at a time
- Stop before the ending value
Syntax
1. range(stop)
range(stop)
2. range(start, stop)
range(start, stop)
3. range(start, stop, step)
range(start, stop, step)
Parameters
| Parameter | Description |
|---|---|
| start | Starting value of the sequence (default = 0) |
| stop | Ending value (not included in the sequence) |
| step | Difference between consecutive numbers (default = 1) |
Important Note
The stop value is never included in the generated sequence.
Example:
range(5)
Generated Values
0 1 2 3 4
Form 1: range(stop)
When only one argument is supplied, Python assumes the starting value as 0 and increments by 1.
Example
for i in range(5):
print(i)
Output
0
1
2
3
4
Form 2: range(start, stop)
This form specifies both the starting and ending values. The ending value is not included.
Example
for i in range(3,8):
print(i)
Output
3
4
5
6
7
Form 3: range(start, stop, step)
This form specifies the starting value, ending value, and increment/decrement value.
Example
for i in range(2,11,2):
print(i)
Output
2
4
6
8
10
Using Negative Step
A negative step generates the sequence in reverse order.
Example
for i in range(10,0,-2):
print(i)
Output
10
8
6
4
2
Example 1: Print Numbers from 1 to 10
for i in range(1,11):
print(i)
Example 2: Print Even Numbers
for i in range(2,21,2):
print(i)
Example 3: Print Odd Numbers
for i in range(1,20,2):
print(i)
Example 4: Reverse Counting
for i in range(10,0,-1):
print(i)
Example 5: Multiples of 5
for i in range(5,51,5):
print(i)
Dry Run
Program
for i in range(2,8,2):
print(i)
| Iteration | Value of i | Output |
|---|---|---|
| 1 | 2 | 2 |
| 2 | 4 | 4 |
| 3 | 6 | 6 |
Real-Life Applications
- Generating serial numbers.
- Printing multiplication tables.
- Displaying roll numbers.
- Processing fixed number of records.
- Generating calendars and schedules.
- Creating menus and reports.
Common Programming Mistakes
1. Assuming the Stop Value is Included
Wrong Expectation
range(1,5)
Many beginners expect the output to be:
1 2 3 4 5
Actual Output
1 2 3 4
2. Incorrect Step Direction
Wrong
for i in range(1,10,-1):
print(i)
Explanation: No output is produced because the step is negative while the stop value is greater than the start value.
3. Step Value Cannot be Zero
Wrong
range(1,10,0)
Explanation: Python raises a ValueError because the step cannot be zero.
CBSE Important Programs
- Print numbers from 1 to 100.
- Print numbers from 100 to 1.
- Print even numbers.
- Print odd numbers.
- Display multiplication table.
- Print multiples of 3.
- Print multiples of 5.
- Display squares of numbers.
- Display cubes of numbers.
- Reverse counting.
Viva Questions
- What is the purpose of the
range()function? - What are the three forms of
range()? - Is the stop value included?
- What is the default value of start?
- What is the default value of step?
- Can the step value be negative?
- Can the step value be zero?
- Can
range()be used without a loop? - Which loop commonly uses
range()? - Write the output of
range(3).
Exam Tips
- Remember that the stop value is excluded.
- Use a positive step for increasing sequences and a negative step for decreasing sequences.
- Never use a step value of zero.
- Choose the correct form of
range()based on the requirement. - Practice predicting the output of different
range()statements.
Quick Revision
range(stop)→ Starts from 0 and stops beforestop.range(start, stop)→ Starts fromstartand stops beforestop.range(start, stop, step)→ Uses a custom increment or decrement.- The stop value is never included.
- The step value cannot be zero.
- Negative step values generate numbers in reverse order.