Skip to content

Commit 14173f7

Browse files
authored
[Edit] Python: range() (#6643)
* [Edit] Python: .range() * minor changes ---------
1 parent 25e031f commit 14173f7

File tree

1 file changed

+57
-21
lines changed
  • content/python/concepts/built-in-functions/terms/range

1 file changed

+57
-21
lines changed
Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
---
22
Title: 'range()'
3-
Description: 'Returns a sequence of numbers based on the given range'
3+
Description: 'Returns a sequence of numbers based on the given range.'
44
Subjects:
5-
- 'Data Science'
65
- 'Computer Science'
6+
- 'Data Science'
77
Tags:
88
- 'Integers'
9-
- 'Range'
109
- 'Functions'
10+
- 'Range'
11+
- 'Values'
1112
CatalogContent:
1213
- 'learn-python-3'
1314
- 'paths/analyze-data-with-python'
1415
---
1516

16-
The `range()` function returns a sequence of numbers based on the given range.
17+
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.
1718

1819
## Syntax
1920

@@ -23,35 +24,70 @@ range(start = 0, stop)
2324
range(start = 0, stop, step = 1)
2425
```
2526

26-
- `stop` (required): specifies where the range will stop, ending at `stop - 1` due to zero-based indexing.
27-
- `start` (optional): specifies where the range will start, defaulting at 0.
28-
- `step` (optional): specifies how many steps are taken between indexes, defaulting to 1.
27+
**Parameters:**
28+
29+
- `stop` (Required): Specifies where the range will stop, ending at `stop - 1` due to zero-based indexing.
30+
- `start` (Optional): Specifies where the range will start, defaulting to `0`.
31+
- `step` (Optional): Specifies how many steps are taken between indices, defaulting to `1`.
32+
33+
**Return value:**
34+
35+
The `range()` function returns a range object, yielding numbers one by one.
36+
37+
## Example 1: Using `range()` with `stop`
38+
39+
This example uses the `range()` function with `stop` set to `5`:
40+
41+
```py
42+
nums = range(5)
43+
44+
print(list(nums))
45+
```
46+
47+
In the example, `list(num)` converts the returned range object into a [list](https://www.codecademy.com/resources/docs/python/lists).
48+
49+
The output for the example will be:
50+
51+
```shell
52+
[0, 1, 2, 3, 4]
53+
```
2954

30-
## Example
55+
## Example 2: Using `range()` with `start` and `stop`
3156

32-
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.
57+
This example uses the `range()` function with `start` set to `6` and `stop` set to `11`:
3358

34-
```python
35-
x = range(5)
36-
y = range(5, 11)
59+
```py
60+
nums = range(6, 11)
3761

38-
print("x: ", list(x))
39-
print("y: ", list(y))
62+
print(list(nums))
4063
```
4164

42-
The output from the code above would look like this:
65+
The output for the example will be:
4366

4467
```shell
45-
x: [0,1,2,3,4]
46-
y: [5,6,7,8,9,10]
68+
[6, 7, 8, 9, 10]
4769
```
4870

49-
## Codebyte Example
71+
## Codebyte Example: Using `range()` with `start`, `stop`, and `step`
5072

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

5375
```codebyte/python
54-
z = range(10, 60, 10)
76+
nums = range(10, 60, 10)
5577
56-
print("z: ", list(z))
78+
print(list(nums))
5779
```
80+
81+
## Frequently Asked Questions
82+
83+
### 1. What is the difference between range() and list(range())?
84+
85+
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.
86+
87+
### 2. Can `range()` be used with negative numbers?
88+
89+
Yes, you can use `range()` with negative values for `start`, `stop`, or `step`. This is commonly used for counting backwards.
90+
91+
### 3. What happens if the step parameter is `0`?
92+
93+
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.

0 commit comments

Comments
 (0)