-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.html
More file actions
182 lines (133 loc) · 5.57 KB
/
index.html
File metadata and controls
182 lines (133 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<!doctype html>
<html>
<head>
<title>CS106A</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="content-type" content="text/html; charset=UTF8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="../css/style.css">
<!-- Java Script -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="../plugins/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<!-- Python highlighting -->
<script src="../plugins/prism/prism.js"></script>
<link href="../plugins/prism/prism.css" rel="stylesheet" />
<!-- Probability Packages -->
<script src="../plugins/probability/gaussian.js"></script>
<script src="../plugins/color.js"></script>
<!-- font awesome -->
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.0/css/all.css" integrity="sha384-lZN37f5QGtY3VHgisS14W3ExzMWZxybE1SJSEsQp9S+oqd12jhcu+A56Ebc1zFSJ" crossorigin="anonymous">
<!-- SWAL -->
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<!-- Stanford -->
<link href='https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Source+Serif+Pro:400,600,700' rel='stylesheet' type='text/css'>
<!-- Math Jax -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<script src="../plugins/math.min.js"></script>
<script src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
</head>
<body>
<div class="container">
<h1>Python range() Function</h1>
<p>The python <code>range()</code> function creates a collection of numbers on the fly, like
0, 1, 2, 3, 4.
This is very useful, since the numbers can be used to index into collections such as string. The range() function can be called in a few different ways.
<!-- njp could explain about motivation of making index numbers -->
<h2>range(n) - 1 Parameter Form</h2>
<p>The most common form is <code>range(n)</code>, for integer n, which returns a numeric series starting with 0 and extending up to but not including n, e.g. <code>range(5)</code> returns <code>0, 1, 2, 3, 4</code>. This is perfect for generating the index numbers into, for example, a string..
<pre>
>>> s = 'Python'
>>> len(s)
6
>>> for i in range(len(s)):
... print(s[i])
...
P
y
t
h
o
n
</pre>
<p>Below are some more examples calling range(). For cosmetic reasons in the examples below, the call of the range() function is inside a list() so the numbers will print out. This use of list() is only for printing, not needed to use range() in a loop.
<pre>
>>> list(range(5))
[0, 1, 2, 3, 4]
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(2))
[0, 1]
</pre>
<p>What is <code>range(0)</code>? Range is supposed to start at 0 and go up to but not including n. It turns out, the "not including" part dominates, so <code>range(0)</code> is no numbers at all.
<pre>
>>> list(range(1))
[0]
>>> list(range(0)) # n <= 0, no numbers
[]
>>> list(range(-42))
[]
</pre>
<h2>reversed() Variation</h2>
<p>Probably the second most common problem is to go through the standard index numbers, but in reverse order. The <code>reversed()</code> function takes in a linear collection and returns a reversed form of it. This works nicely with range() to go over the regular numbers in reverse order:
<pre>
>>> s = 'Python'
>>> len(s)
6
>>> for i in reversed(range(len(s))):
... print(s[i])
...
n
o
h
t
y
P
</pre>
<h2>range(start, stop) - 2 Parameter Form</h2>
<p>Range with 2 parameters specifies a start number other than 0, but is otherwise like the 1 parameter form above, going up to but not equal to the stop number.
<pre>
>>> list(range(5, 10))
[5, 6, 7, 8, 9]
>>> list(range(5, 7))
[5, 6]
>>> list(range(5, 6))
[5]
>>> list(range(5, 5)) # start >= stop, no numbers
[]
>>> list(range(0, 5))
[0, 1, 2, 3, 4]
>>> list(range(0, 0))
[]
>>> list(range(0, -1))
[]
</pre>
<p>Mnemonic: the "stop" number is <b>strong</b>, so as soon as the numbers hit or exceed the stop the range is done (even if the start number initiates things in that position).
<h2>range(start, stop, step) - 3 Parameter Form</h2>
<p>The 3 parameter form begins with start number, up to but no including the stop number as usual. The difference is the "step" amount between numbers is now custom. Once the number is equal or goes beyond the stop, the range ends. As before, the stop number itself is always omitted.
<pre>
>>> list(range(0, 10, 2))
[0, 2, 4, 6, 8]
>>> list(range(0, 10, 6))
[0, 6]
>>> list(range(200, 800, 100))
[200, 300, 400, 500, 600, 700]
</pre>
<p>If the step is negative, the range decreases from start down to stop. As always, numbers reaching or beyond the stop are omitted, but now step is decreasing.
<pre>
>>> list(range(10, 5, -1))
[10, 9, 8, 7, 6]
>>> list(range(10, 5, -2))
[10, 8, 6]
>>> list(range(6, 5, -2))
[6]
>>> list(range(5, 5, -2)) # equal to stop is omitted
[]
>>> list(range(4, 5, -2)) # beyond the stop is omitted
[]
</pre>
</div></body></html>