Skip to content

[Edit] Python: range() #6643

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 57 additions & 21 deletions content/python/concepts/built-in-functions/terms/range/range.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
---
Title: 'range()'
Description: 'Returns a sequence of numbers based on the given range'
Description: 'Returns a sequence of numbers based on the given range.'
Subjects:
- 'Data Science'
- 'Computer Science'
- 'Data Science'
Tags:
- 'Integers'
- 'Range'
- 'Functions'
- 'Range'
- 'Values'
CatalogContent:
- 'learn-python-3'
- 'paths/analyze-data-with-python'
---

The `range()` function returns a sequence of numbers based on the given range.
The `range()` [function](https://www.codecademy.com/resources/docs/python/functions) returns a sequence of numbers based on the given range. This function is commonly used for [looping](https://www.codecademy.com/resources/docs/python/loops) a specific number of times in `for` loops. It doesn’t generate all the numbers at once. Instead, it returns a range object that yields numbers individually, making it memory-efficient.

## Syntax

Expand All @@ -23,35 +24,70 @@ range(start = 0, stop)
range(start = 0, stop, step = 1)
```

- `stop` (required): specifies where the range will stop, ending at `stop - 1` due to zero-based indexing.
- `start` (optional): specifies where the range will start, defaulting at 0.
- `step` (optional): specifies how many steps are taken between indexes, defaulting to 1.
**Parameters:**

- `stop` (Required): Specifies where the range will stop, ending at `stop - 1` due to zero-based indexing.
- `start` (Optional): Specifies where the range will start, defaulting to `0`.
- `step` (Optional): Specifies how many steps are taken between indices, defaulting to `1`.

**Return value:**

The `range()` function returns a range object, yielding numbers one by one.

## Example 1: Using `range()` with `stop`

This example uses the `range()` function with `stop` set to `5`:

```py
nums = range(5)

print(list(nums))
```

In the example, `list(num)` converts the returned range object into a [list](https://www.codecademy.com/resources/docs/python/lists).

The output for the example will be:

```shell
[0, 1, 2, 3, 4]
```

## Example
## Example 2: Using `range()` with `start` and `stop`

Here, two variables are created. One with the range set to stop before 5. The other is set to start at 5 and stop before 11. Both are converted to [lists](https://www.codecademy.com/resources/docs/python/lists) before being printed.
This example uses the `range()` function with `start` set to `6` and `stop` set to `11`:

```python
x = range(5)
y = range(5, 11)
```py
nums = range(6, 11)

print("x: ", list(x))
print("y: ", list(y))
print(list(nums))
```

The output from the code above would look like this:
The output for the example will be:

```shell
x: [0,1,2,3,4]
y: [5,6,7,8,9,10]
[6, 7, 8, 9, 10]
```

## Codebyte Example
## Codebyte Example: Using `range()` with `start`, `stop`, and `step`

In the example below, the range is set to 10 and increments by 10, stopping before 60:
This codebyte example uses the `range()` function with `start` set to `10`, `stop` set to `60`, and `step` set to `10`:

```codebyte/python
z = range(10, 60, 10)
nums = range(10, 60, 10)

print("z: ", list(z))
print(list(nums))
```

## Frequently Asked Questions

### 1. What is the difference between range() and list(range())?

The `range()` function returns a range object, yielding numbers one by one. When you wrap it with `list()`, it creates a list containing all the numbers from the range.

### 2. Can `range()` be used with negative numbers?

Yes, you can use `range()` with negative values for `start`, `stop`, or `step`. This is commonly used for counting backwards.

### 3. What happens if the step parameter is `0`?

If you set `step = 0`, Python will raise a [`ValueError`](https://www.codecademy.com/resources/docs/python/errors) because the step must not be zero - this would create an infinite loop.