function1() ### # Python поставляется с библиотекой стандартных модулей # полный список http://docs.python.org/3/library/index.html import math print(math.sin(90)) ### # Модули для импорта ищутся в текущем каталоге а затем в каталогах, # указанных в переменной окружения PYTHONPATH, потом - в директории инсталляции пайтона import my_math print(my_math.cos(90)) # В действительности, поиск модулей производится в списке каталогов, # передающемся в списке sys.path из модуля sys import sys for path in sys.path: print(path) ### # Обычно все импорты указываются в начале модуля, но можно импортировать и в коде функций, # тогда имена из модуля попадают в локальное пространство имен, в глобальном не видны. # то есть import - вычислимый оператор def some_func():
def test_cos(self): self.assertAlmostEqual(m.cos(123) ,-0.8879689) self.assertAlmostEqual(m.cos(-23) ,-0.53283302) self.assertAlmostEqual(m.cos(0) ,1) self.assertAlmostEqual(m.cos(m.pi) ,-1) self.assertAlmostEqual(m.cos(m.pi/2),0)
# 点を打つ def plot(x, y): print('<circle cx="{}" cy="{}" r="1" fill="lightsteelblue"/>'.format( convert_x(x), convert_y(y))) # 目盛り for y in range(-1, 2): print( '<rect x="0" y="{}" width="{}" height="0.0001" stroke="lavender" stroke-width="0.5"/>' .format(convert_y(y), width)) for x in range(-7, 8): print( '<rect x="{}" y="0" width="0.0001" height="{}" stroke="lavender" stroke-width="0.5"/>' .format(convert_x(x), height)) # 縦軸/横軸 print('<rect x="0" y="{}" width="{}" height="0.0001" stroke="lightgray"/>'. format(height / 2, width)) print('<rect x="{}" y="0" width="0.0001" height="{}" stroke="lightgray"/>'. format(width / 2, height)) # cos をプロット resolution = 40 for i in range(-6 * resolution, 6 * resolution): t = i / resolution plot(t, my_math.cos(math.pi * t)) print("</svg>")
import my_math print dir(my_math) # ['__doc__', '__file__', '__name__', '__package__', 'arithmetic_add', 'arithmetic_divide', 'arithmetic_multiply', 'arithmetic_subtract', 'cos', 'fibo_n', 'sort_list'] # these are the possible methods # # reimport it as a shorted name import my_math as mm a1 = mm.arithmetic_add(1, 2) a2 = mm.arithmetic_subtract(10, 2) a3 = mm.arithmetic_multiply(100, 3) a4 = mm.arithmetic_divide(400, 4.23) a5 = mm.arithmetic_divide(1000, 0) a6 = mm.cos(0, 0) start = time.time() a7 = mm.fibo_n(1000) end = time.time() for i, each in enumerate([ a1, a2, a3, a4, a5, a6, a7, ]): print "a%d : %s" % (i, each)
import sys import math import os sys.path.append(os.path.dirname(__file__) + '/../../') import my_math print("ver:1.0") # このテストのバージョン print("my_math.cos()の戻り値に対するテスト") # このテストの説明文 resolution = 8 for i in range(-3 * resolution, 3 * resolution): t = i / resolution print("cos({:<6} * PI): {}".format(t, my_math.cos(math.pi * t)))