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
Copy file name to clipboardExpand all lines: docs/builtin/range.md
+88-3Lines changed: 88 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -16,8 +16,37 @@ Python range() built-in function
16
16
</base-disclaimer-content>
17
17
</base-disclaimer>
18
18
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
+
19
47
## Examples
20
48
49
+
Range with only `stop` parameter specified
21
50
```python
22
51
>>>for i inrange(5):
23
52
...print(i)
@@ -28,6 +57,62 @@ Python range() built-in function
28
57
# 4
29
58
```
30
59
31
-
<!-- remove this tag to start editing this page -->
32
-
<empty-section />
33
-
<!-- remove this tag to start editing this page -->
60
+
Range with both `start` and `stop` parameters
61
+
```python
62
+
>>>for i inrange(1,8):
63
+
...print(i)
64
+
# 1
65
+
# 2
66
+
# 3
67
+
# 4
68
+
# 5
69
+
# 6
70
+
# 7
71
+
```
72
+
73
+
Range with all parameters specified
74
+
```python
75
+
>>>for i inrange(0,30,5):
76
+
...print(i)
77
+
# 0
78
+
# 5
79
+
# 10
80
+
# 15
81
+
# 20
82
+
# 25
83
+
```
84
+
85
+
Range with all parameters, where the `stop` parameter is not divisible by the `step` parameter
86
+
```python
87
+
>>>for i inrange(0,10,3):
88
+
...print(i)
89
+
# 0
90
+
# 3
91
+
# 6
92
+
# 9
93
+
```
94
+
95
+
Range with all parameters, where the `stop` and `step` parameters are negative
96
+
```python
97
+
>>>for i inrange(0,-6,-1):
98
+
...print(i)
99
+
# 0
100
+
# -1
101
+
# -2
102
+
# -3
103
+
# -4
104
+
# -5
105
+
```
106
+
107
+
Two examples where the `stop` parameter is set to 0
0 commit comments