зобнин информатика в школе
TRANSCRIPT
Информатика в школе:язык Python и компьютерная алгебра
Алексей Зобнин
Яндекс,Механико-математический факультет МГУ
4 декабря 2013 г.
Язык программирования Python
– появился в 1991 году;– сейчас — один из самых популярных языков
программирования;
http://python.org/
Создатель языка Python
Гвидо ван Россум, голландский программист.
Первая программа на Python’е
print "Hello, world!"
Первая программа на Python’е
print "Hello, world!"
Версии языка
– 2.x– 3.x
Версии языка
– 2.x (2.7.6);– 3.x (3.4.0).
Особенности языка
– скриптовый интерпретируемый язык;– язык высокого уровня;– динамическая типизация;– минималистичный синтаксис;– богатая стандартная библиотека;– автоматическое управление памятью.
a, b = 5, 7while b != 0:
a, b = b, a % bprint a
Фрагменты программ из ЕГЭ (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 beginM:=t; R:=F(t);
end;end;write(M);
END.
А теперь на Python’е
def F(x):return 9*(x+19)*(x-19) + 1
a, b = -20, 20M = a; R = F(a)for t in xrange(a, b + 1):
if F(t) < R:M = tR = F(t)
print M
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
Преимущества Python’а для обучения
– компактный код;– меньше шансов для синтаксических ошибок;– отступы — неотъемлемая часть языкa;– синтаксис напоминает псевдокод;– высокая скорость разработки.
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 therules.Although practicality beats purity.Errors should never pass silently.Unless explicitly silenced.
The Zen of Python, II
In the face of ambiguity, refuse the temptationto guess.There should be one– and preferably only one–obvious way to do it.Although that way may not be obvious at firstunless 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 abad idea.If the implementation is easy to explain, it maybe a good idea.Namespaces are one honking great idea – let’s domore of those!
Формальные исполнители алгоритмов
– «черепашка», «чертежник» (язык Logo, 1967)– «робот» (язык Karel, 1981).
Модуль turtle
from turtle import *
def draw_circle(a, s):for j in xrange(a):
right(360.0/a)forward(s)
def draw(a, s):for i in xrange(a):
right(360.0/a)draw_circle(a, s)
hideturtle()bgcolor("black")pencolor("red")pensize(3)draw(36, 20)
Guido van Robot
Математические модули
– math– pylab– matplotlib– numpy
Модуль matplotlib
import math, pylabfrom matplotlib import mlab
def f(x):if x == 0:
return 1.0return math.sin(x) / x
xmin = -20.0xmax = 20.0dx = 0.01
xlist = mlab.frange(xmin, xmax, dx)ylist = [f(x) for x in xlist]
pylab.plot (xlist, ylist)pylab.show()
Модуль 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/
Система компьютерной алгебры Sage
– свободная и кроссплатформенная;– в качестве языка программирования выбран Python;– объединяет множество профессиональных
математических пакетов;– можно работать через браузер.
http://sagemath.org/
f(x) =
(x6 − 1
110
)×
×(x6 +
2
5x5 − 2
25x4 +
4
125x3 − 2
125x2 +
2
125x+
1
125
).