Range

In AP Computer Science Principles, range is a built-in Python function that generates a sequence of numbers defined by a start value, a stop value (which is excluded), and a step value, most often used to control how many times a loop repeats.

Verified for the 2027 AP Computer Science Principles examLast updated June 2026

What is range?

range() is Python's way of saying "give me a sequence of numbers." You can call it three ways. range(5) produces 0, 1, 2, 3, 4. range(2, 8) produces 2 through 7. range(2, 10, 2) produces 2, 4, 6, 8, counting by twos. The pattern is range(start, stop, step), and the one rule that trips everyone up is that the stop value is never included. range(5) ends at 4, not 5.

Each number range produces can be assigned to a variable, which is exactly what happens in a for loop like for i in range(5). The variable i holds one value at a time and gets reassigned on every pass, which connects directly to how the CED describes variables (EK AAP-1.A.1) and assignment (EK AAP-1.B.3). In other words, range isn't magic. It's just a sequence generator feeding values into a variable, one at a time.

Why range matters in AP Computer Science Principles

range lives in Unit 3 (Algorithms and Programming) and supports Topic 3.1, specifically learning objectives 3.1.A (represent a value with a variable) and 3.1.B (determine the value of a variable as a result of an assignment). When you trace for i in range(3), you're really tracing a variable getting assigned 0, then 1, then 2, which is the exact skill EK AAP-1.B.3 describes (the value stored in a variable is the most recent value assigned). One important caveat for the exam itself: the AP CSP exam reference sheet uses its own pseudocode, not Python. There is no range on the reference sheet. Instead you'll see REPEAT n TIMES and REPEAT UNTIL (condition). So range matters for two reasons. It's how your class probably writes loops in Python, including the code in your Create Performance Task, and it's the mental model behind the pseudocode iteration you'll trace on the multiple-choice section.

How range connects across the course

start, stop, and step (Unit 3)

These three parameters are the entire anatomy of range. Start is where counting begins (default 0), stop is where it ends without ever being included, and step is the jump size (default 1). If you can answer "what's the last number range actually produces," you've mastered the most common range mistake.

Variable (Unit 3)

A for loop using range is just rapid-fire variable assignment. for i in range(4) assigns 0 to i, runs the body, assigns 1, runs the body, and so on. Tracing that reassignment is exactly the skill in learning objective 3.1.B.

Data Type (Unit 3)

range only works with integers. You can't write range(2.5) or step by 0.5, because the function generates whole-number sequences. That's a clean example of why a value's data type (EK AAP-1.A.3) limits what you can do with it.

Iteration and the exam's REPEAT blocks (Unit 3)

On the exam, the job range does in Python is done by REPEAT n TIMES in pseudocode. for i in range(5) and REPEAT 5 TIMES both run a block five times, so translating between the two is a skill you'll use constantly when moving between class code and exam questions.

Is range on the AP Computer Science Principles exam?

You won't see the word range on the multiple-choice section, because the AP CSP exam uses reference-sheet pseudocode (REPEAT n TIMES, REPEAT UNTIL) instead of Python. What you will do is trace loops where a variable takes on a sequence of values, which is the same thinking range trains. Where range can genuinely appear is in your Create Performance Task code and the Written Response questions tied to it. The 2024 and 2025 Written Response prompts ask you to explain conditional statements, program behavior, and how your program handles unexpected input, all referring to your own code. If your Create project is written in Python and uses range in a loop, you need to explain what values it generates and why your start, stop, and step choices make the program work. Saying "range(10) loops 10 times, producing 0 through 9" precisely is exactly the kind of accurate explanation those questions reward.

Range vs REPEAT n TIMES (AP pseudocode)

Both repeat a block of code a fixed number of times, but they're not identical. REPEAT 5 TIMES just runs the block five times with no counter variable. for i in range(5) runs five times AND hands you a variable i that counts 0, 1, 2, 3, 4 along the way. If a question needs the loop to know which pass it's on, range (or a manually updated counter in pseudocode) is doing extra work that REPEAT alone doesn't.

Key things to remember about range

  • range(start, stop, step) generates a sequence of integers, and the stop value is never included, so range(5) produces 0 through 4.

  • Start defaults to 0 and step defaults to 1, which is why range(5) and range(0, 5, 1) produce the same sequence.

  • In a for loop, range works by reassigning the loop variable a new value on each pass, which is the variable-assignment behavior described in EK AAP-1.B.3.

  • The AP exam's pseudocode does not use range; it uses REPEAT n TIMES and REPEAT UNTIL, so you need to translate between Python loops and exam pseudocode.

  • range only produces integers, so a step of 0.5 or a decimal stop value won't work, tying the function back to data types.

  • If your Create Performance Task code uses range, be ready to explain exactly which values it generates when answering the Written Response questions.

Frequently asked questions about range

What is range in AP Computer Science Principles?

range is a built-in Python function that generates a sequence of integers using start, stop, and step values, like range(2, 10, 2) producing 2, 4, 6, 8. In AP CSP it's most often used to control how many times a for loop repeats.

Is range(5) inclusive of 5?

No. The stop value is always excluded, so range(5) produces 0, 1, 2, 3, 4. If you need the sequence to reach 5, write range(6) or range(1, 6).

Is range on the AP CSP exam reference sheet?

No. The exam reference sheet uses pseudocode like REPEAT n TIMES and REPEAT UNTIL instead of Python's range. You'll only use range in actual Python code, like your Create Performance Task.

What's the difference between range and a list in Python?

A list stores values you put in it, while range generates a number sequence on demand from its start, stop, and step rules. Both can feed a for loop one value at a time, but range is built for counting, not storing arbitrary data.

Can range count backwards or by decimals?

It can count backwards with a negative step, like range(10, 0, -1) producing 10 down to 1, but it cannot use decimals. range only works with integers, so a step like 0.5 throws an error.