Skip to content

Commit 6d73732

Browse files
authored
Merge pull request wilfredinni#432 from johntheholman/patch-1
Updated range.md with Explanation and Further Examples
2 parents 9044f1d + 0957310 commit 6d73732

File tree

1 file changed

+88
-3
lines changed

1 file changed

+88
-3
lines changed

docs/builtin/range.md

Lines changed: 88 additions & 3 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,62 @@ Python range() built-in function
2857
# 4
2958
```
3059

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 in range(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 in range(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 in range(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 in range(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
108+
```python
109+
>>> for i in range(0):
110+
... print(i)
111+
#
112+
```
113+
114+
```python
115+
>>> for i in range(1,0):
116+
... print(i)
117+
#
118+
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy