Skip to content

Commit b0aa83c

Browse files
committed
update
1 parent a69f72e commit b0aa83c

22 files changed

+1553
-79
lines changed

.ipynb_checkpoints/List Comprehensions-checkpoint.ipynb

Lines changed: 201 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,212 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"collapsed": true
7+
},
8+
"source": [
9+
"#Comprehensions\n",
10+
"\n",
11+
"In addition to sequence operations and list methods, Python includes a more advanced operation called a list comprehension.\n",
12+
"\n",
13+
"List comprehensions allow us to build out lists using a different notation. You can think of it as essentially a one line for loop built inside of brackets. For a simple example:\n",
14+
"##Example 1"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"metadata": {
21+
"collapsed": true
22+
},
23+
"outputs": [],
24+
"source": [
25+
"# Grab every letter in string\n",
26+
"lst = [x for x in 'word']"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 2,
32+
"metadata": {
33+
"collapsed": false
34+
},
35+
"outputs": [
36+
{
37+
"data": {
38+
"text/plain": [
39+
"['w', 'o', 'r', 'd']"
40+
]
41+
},
42+
"execution_count": 2,
43+
"metadata": {},
44+
"output_type": "execute_result"
45+
}
46+
],
47+
"source": [
48+
"# Check\n",
49+
"lst"
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"metadata": {},
55+
"source": [
56+
"This is the basic idea of a list comprehension. If you're familiar with mathematical notation this format should feel familiar for example: x^2 : x in { 0,1,2...10} \n",
57+
"\n",
58+
"Lets see a few more example of list comprehensions in Python:\n",
59+
"##Example 2"
60+
]
61+
},
62+
{
63+
"cell_type": "code",
64+
"execution_count": 3,
65+
"metadata": {
66+
"collapsed": true
67+
},
68+
"outputs": [],
69+
"source": [
70+
"# Square numbers in range and turn into list\n",
71+
"lst = [x*2 for x in range(0,11)]"
72+
]
73+
},
74+
{
75+
"cell_type": "code",
76+
"execution_count": 4,
77+
"metadata": {
78+
"collapsed": false
79+
},
80+
"outputs": [
81+
{
82+
"data": {
83+
"text/plain": [
84+
"[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]"
85+
]
86+
},
87+
"execution_count": 4,
88+
"metadata": {},
89+
"output_type": "execute_result"
90+
}
91+
],
92+
"source": [
93+
"lst"
94+
]
95+
},
96+
{
97+
"cell_type": "markdown",
98+
"metadata": {},
99+
"source": [
100+
"##Example 3\n",
101+
"Lets see how to add in if statements:"
102+
]
103+
},
3104
{
4105
"cell_type": "code",
5-
"execution_count": null,
106+
"execution_count": 5,
6107
"metadata": {
7108
"collapsed": true
8109
},
9110
"outputs": [],
10-
"source": []
111+
"source": [
112+
"# Check for even numbers in a range\n",
113+
"lst = [x for x in range(11) if x % 2 == 0]"
114+
]
115+
},
116+
{
117+
"cell_type": "code",
118+
"execution_count": 6,
119+
"metadata": {
120+
"collapsed": false
121+
},
122+
"outputs": [
123+
{
124+
"data": {
125+
"text/plain": [
126+
"[0, 2, 4, 6, 8, 10]"
127+
]
128+
},
129+
"execution_count": 6,
130+
"metadata": {},
131+
"output_type": "execute_result"
132+
}
133+
],
134+
"source": [
135+
"lst"
136+
]
137+
},
138+
{
139+
"cell_type": "markdown",
140+
"metadata": {},
141+
"source": [
142+
"##Example 4\n",
143+
"Can also do more complicated arithmetic:"
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": 7,
149+
"metadata": {
150+
"collapsed": false
151+
},
152+
"outputs": [
153+
{
154+
"data": {
155+
"text/plain": [
156+
"[32.0, 50.0, 68.18, 94.1]"
157+
]
158+
},
159+
"execution_count": 7,
160+
"metadata": {},
161+
"output_type": "execute_result"
162+
}
163+
],
164+
"source": [
165+
"# Convert Celsius to Fahrenheit\n",
166+
"celsius = [0,10,20.1,34.5]\n",
167+
"\n",
168+
"fahrenheit = [ ((float(9)/5)*temp + 32) for temp in celsius ]\n",
169+
"\n",
170+
"fahrenheit"
171+
]
172+
},
173+
{
174+
"cell_type": "markdown",
175+
"metadata": {},
176+
"source": [
177+
"##Example 5\n",
178+
"We can also perform nested list comprehensions, for example:"
179+
]
180+
},
181+
{
182+
"cell_type": "code",
183+
"execution_count": 8,
184+
"metadata": {
185+
"collapsed": false
186+
},
187+
"outputs": [
188+
{
189+
"data": {
190+
"text/plain": [
191+
"[0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000]"
192+
]
193+
},
194+
"execution_count": 8,
195+
"metadata": {},
196+
"output_type": "execute_result"
197+
}
198+
],
199+
"source": [
200+
"lst = [ x**2 for x in [x**2 for x in range(11)]]\n",
201+
"lst"
202+
]
203+
},
204+
{
205+
"cell_type": "markdown",
206+
"metadata": {},
207+
"source": [
208+
"Later on in the course we will learn about generator comprehensions. After this lecture you should feel comfortable reading and writing basic list comprehensions."
209+
]
11210
}
12211
],
13212
"metadata": {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {
6+
"collapsed": true
7+
},
8+
"source": [
9+
"#Nested Statements and Scope \n",
10+
"\n",
11+
"Now that we have gone over on writing our own functions, its important to understand how Python deals with the variable names you assign. When you create a variable name in Python the name is stored in a *namespace*. Variable names also have a *scope*, the scope determines the visbility of that variable name to other parts of your code.\n",
12+
"\n",
13+
"This idea of scope in your code is very important to understand in order to properly assign and call variable names. \n",
14+
"\n",
15+
"In simple terms, the idea of scope can be described by 3 general rules:\n",
16+
"\n",
17+
"1. Name assignments will create or change local names by default.\n",
18+
"2. Name references search (at most) four scopes:\n",
19+
" * local\n",
20+
" * any enclosing functions\n",
21+
" * global\n",
22+
" * built-in\n",
23+
"3. Names declared in global and nonlocal statements map assigned names to enclosing module and function scopes.\n",
24+
"\n",
25+
"**LEGB Rule.**\n",
26+
"\n",
27+
"L: Local — Names assigned in any way within a function (def or lambda)), and not declared global in that function.\n",
28+
"\n",
29+
"E: Enclosing function locals — Name in the local scope of any and all enclosing functions (def or lambda), from inner to outer.\n",
30+
"\n",
31+
"G: Global (module) — Names assigned at the top-level of a module file, or declared global in a def within the file.\n",
32+
"\n",
33+
"B: Built-in (Python) — Names preassigned in the built-in names module : open,range,SyntaxError,..."
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": null,
39+
"metadata": {
40+
"collapsed": true
41+
},
42+
"outputs": [],
43+
"source": []
44+
}
45+
],
46+
"metadata": {
47+
"kernelspec": {
48+
"display_name": "Python 2",
49+
"language": "python",
50+
"name": "python2"
51+
},
52+
"language_info": {
53+
"codemirror_mode": {
54+
"name": "ipython",
55+
"version": 2
56+
},
57+
"file_extension": ".py",
58+
"mimetype": "text/x-python",
59+
"name": "python",
60+
"nbconvert_exporter": "python",
61+
"pygments_lexer": "ipython2",
62+
"version": "2.7.10"
63+
}
64+
},
65+
"nbformat": 4,
66+
"nbformat_minor": 0
67+
}

.ipynb_checkpoints/Nested Statements-checkpoint.ipynb

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)