Skip to content

Commit 0957310

Browse files
committed
Added more context to the range builtin
Added comments to the examples Added explanations of the parameters for range
1 parent 2c1753b commit 0957310

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

docs/builtin/range.md

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,37 @@ Python range() built-in function
1616
</base-disclaimer-content>
1717
</base-disclaimer>
1818

19+
# Basics
20+
21+
The `range` type is commonly used in `for` loops to loop a specific number of times. `range` takes in three parameters, `start`, `stop` and `step`. Each parameter must be intergers (either built-in int or any object that implements the __index__() special method).
22+
23+
If there is only one parameter, it represents the `stop` parameter. If the `step` parameter is omitted at all, it will default to `1`. If the `start` parameter is omitted, it will default to `0`.
24+
25+
Ranges do support negative indices, but these are interpreted as indexing from the end of the sequence determined by the positive indices.
26+
27+
***The advantage of the range type over a regular list or tuple is that a range object will always take the same (small) amount of memory, no matter the size of the range it represents
28+
29+
## Input Parameters:
30+
31+
Ranges implement all of the common sequence operations except concatenation and repetition (due to the fact that range objects can only represent sequences that follow a strict pattern and repetition and concatenation will usually violate that pattern).
32+
33+
`start`:
34+
- The value of the start parameter
35+
- If not supplied, parameter will default to 0
36+
- Parameter value is _inclusive_
37+
38+
`stop`
39+
- The value of the stop parameter
40+
- The only parameter that is required
41+
- Parameter value is _exclusive_
42+
43+
`step`
44+
- The value of the step parameter
45+
- If not supplied, parameter will default to 1
46+
1947
## Examples
2048

49+
Range with only `stop` parameter specified
2150
```python
2251
>>> for i in range(5):
2352
... print(i)
@@ -28,6 +57,7 @@ Python range() built-in function
2857
# 4
2958
```
3059

60+
Range with both `start` and `stop` parameters
3161
```python
3262
>>> for i in range(1,8):
3363
... print(i)
@@ -40,6 +70,7 @@ Python range() built-in function
4070
# 7
4171
```
4272

73+
Range with all parameters specified
4374
```python
4475
>>> for i in range(0,30,5):
4576
... print(i)
@@ -51,6 +82,7 @@ Python range() built-in function
5182
# 25
5283
```
5384

85+
Range with all parameters, where the `stop` parameter is not divisible by the `step` parameter
5486
```python
5587
>>> for i in range(0,10,3):
5688
... print(i)
@@ -60,6 +92,7 @@ Python range() built-in function
6092
# 9
6193
```
6294

95+
Range with all parameters, where the `stop` and `step` parameters are negative
6396
```python
6497
>>> for i in range(0,-6,-1):
6598
... print(i)
@@ -71,17 +104,7 @@ Python range() built-in function
71104
# -5
72105
```
73106

74-
```python
75-
>>> for i in range(0,-6,-1):
76-
... print(i)
77-
# 0
78-
# -1
79-
# -2
80-
# -3
81-
# -4
82-
# -5
83-
```
84-
107+
Two examples where the `stop` parameter is set to 0
85108
```python
86109
>>> for i in range(0):
87110
... print(i)
@@ -93,7 +116,3 @@ Python range() built-in function
93116
... print(i)
94117
#
95118
```
96-
97-
<!-- remove this tag to start editing this page -->
98-
<empty-section />
99-
<!-- remove this tag to start editing this page -->

0 commit comments

Comments
 (0)