# 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.1244018361.txt.gz · 最終更新: 2009/06/03 17:39 by kimi