Skip to content

Commit 7bfbb6b

Browse files
committed
some more snippets to post on the discussion forum
1 parent ead5460 commit 7bfbb6b

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Python functions are objects too. Here's an example of passing a
2+
function to a function. There's a other things to note here:
3+
4+
1. we use sys.exit() to abort if no function is supplied to execute()
5+
6+
2. we can use the "{}" syntax we saw with print in normal strings too.
7+
8+
9+
import sys
10+
11+
def execute(x, function=None):
12+
if function == None:
13+
sys.exit("ERROR: no function supplied")
14+
15+
return function(x)
16+
17+
18+
def test_function(x):
19+
a = "you passed in {}".format(x)
20+
return a
21+
22+
23+
print execute("test", function=test_function)
24+
25+

examples/python-snippets/in.post

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
We say the "in" operator in class to loop over members of a list, e.g.:
2+
3+
for a in [0, 1, 2, 3]:
4+
...
5+
6+
but in can also be used to test if a string contains a substring:
7+
8+
9+
>>> mystring = "this is my string"
10+
>>> "this" in mystring
11+
True
12+
>>> "that" in mystring
13+
False

examples/python-snippets/zip.post

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Here's another simple example that shows how to use zip() to merge two
2+
lists, element by element into a list of tuples, and how to use the
3+
".split()" method on a string to split it into its component words:
4+
5+
>>> a = range(10)
6+
>>> print a
7+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
8+
>>> b = "this is my nice long string".split()
9+
>>> print b
10+
['this', 'is', 'my', 'nice', 'long', 'string']
11+
>>> for n, w in zip(a, b):
12+
....: print n, w
13+
....:
14+
0 this
15+
1 is
16+
2 my
17+
3 nice
18+
4 long
19+
5 string
20+

0 commit comments

Comments
 (0)