Lesson 3: 関数 (外部手続) とライブラリ

>>> def fact(n): 
... k = 1 
... for m in range(n): 
... k = k*(m + 1) 
... return k 
... 
>>> print fact(2), fact(3), fact(4), fact(5), fact(6) 
2 6 24 120 720 
>>> 
>>> def fact(n): 
... if (n == 0): 
... return 1 
... else: 
... return n*fact(n - 1) 
... 
>>> print fact(2), fact(3), fact(4), fact(5) 
2 6 24 120 
>>> 
>>> print sin(0.1) 
NameError: name 'sin' is not defined 
>>> from math import sin 
>>> print sin(0.1) 
0.0998334166468 
>>> 
>>> from math import * 
>>> dir() 
['__builtins__', '__doc__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh'] 
>>> 
>>> help(sin) 
Help on built-in function sin: 
 
sin(...) 
sin(x) 
 
Return the sine of x (measured in radians). 
 
>>> 
>>> a = [1, 2, 3]; dir(a) 
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__str__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort'] 
>>> help(a.reverse) 
Help on built-in function reverse: 
 
reverse(...) 
L.reverse() -- reverse *IN PLACE* 
 
>>> print a.reverse() 
None 
>>> print a 
[3, 2, 1] 
>>>
seminar/python_tutorial_lesson3.txt · 最終更新: 2009/06/03 20:17 by kimi
www.chimeric.de Creative Commons License Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0