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
+34-15Lines changed: 34 additions & 15 deletions
Display the source diff
Display the rich diff
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,7 @@ Python range() built-in function
28
57
# 4
29
58
```
30
59
60
+
Range with both `start` and `stop` parameters
31
61
```python
32
62
>>>for i inrange(1,8):
33
63
...print(i)
@@ -40,6 +70,7 @@ Python range() built-in function
40
70
# 7
41
71
```
42
72
73
+
Range with all parameters specified
43
74
```python
44
75
>>>for i inrange(0,30,5):
45
76
...print(i)
@@ -51,6 +82,7 @@ Python range() built-in function
51
82
# 25
52
83
```
53
84
85
+
Range with all parameters, where the `stop` parameter is not divisible by the `step` parameter
54
86
```python
55
87
>>>for i inrange(0,10,3):
56
88
...print(i)
@@ -60,6 +92,7 @@ Python range() built-in function
60
92
# 9
61
93
```
62
94
95
+
Range with all parameters, where the `stop` and `step` parameters are negative
63
96
```python
64
97
>>>for i inrange(0,-6,-1):
65
98
...print(i)
@@ -71,17 +104,7 @@ Python range() built-in function
71
104
# -5
72
105
```
73
106
74
-
```python
75
-
>>>for i inrange(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
85
108
```python
86
109
>>>for i inrange(0):
87
110
...print(i)
@@ -93,7 +116,3 @@ Python range() built-in function
93
116
...print(i)
94
117
#
95
118
```
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