You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description: 'Returns a sequence of numbers based on the given range'
3
+
Description: 'Returns a sequence of numbers based on the given range.'
4
4
Subjects:
5
-
- 'Data Science'
6
5
- 'Computer Science'
6
+
- 'Data Science'
7
7
Tags:
8
8
- 'Integers'
9
-
- 'Range'
10
9
- 'Functions'
10
+
- 'Range'
11
+
- 'Values'
11
12
CatalogContent:
12
13
- 'learn-python-3'
13
14
- 'paths/analyze-data-with-python'
14
15
---
15
16
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.
17
18
18
19
## Syntax
19
20
@@ -23,35 +24,70 @@ range(start = 0, stop)
23
24
range(start = 0, stop, step = 1)
24
25
```
25
26
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
+
```
29
54
30
-
## Example
55
+
## Example 2: Using `range()` with `start` and `stop`
31
56
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`:
33
58
34
-
```python
35
-
x =range(5)
36
-
y =range(5, 11)
59
+
```py
60
+
nums =range(6, 11)
37
61
38
-
print("x: ", list(x))
39
-
print("y: ", list(y))
62
+
print(list(nums))
40
63
```
41
64
42
-
The output from the code above would look like this:
65
+
The output for the example will be:
43
66
44
67
```shell
45
-
x: [0,1,2,3,4]
46
-
y: [5,6,7,8,9,10]
68
+
[6, 7, 8, 9, 10]
47
69
```
48
70
49
-
## Codebyte Example
71
+
## Codebyte Example: Using `range()` with `start`, `stop`, and `step`
50
72
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`:
52
74
53
75
```codebyte/python
54
-
z = range(10, 60, 10)
76
+
nums = range(10, 60, 10)
55
77
56
-
print("z: ", list(z))
78
+
print(list(nums))
57
79
```
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