If lever of programming beginner, you will find useful to also look at these concepts, they ll be handy in your programmer future:
map
, lambda
and generators
.
- You could define simple functions using lambda functions.
As:
in_circle = lambda (x, y): True if Math.sqrt( Math.pow(x,2) + Math.pow(y,2) ) < 1 else False
# assuming circle has center 0, 0 and radius 1
2.
and then map the function to a list of points:
map( in_circle, your_list)
Note that in lambda the syntax (x, y) is because you are passing a tuple as one argument, and your list is formed like:
your_list = [(0,1),(0, 0.5), (0.3, 0.4) ...]
3.
Instead of list, you can also use a generator, a structure very handy in iterations if you don't need to use again your list.
syntax is similar (note the brakets ! ( VS [ )
your_point = [ (random.random(), random.random()) for i in range(n) ]
# list comprehension
your_point = ( (random.random(), random.random()) for i in range(n) )
# generator
4.
So you could generate a list of N booleans like:
n = 10000
your_point = ( (random.random(), random.random()) for i in range(n) )
bool_list = map( in_circle, your_list)
For your curiosity in difference between lamdba and regular functions, see also:
what is the difference for python between lambda and regular function?
For your interest in generators VS lists comprehension:
Generator Expressions vs. List Comprehension