โ† Back to Python Course
๐Ÿ”„ Loops & Repetitionยท15 minยทBeginner
๐Ÿ“Š

Range

Counting made easy

Discover the range() function โ€” your best friend for counting and repeating in loops.

๐Ÿ“–

What we are learning

The range() function generates a sequence of numbers. You can use it with for loops to count from 1 to 10, from 0 to 100, or even count by 2s or 5s. It is the most common companion of the for loop!

๐Ÿ’ก Think of it like this:

Think of range like a number line maker. You say "give me numbers from 1 to 10" and range creates them for you. Then your for loop walks through each number one at a time.

๐ŸŒŸ

Why it matters

Range is essential for counting loops. Whether you are repeating an action 50 times or counting from 0 to 100 by steps of 10, range makes it simple and clean.

Where you see this in real life:

  • โœ“Counting scores in a game leaderboard
  • โœ“Generating numbered lists like "Item 1, Item 2, Item 3"
  • โœ“Creating timers and countdowns
๐ŸŽฏ

By the end, you can:

  • 1.Use range(n) to count from 0 to n-1
  • 2.Use range(a, b) to count from a to b-1
  • 3.Use range(a, b, step) to count by steps
๐Ÿ”ค

New words

rangestartstopstep
๐Ÿ
If you could count by any number (like counting by 7s), what would you count to and why?
๐Ÿค“

Fun Fact!

range(1000000) creates a million numbers instantly, but Python barely uses any memory because it generates them one at a time!

๐Ÿ“Š The range() Function

range()generates a sequence of numbers. It is the best friend of the for loop! You can use it in three different ways.

Way 1: range(stop)

Gives you numbers from 0 up to (but not including) the stop number.

๐Ÿrange(5) โ†’ 0, 1, 2, 3, 4
main.py
โ— Output
Starts at 0, stops before 5

Way 2: range(start, stop)

Gives you numbers from start up to (but not including) stop.

๐Ÿrange(3, 8) โ†’ 3, 4, 5, 6, 7
main.py
โ— Output
Starts at 3, stops before 8

Way 3: range(start, stop, step)

The third number is the step โ€” how much to jump each time!

๐Ÿrange with steps!
main.py
โ— Output
Click โ–ถ Run to see the output!

๐Ÿงฎ Quick Reference

CodeNumbers Generated
range(5)0, 1, 2, 3, 4
range(2, 6)2, 3, 4, 5
range(0, 10, 2)0, 2, 4, 6, 8
range(10, 0, -1)10, 9, 8, 7, 6, 5, 4, 3, 2, 1
range(1, 20, 5)1, 6, 11, 16
๐Ÿ
Try this: use range() with a step of 3 to count from 0 to 30. Then try counting backwards from 20 to 0!
๐Ÿง 

Quick Quiz!

+10 XP

What numbers does range(2, 10, 2) produce?

๐Ÿ
Finished the lesson? Click the button to mark it complete and earn XP!