Skip to content

Commit a69f72e

Browse files
committed
done
1 parent b4de26a commit a69f72e

File tree

5 files changed

+264573
-5
lines changed

5 files changed

+264573
-5
lines changed

.ipynb_checkpoints/range()-checkpoint.ipynb

Lines changed: 196 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,202 @@
44
"cell_type": "markdown",
55
"metadata": {},
66
"source": [
7-
"#range()"
7+
"#range()\n",
8+
"\n",
9+
"In this short lecture we will be discussing the range function. We haven't developed a very deep level of knowledge of functions yet, but we can understand the basics of this simple (but extremely useful!) function.\n",
10+
"\n",
11+
"range() allows us to create a list of numbers ranging from a starting point *up to* an ending point. We can also specify step size. Lets walk through a few examples:"
12+
]
13+
},
14+
{
15+
"cell_type": "code",
16+
"execution_count": 7,
17+
"metadata": {
18+
"collapsed": false
19+
},
20+
"outputs": [
21+
{
22+
"data": {
23+
"text/plain": [
24+
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
25+
]
26+
},
27+
"execution_count": 7,
28+
"metadata": {},
29+
"output_type": "execute_result"
30+
}
31+
],
32+
"source": [
33+
"range(0,10)"
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": 8,
39+
"metadata": {
40+
"collapsed": false
41+
},
42+
"outputs": [
43+
{
44+
"data": {
45+
"text/plain": [
46+
"list"
47+
]
48+
},
49+
"execution_count": 8,
50+
"metadata": {},
51+
"output_type": "execute_result"
52+
}
53+
],
54+
"source": [
55+
"x =range(0,10)\n",
56+
"type(x)"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 3,
62+
"metadata": {
63+
"collapsed": true
64+
},
65+
"outputs": [],
66+
"source": [
67+
"start = 0 #Default\n",
68+
"stop = 20 \n",
69+
"x = range(start,stop)"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": 4,
75+
"metadata": {
76+
"collapsed": false
77+
},
78+
"outputs": [
79+
{
80+
"data": {
81+
"text/plain": [
82+
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"
83+
]
84+
},
85+
"execution_count": 4,
86+
"metadata": {},
87+
"output_type": "execute_result"
88+
}
89+
],
90+
"source": [
91+
"x"
92+
]
93+
},
94+
{
95+
"cell_type": "markdown",
96+
"metadata": {},
97+
"source": [
98+
"Great! Notice how it went *up to* 20, but doesn't actually produce 20. Just like in indexing. What about step size? We can specify that as a third argument:"
99+
]
100+
},
101+
{
102+
"cell_type": "code",
103+
"execution_count": 5,
104+
"metadata": {
105+
"collapsed": false
106+
},
107+
"outputs": [
108+
{
109+
"data": {
110+
"text/plain": [
111+
"[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]"
112+
]
113+
},
114+
"execution_count": 5,
115+
"metadata": {},
116+
"output_type": "execute_result"
117+
}
118+
],
119+
"source": [
120+
"x = range(start,stop,2)\n",
121+
"#Show\n",
122+
"x"
123+
]
124+
},
125+
{
126+
"cell_type": "markdown",
127+
"metadata": {},
128+
"source": [
129+
"Awesome! Well thats it...or is it?"
130+
]
131+
},
132+
{
133+
"cell_type": "markdown",
134+
"metadata": {},
135+
"source": [
136+
"### <font color='red'>Python 3 Alert!</font>\n",
137+
"\n",
138+
"You might have been wondering, what happens if I want to use a huge range of numbers? Can my computer store that all in memory?\n",
139+
"\n",
140+
"Great thinking! This is a dilemma that can be solve with the use of a generator. For a simplified explanation: A generator allows the generation of generated objects that are provided at that instance but does not store every instance generated into memory.\n",
141+
"\n",
142+
"This means a generator would not create a list to generate like range() does, but instead provide a one time generation of the numbers in that range. Python 2 has a built-in range generator called xrange(). It is recommended to use xrange() for **for** loops in Python 2. \n",
143+
"\n",
144+
"The good news is in Python 3, range() behaves as a generator and you don't need to worry about it. Let's see a quick example with xrange()"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 9,
150+
"metadata": {
151+
"collapsed": false
152+
},
153+
"outputs": [
154+
{
155+
"name": "stdout",
156+
"output_type": "stream",
157+
"text": [
158+
"0\n",
159+
"1\n",
160+
"2\n",
161+
"3\n",
162+
"4\n",
163+
"5\n",
164+
"6\n",
165+
"7\n",
166+
"8\n",
167+
"9\n"
168+
]
169+
}
170+
],
171+
"source": [
172+
"for num in range(10):\n",
173+
" print num"
174+
]
175+
},
176+
{
177+
"cell_type": "code",
178+
"execution_count": 10,
179+
"metadata": {
180+
"collapsed": false
181+
},
182+
"outputs": [
183+
{
184+
"name": "stdout",
185+
"output_type": "stream",
186+
"text": [
187+
"0\n",
188+
"1\n",
189+
"2\n",
190+
"3\n",
191+
"4\n",
192+
"5\n",
193+
"6\n",
194+
"7\n",
195+
"8\n",
196+
"9\n"
197+
]
198+
}
199+
],
200+
"source": [
201+
"for num in xrange(10):\n",
202+
" print num"
8203
]
9204
},
10205
{

0 commit comments

Comments
 (0)