8. Особенности языка
–
–
–
–
–
–
скриптовый интерпретируемый язык;
язык высокого уровня;
динамическая типизация;
минималистичный синтаксис;
богатая стандартная библиотека;
автоматическое управление памятью.
a, b = 5, 7
while b != 0:
a, b = b, a % b
print a
9. Фрагменты программ из ЕГЭ (B14)
var a,b,t,M,R: integer;
Function F(x: integer): integer;
begin
F := 9*(x+19)*(x-19) + 1;
end;
BEGIN
a := -20; b := 20;
M := a; R := F(a);
for t := a to b do begin
if (F(t)<R) then begin
M:=t; R:=F(t);
end;
end;
write(M);
END.
10. А теперь на Python’е
def F(x):
return 9*(x+19)*(x-19) + 1
a, b = -20, 20
M = a; R = F(a)
for t in xrange(a, b + 1):
if F(t) < R:
M = t
R = F(t)
print M
11. import sys
words_dict = dict()
for line in sys.stdin:
words = line.strip().split()
for word in words:
if word not in words_dict:
words_dict[word] = 1
else:
words_dict[word] += 1
for word, freq in words_dict.items():
print word, freq
12. Преимущества Python’а для обучения
–
–
–
–
–
компактный код;
меньше шансов для синтаксических ошибок;
отступы — неотъемлемая часть языкa;
синтаксис напоминает псевдокод;
высокая скорость разработки.
13. The Zen of Python
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren’t special enough to break the
rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
14. The Zen of Python, II
In the face of ambiguity, refuse the temptation
to guess.
There should be one– and preferably only one
–obvious way to do it.
Although that way may not be obvious at first
unless you’re Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it’s a
bad idea.
If the implementation is easy to explain, it may
be a good idea.
Namespaces are one honking great idea – let’s do
more of those!
20. Модуль matplotlib
import math, pylab
from matplotlib import mlab
def f(x):
if x == 0:
return 1.0
return math.sin(x) / x
xmin = -20.0
xmax = 20.0
dx = 0.01
xlist = mlab.frange(xmin, xmax, dx)
ylist = [f(x) for x in xlist]
pylab.plot (xlist, ylist)
pylab.show()
21.
22. Модуль sympy
from sympy import *
n, x, y = symbols("n x y")
expr = Limit((1 + 1/n)**n, n, oo)
print expr
Limit((1 + 1/n)**n, n, oo)
print expr.doit()
E
trigsimp(sin(x)*cos(y) + sin(y)*cos(x))
sin(x+y)
solve(x**3 - 6*x**2 + 9*x, x)
[0,3]
http://sympy.org/
http://live.sympy.org/
23. Система компьютерной алгебры Sage
– свободная и кроссплатформенная;
– в качестве языка программирования выбран Python;
– объединяет множество профессиональных
математических пакетов;
– можно работать через браузер.
http://sagemath.org/