Skip to content

Commit 154a05f

Browse files
committed
Adding additional ipython notebooks
1 parent 82f45e8 commit 154a05f

22 files changed

+4645
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:db89df05ffb3b02ecb6fb06d9e0c3c08bf775b72df717fc58718a4ccfe4f5409"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": [
9+
{
10+
"cells": [
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"# Applying Conditional Functions To List Items\n",
16+
"\n",
17+
"This snippit was written by [Chris R. Albon](http://www.chrisralbon.com/) and is part of his collection of [well-documented Python snippits](https://github.com/chrisalbon/code_py). All code is written in Python 3 in iPython notebook and offered under the [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/)."
18+
]
19+
},
20+
{
21+
"cell_type": "markdown",
22+
"metadata": {},
23+
"source": [
24+
"### Create a list of regiment names"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"collapsed": false,
30+
"input": [
31+
"regimentNames = ['Night Riflemen', 'Jungle Scouts', 'The Dragoons', 'Midnight Revengence', 'Wily Warriors']"
32+
],
33+
"language": "python",
34+
"metadata": {},
35+
"outputs": [],
36+
"prompt_number": 5
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"metadata": {},
41+
"source": [
42+
"## Using A Conditional For Loop"
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"### Create a for loop goes through the list and capitalizes an item if the first letter starts with N"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"collapsed": false,
55+
"input": [
56+
"# create a variable for the loop results\n",
57+
"regimentNamesCapitalized_f = []\n",
58+
"\n",
59+
"# for every item in regimentNames\n",
60+
"for i in regimentNames:\n",
61+
" # if the first letter of the regiment's name is N\n",
62+
" if i[0] == \"N\":\n",
63+
" # capitalize the item and add it to regimentNamesCapitalized_f\n",
64+
" regimentNamesCapitalized_f.append(i.upper())\n",
65+
" # otherwise, add it to the new list without changing anything\n",
66+
" else:\n",
67+
" regimentNamesCapitalized_f.append(i)\n",
68+
" \n",
69+
"# View the outcome\n",
70+
"regimentNamesCapitalized_f"
71+
],
72+
"language": "python",
73+
"metadata": {},
74+
"outputs": [
75+
{
76+
"metadata": {},
77+
"output_type": "pyout",
78+
"prompt_number": 9,
79+
"text": [
80+
"['NIGHT RIFLEMEN',\n",
81+
" 'Jungle Scouts',\n",
82+
" 'The Dragoons',\n",
83+
" 'Midnight Revengence',\n",
84+
" 'Wily Warriors']"
85+
]
86+
}
87+
],
88+
"prompt_number": 9
89+
},
90+
{
91+
"cell_type": "markdown",
92+
"metadata": {},
93+
"source": [
94+
"## Using List Comprehension With Conditionals"
95+
]
96+
},
97+
{
98+
"cell_type": "markdown",
99+
"metadata": {},
100+
"source": [
101+
"### Apply the expression x.upper to each item in the list called regiment names if it matches a condition, else apply a different function (or, in this case, do nothing)"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"collapsed": false,
107+
"input": [
108+
"regimentNamesCapitalized_l =[x.upper() if x[0] == \"N\" else x for x in regimentNames]; regimentNamesCapitalized_l"
109+
],
110+
"language": "python",
111+
"metadata": {},
112+
"outputs": [
113+
{
114+
"metadata": {},
115+
"output_type": "pyout",
116+
"prompt_number": 24,
117+
"text": [
118+
"['NIGHT RIFLEMEN',\n",
119+
" 'Jungle Scouts',\n",
120+
" 'The Dragoons',\n",
121+
" 'Midnight Revengence',\n",
122+
" 'Wily Warriors']"
123+
]
124+
}
125+
],
126+
"prompt_number": 24
127+
},
128+
{
129+
"cell_type": "markdown",
130+
"metadata": {},
131+
"source": [
132+
"Note that the syntax for conditional list comprehension is **[x if y else z for k in v]**\n",
133+
"\n",
134+
"- x = function if conditional statement is true\n",
135+
"- y = conditional statement\n",
136+
"- z = function if conditional statement is not true\n",
137+
"- k = items in list\n",
138+
"- v = list variable"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"collapsed": false,
144+
"input": [],
145+
"language": "python",
146+
"metadata": {},
147+
"outputs": []
148+
}
149+
],
150+
"metadata": {}
151+
}
152+
]
153+
}
+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
{
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:47e7e56050a2cbd121f24f123c0c64e5a01a2f75bf3bea63f73798cb9d7e921c"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
8+
"worksheets": [
9+
{
10+
"cells": [
11+
{
12+
"cell_type": "markdown",
13+
"metadata": {},
14+
"source": [
15+
"# Applying Functions To List Items\n",
16+
"\n",
17+
"This snippit was written by [Chris R. Albon](http://www.chrisralbon.com/) and is part of his collection of [well-documented Python snippits](https://github.com/chrisalbon/code_py). All code is written in Python 3 in iPython notebook and offered under the [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/)."
18+
]
19+
},
20+
{
21+
"cell_type": "markdown",
22+
"metadata": {},
23+
"source": [
24+
"### Create a list of regiment names"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"collapsed": false,
30+
"input": [
31+
"regimentNames = ['Night Riflemen', 'Jungle Scouts', 'The Dragoons', 'Midnight Revengence', 'Wily Warriors']"
32+
],
33+
"language": "python",
34+
"metadata": {},
35+
"outputs": [],
36+
"prompt_number": 23
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"metadata": {},
41+
"source": [
42+
"## Using A For Loop"
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"### Create a for loop goes through the list and capitalizes each"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"collapsed": false,
55+
"input": [
56+
"# create a variable for the for loop results\n",
57+
"regimentNamesCapitalized_f = []\n",
58+
"\n",
59+
"# for every item in regimentNames\n",
60+
"for i in regimentNames:\n",
61+
" # capitalize the item and add it to regimentNamesCapitalized_f\n",
62+
" regimentNamesCapitalized_f.append(i.upper())\n",
63+
" \n",
64+
"# View the outcome\n",
65+
"regimentNamesCapitalized_f"
66+
],
67+
"language": "python",
68+
"metadata": {},
69+
"outputs": [
70+
{
71+
"metadata": {},
72+
"output_type": "pyout",
73+
"prompt_number": 26,
74+
"text": [
75+
"['NIGHT RIFLEMEN',\n",
76+
" 'JUNGLE SCOUTS',\n",
77+
" 'THE DRAGOONS',\n",
78+
" 'MIDNIGHT REVENGENCE',\n",
79+
" 'WILY WARRIORS']"
80+
]
81+
}
82+
],
83+
"prompt_number": 26
84+
},
85+
{
86+
"cell_type": "markdown",
87+
"metadata": {},
88+
"source": [
89+
"## Using Map()"
90+
]
91+
},
92+
{
93+
"cell_type": "markdown",
94+
"metadata": {},
95+
"source": [
96+
"### Create a lambda function that capitalizes x"
97+
]
98+
},
99+
{
100+
"cell_type": "code",
101+
"collapsed": false,
102+
"input": [
103+
"capitalizer = lambda x: x.upper()"
104+
],
105+
"language": "python",
106+
"metadata": {},
107+
"outputs": [],
108+
"prompt_number": 25
109+
},
110+
{
111+
"cell_type": "markdown",
112+
"metadata": {},
113+
"source": [
114+
"### Map the capitalizer function to regimentNames, convert the map into a list, and view the variable"
115+
]
116+
},
117+
{
118+
"cell_type": "code",
119+
"collapsed": false,
120+
"input": [
121+
"regimentNamesCapitalized_m = list(map(capitalizer, regimentNames)); regimentNamesCapitalized_m"
122+
],
123+
"language": "python",
124+
"metadata": {},
125+
"outputs": [
126+
{
127+
"metadata": {},
128+
"output_type": "pyout",
129+
"prompt_number": 18,
130+
"text": [
131+
"['NIGHT RIFLEMEN',\n",
132+
" 'JUNGLE SCOUTS',\n",
133+
" 'THE DRAGOONS',\n",
134+
" 'MIDNIGHT REVENGENCE',\n",
135+
" 'WILY WARRIORS']"
136+
]
137+
}
138+
],
139+
"prompt_number": 18
140+
},
141+
{
142+
"cell_type": "markdown",
143+
"metadata": {},
144+
"source": [
145+
"## Using List Comprehension"
146+
]
147+
},
148+
{
149+
"cell_type": "markdown",
150+
"metadata": {},
151+
"source": [
152+
"### Apply the expression x.upper to each item in the list called regiment names. Then view the output"
153+
]
154+
},
155+
{
156+
"cell_type": "code",
157+
"collapsed": false,
158+
"input": [
159+
"regimentNamesCapitalized_l = [x.upper() for x in regimentNames]; regimentNamesCapitalized_l"
160+
],
161+
"language": "python",
162+
"metadata": {},
163+
"outputs": [
164+
{
165+
"metadata": {},
166+
"output_type": "pyout",
167+
"prompt_number": 22,
168+
"text": [
169+
"['NIGHT RIFLEMEN',\n",
170+
" 'JUNGLE SCOUTS',\n",
171+
" 'THE DRAGOONS',\n",
172+
" 'MIDNIGHT REVENGENCE',\n",
173+
" 'WILY WARRIORS']"
174+
]
175+
}
176+
],
177+
"prompt_number": 22
178+
}
179+
],
180+
"metadata": {}
181+
}
182+
]
183+
}

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ code_py
44
Some well commented code snippits in (mostly) Python 3
55

66
This collection of snippits is not comprehensive, and is in many places repetative.
7+
8+
You can view all the snippits in [iPython Notebook Viewer](http://nbviewer.ipython.org/github/chrisalbon/code_py/tree/master/).

0 commit comments

Comments
 (0)