def add_two(number):
return number + 2
for i in range(5):
print add_two(i)
2 3 4 5 6
def add_some_other_number(number, other_number=12):
return number + other_number
add_some_other_number(2, 6)
8
add_some_other_number(3, other_number=4)
7
add_some_other_number(5)
17
def factorial(n):
"""Compute factorial of n in the obious way."""
if n == 0:
return 1
else:
return factorial(n - 1) * n
help(factorial)
Help on function factorial in module __main__: factorial(n) Compute factorial of n in the obious way.
functions = [add_two, add_some_other_number]
for function in functions:
print function(7)
9 19
Simple anonymous functions can be created with lambda
.
functions.append(lambda x: x * 7)
for function in functions:
print function(4)
6 16 28
help(map)
Help on built-in function map in module __builtin__: map(...) map(function, sequence[, sequence, ...]) -> list Return a list of the results of applying the function to the items of the argument sequence(s). If more than one sequence is given, the function is called with an argument list consisting of the corresponding item of each sequence, substituting None for missing values when not all sequences have the same length. If the function is None, return a list of the items of the sequence (or a list of tuples if more than one sequence).
map(add_two, [1, 2, 3, 4])
[3, 4, 5, 6]
[(x, x * x) for x in range(10) if x % 2]
[(1, 1), (3, 9), (5, 25), (7, 49), (9, 81)]
We can do the same thing using map
and filter
, but list comprehensions are often more readable.
map(lambda x: (x, x * x), filter(lambda x: x %2, range(10)))
[(1, 1), (3, 9), (5, 25), (7, 49), (9, 81)]
{c for c in 'LUMC-standard' if 'a' <= c <= 'z'}
{'a', 'd', 'n', 'r', 's', 't'}
And dictionaries.
colors = ['red', 'white', 'blue', 'orange']
{c: len(c) for c in colors}
{'blue': 4, 'orange': 6, 'red': 3, 'white': 5}
dir(o)
, or by typing o.<tab>
in the IPython interpreter.dir('abc')[-5:]
['swapcase', 'title', 'translate', 'upper', 'zfill']
help('abc'.upper)
Help on built-in function upper: upper(...) S.upper() -> string Return a copy of the string S converted to uppercase.
'abc'.upper()
'ABC'
cat examples/fsquare.py
d = {} for i in range(10): d[i] = i ** 2 for i in d: print "{0} is the square of {1}.".format(d[i], i)
%%sh
python examples/fsquare.py
0 is the square of 0. 1 is the square of 1. 4 is the square of 2. 9 is the square of 3. 16 is the square of 4. 25 is the square of 5. 36 is the square of 6. 49 is the square of 7. 64 is the square of 8. 81 is the square of 9.
%run examples/fsquare.py
0 is the square of 0. 1 is the square of 1. 4 is the square of 2. 9 is the square of 3. 16 is the square of 4. 25 is the square of 5. 36 is the square of 6. 49 is the square of 7. 64 is the square of 8. 81 is the square of 9.
You can edit and run a file with %edit
.
%edit examples/fsquare.py
The Hitchhiker's Guide to Python
A Primer on Scientific Programming with Python
from IPython.display import HTML
def css_styling():
styles = open('styles/custom.css', 'r').read()
return HTML('<style>' + styles + '</style>')
css_styling()