Komunikacja Człowiek-Komputer Wprowadzenie do Python a Wojciech Jaśkowski Instytut Informatyki Politechnika Poznańska Wersja: 14 października 2013
Hello world print( Hello world )
Dlaczego Python? Prosty do nauki Zwięzła składnia Czytelny kod Przenośny: Windows, Linux/Unix, OSX, Java,.NET Otwarty, wolny, darmowy Bogata biblioteka standardowa, ogromna liczba bibliotek Popularny wśród naukowców Łatwo googlywalny Stosunkowo popularny: 8 na liście TIOBE Programming Community Index
TIOBE Programming Community Index http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html
Kto używa? Dropbox (Guido van Rossum) Mercurial Ubuntu Software Center Trac web-based bug/issue tracking database, wiki, and version control front-end Civilization IV, World of Tanks for most of its tasks Battlefield 2 for all its addons Django, an MVT (model, view, template) web framework Abaqus, Finite Element Software (scripting) Blender 3D, Unity 3D, Cinema 4D, Maya, Corel Paint Shop Pro, GIMP (scripting) Google (e.g. Youtube, Google App, Google Chrome, partially) Engine Quora, Instagram, Spotify, Reddit
Implementacje CPython (standard) Najnowsze stabilne wersje: 2.7 i 3.3 PyPy (w Pythonie, JIT) Jython (dla JVM) IronPython (dla.net)
Paradygmaty Proceduralny arr = sorted([3,2,-1]) first = arr[0] Obiektowy class Pair(list): def init (self, first, second): self.first = first self.second = second arr = Pair(3, 7) Elementy funkcyjne arr = sorted([3,2,-1], key=lambda x: abs(x))
Cechy Python a System typów: bez deklaracji x = 1 dynamicznie typowany x = 1 x = "12" silnie typowany x = "1" + 1 TypeError: Can t convert int object to str implicitly
Składnia Life s better without braces (Bruce Eckel) dwukropek, spacje (tabulatory), cudzysłowy if 1+1==2: print( Uff! ) else: print("ups. I should ve tried that") komentarze # This should not happen... normally """ An ugly two-line comment """
Tutorial
Uruchamianie i środowiska interaktywne $> python plik.py $> python $> ipython $> ipython plik.ipy $> ipython qtconsole $> ipython qtconsole --matplotlib $> ipython notebook $> ipython notebook --matplotlib=inline
Środowiska developerskie warte uwagi PyCharm (http://www.jetbrains.com/pycharm/) pyscripter (https://code.google.com/p/pyscripter/) spyder (https://code.google.com/p/spyderlib/)
Moduły Moduł to plik, np: fibonacci.py from future import print_function # Python < 3.0 MAXFIB=1000 def fib(n): a, b = 0, 1 while b < n: print(b,end= ) a, b = b, a+b >>> import fibonacci >>> fibonacci.fib(10) 1 1 2 3 5 8
Moduły Moduł to plik, np: fibonacci.py from future import print_function # Python < 3.0 MAXFIB=1000 def fib(n): a, b = 0, 1 while b < n: print(b,end= ) a, b = b, a+b >>> import fibonacci as fibo >>> fibo.fib(10) 1 1 2 3 5 8
Moduły Moduł to plik, np: fibonacci.py from future import print_function # Python < 3.0 MAXFIB=1000 def fib(n): a, b = 0, 1 while b < n: print(b,end= ) a, b = b, a+b >>> from fibonacci import fib >>> fib(10) 1 1 2 3 5 8
Moduły Moduł to plik, np: fibonacci.py from future import print_function # Python < 3.0 MAXFIB=1000 def fib(n): a, b = 0, 1 while b < n: print(b,end= ) a, b = b, a+b >>> from fibonacci import fib, MAXFIB >>> fib(10) 1 1 2 3 5 8 >>> MAXFIB 1000
Moduły Moduł to plik, np: fibonacci.py from future import print_function # Python < 3.0 MAXFIB=1000 def fib(n): a, b = 0, 1 while b < n: print(b,end= ) a, b = b, a+b >>> from fibonacci import * # Niepolecane >>> fib(10) 1 1 2 3 5 8 >>> MAXFIB 1000
Skrypty >>> import fibonacci as fib >>> fib. name fibonacci Uruchamianie skryptów: python fibonacci.py <argumenty> ustawia name na main
Skrypty fibonacci.py from future import print_function def fib(n): a, b = 0, 1 while b < n: print(b, end= ) a, b = b, a+b if name == main : import sys fib(int(sys.argv[1])) > python fibbonacci.py 20 1 1 2 3 5 8 13
Pakiety sound/ init.py formats/ init.py wavread.py wavwrite.py aiffread.py aiffwrite.py auread.py auwrite.py... effects/ init.py echo.py surround.py Pakiet Inicjalizacja pakietu Podpakiet formats Podpakiet efekty
Pakiety Moduły i pakiety muszą być w katalogu sys.path >>> import sys >>> sys.path [, /usr/lib/python2.7/dist-packages, /home/wojciech/.local/lib/python2.7/site-packages, (...)]
Pakiety instalacja Ubuntu: sudo apt-get install python-numpy Windows: http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy Wszędzie z repozytorium PyPI: > pip install numpy > easy_install numpy Lokalnie, w katalogu użytkownika: > pip install --user numpy > easy_install --user numpy
Pomoc >>> help(abs) Help on built-in function abs in module builtin : abs(...) abs(number) -> number Return the absolute value of the argument. >>> abs. doc abs(number) -> number\n\nreturn the absolute value of the argument. >>> dir(list) [ add,..., sort,...]
Python jest wolny. Ale czy zbyt wolny? Źródło: http://www.infoworld.com/d/application-development/van-rossum-python-not-too-slow-188715
Przyspieszanie PyPy, kompilator z JIT Rozszerzenia w C lub C++: http: //docs.python.org/release/2.5.2/ext/intro.html Cython, Python z typami z C
Cython def primes(int kmax): cdef int n, k, i cdef int p[1000] result = [] k = 0; n = 2 while k < kmax: i = 0 while i < k and n % p[i]!= 0: i += 1 if i == k: p[k] = n k += 1 result.append(n) n = n + 1 return result
Koniec Dziękuję za uwagę.