Python Range
π Python range() β Full Tutorial
The range() function is used to generate a sequence of numbers, commonly used in for loops.
πΉ 1. Syntax of range()
-
start β Starting number (inclusive, default = 0)
-
stop β Ending number (exclusive)
-
step β Increment (default = 1, can be negative for reverse)
πΉ 2. Using range() with a Single Argument
β Output:
-
Starts from 0
-
Stops before 5
πΉ 3. Using range() with Start and Stop
β Output:
-
Starts from 1
-
Stops before 6
πΉ 4. Using range() with Step
β Output:
-
Step = 2 β increment by 2 each time
Reverse Range (Negative Step)
β Output:
πΉ 5. Converting Range to List, Tuple, Set
πΉ 6. Using Range in Loops
πΉ 7. Nested Loops with Range
πΉ 8. Practical Examples
Sum of first N numbers
Multiplication Table
πΉ 9. Tips
-
range()does not generate a list directly β itβs a lazy sequence (efficient for large numbers). -
Always remember
stopis exclusive. -
Negative
stepcan be used for counting down.
π§ Practice Exercises
-
Print all odd numbers from 1 to 20.
-
Print numbers in reverse from 10 to 1.
-
Print squares of numbers from 1 to 10.
-
Create a list of numbers divisible by 3 using
range(). -
Nested loop: Print a 5×5 grid of numbers.
