-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
627 additions
and
556 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
Control structures | ||
================== | ||
|
||
Chaining comparison operators | ||
----------------------------- | ||
|
||
>>> x = 5 | ||
>>> 1 < x < 10 | ||
True | ||
>>> 10 < x < 20 | ||
False | ||
>>> x < 10 < x*10 < 100 | ||
True | ||
>>> 1 <= x <= 9 | ||
True | ||
>>> 4 < x == 5 | ||
True | ||
|
||
|
||
The for...else syntax | ||
--------------------- | ||
|
||
>>> def find(seq, val): | ||
... for i in seq: | ||
... if i == val: | ||
... print "found" | ||
... break | ||
... else: | ||
... print "not found" | ||
>>> find([1, 2, 3], 2) | ||
found | ||
>>> find([1, 2, 3], 5) | ||
not found | ||
|
||
The "else" block will be normally executed at the end of the for loop, unless | ||
the break is called (see http://docs.python.org/ref/for.html). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
Functions | ||
========= | ||
|
||
Be careful with mutable default arguments | ||
----------------------------------------- | ||
|
||
>>> def foo(x=[]): | ||
... x.append(1) | ||
... print x | ||
... | ||
>>> foo() | ||
[1] | ||
>>> foo() | ||
[1, 1] | ||
>>> foo() | ||
[1, 1, 1] | ||
|
||
Instead, you should use a sentinel value denoting "not given" and replace with | ||
the mutable you'd like as default: | ||
|
||
>>> def foo(x=None): | ||
... if x is None: | ||
... x = [] | ||
... x.append(1) | ||
... print x | ||
>>> foo() | ||
[1] | ||
>>> foo() | ||
[1] | ||
|
||
|
||
Partial functions | ||
----------------- | ||
|
||
>>> from functools import partial | ||
>>> bound_func = partial(range, 0, 10) | ||
>>> bound_func() | ||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | ||
>>> bound_func(2) | ||
[0, 2, 4, 6, 8] | ||
|
||
|
||
Operators can be called as functions | ||
------------------------------------ | ||
|
||
>>> from operator import add | ||
>>> print reduce(add, [1, 2, 3, 4, 5, 6]) | ||
21 | ||
|
||
|
||
Manipulating Recursion Limit | ||
---------------------------- | ||
|
||
We can limit it to prevent a stack overflow caused by infinite recursion by | ||
getting or setting the maximum depth of recursion with `sys.getrecursionlimit()` | ||
& `sys.setrecursionlimit()`. | ||
|
||
|
||
"Unpacking" to function parameters | ||
---------------------------------- | ||
|
||
>>> def foo(a, b, c): | ||
... print a, b, c | ||
>>> bar = (1, 2, 3) | ||
>>> foo(*bar) | ||
1 2 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.