(Trop) Brève introduction à Python S. Vialette IGM-LabInfo, Université Paris-Est 21 janvier 2008 S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 1 / 17 Qu’est-ce que c’est ? http ://fr.wikibooks.org/wiki/Programmation Python/Introduction Python est un langage de script de haut niveau, structuré et open source. Il est multi-paradigme et multi-usage. Développé à l’origine par Guido Van Rossum en 1993, il est, comme la plupart des applications et outils open source, maintenu par une équipe de développeurs un peu partout dans le monde. Conçu pour être orienté objet, il n’en dispose pas moins d’outils permettant de se livrer à la programmation fonctionnelle ou impérative ; c’est d’ailleurs une des raisons qui lui vaut son appellation de ”langage agile”. Parmi les autres raisons, citons la rapidité de développement (qualité propre aux langages interprétés), la grande quantité de modules fournis dans la distribution de base ainsi que le nombre d’interfaces disponibles avec des bibliothèques écrites en C, C++ ou Fortran. S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 2 / 17 Utilisation Python se prête à un grand nombre de tâches : développement réseau twisted Framework réseau PYRO Python Remote Objects PyLinda Distributed Computing Made Easy ... Framework web Django The Web framework for perfectionists with deadlines Plone CMS Open Source Content Management Zope Open source application server Turbogears Web development ... S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 3 / 17 Utilisation GUI Tk wxPython PyQt PyGTK ... A thin object-oriented layer on top of Tcl/Tk Cross-platform Python GUI toolkit Python bindings for Trolltech’s Qt application framework Python bindings for GTK+ application framework XML PyXML XML package for Python 4suite Open-source platform for XML and RDF processing pyRXP The fastest XML parser ... S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 4 / 17 Utilisation Mutimédia PyGame pyMedia Shtoom PiTiVi ... Python modules designed for writing games (SDL) Python module for wav, mp3, ogg, avi, divx, dvd, . . . VoIP softphone Open source video editor PDF ReportLab Toolkit Industry-strength PDF generating solution pyPdf Pure-Python library built as a PDF toolkit ... S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 5 / 17 Utilisation et bien d’autres BioPython Tools for computational molecular biology. networkx Creation, manipulation, and study of the structure, dynamics, and functions of complex networks NLTK The Natural Language Toolkit PLY Implementation of lex and yacc parsing SciPy Open-source software for mathematics, science, and engineering Simpy Simulation in Python jython Implementation of Python written in 100% pure Java unittest Unit testing framework ... S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 6 / 17 Environnements de Développement emacs avec python-mode bien sûr SPE Stani’s Python Editor PyDev Python development enviroment for Eclipse geany A fast and lightweight IDE Boa Constructor Cross platform Python IDE PyPE Editor written in Python with the wxPython GUI toolkit ... S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 7 / 17 Interpréteur $ python Python 2 . 5 . 1 ( r 2 5 1 : 5 4 8 6 3 , Oct 5 2 0 0 7 , 1 3 : 3 6 : 3 2 ) [ GCC 4 . 1 . 3 20070929 ( p r e r e l e a s e ) ( Ubuntu 4.1.2 −16 ub un t u2 ) ] on l i n u x 2 Type " help " , " copyright " , " credits " o r " license " f o r more i n f o r m a t i o n . >>> p r i n t ’ hello world ! ’ h e l l o world ! >>> i m p o r t s y s >>> s y s . s t d o u t . w r i t e ( ’ hello world !\ n ’ ) h e l l o world ! >>> d i r ( s y s ) [ ’ __dis pl a yh ook__ ’ , ’ __doc__ ’ , ’ __excepthook__ ’ , ’ __name__ ’ , ’ __stderr__ ’ , ’ __stdin__ ’ , ’ __stdout__ ’ , ’ _current_frames ’ , ’ _getframe ’ , ’ api_version ’ , ’ argv ’ , ’ b u i l t i n _ m o d u l e _ n a m e s ’ , ’ byteorder ’ , ’ call_tracing ’ , ’ callstats ’ , ’ copyright ’ , ’ displayhook ’ , ’ exc_clear ’ , ’ exc_info ’ , ’ exc_type ’ , ’ excepthook ’ , ’ exec_prefix ’ , ’ executable ’ , ’ exit ’ , ’ getchec kinterval ’ , ’ g et d ef au l te nc o di n g ’ , ’ getdlopenflags ’ , ’ g e t f i l e s y s t e m e n c o d i n g ’ , ’ get r ec urs ion limi t ’ , ’ getrefcount ’ , ’ hexversion ’ , ’ maxint ’ , ’ maxunicode ’ , ’ meta_path ’ , ’ modules ’ , ’ path ’ , ’ path_hooks ’ , ’ p a t h _ i m p or t e r _ c a c h e ’ , ’ platform ’ , ’ prefix ’ , ’ ps1 ’ , ’ ps2 ’ , ’ pydebug ’ , ’ setcheckinterval ’ , ’ setdlopenflags ’ , ’ setprofile ’ , ’ set rec ursi onl imi t ’ , ’ settrace ’ , ’ stderr ’ , ’ stdin ’ , ’ stdout ’ , ’ subversion ’ , ’ version ’ , ’ version_info ’ , ’ warnoptions ’ ] >>> # <Crt−D> p o u r q u i t t e r . . . ... $ S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 8 / 17 Interpréteur $ p y t h o n −−h e l p u s a g e : p y t h o n [ o p t i o n ] . . . [−c cmd | −m mod | f i l e | −] [ a r g ] . . . O p t i o n s and a r g u m e n t s ( and c o r r e s p o n d i n g e n v i r o n m e n t v a r i a b l e s ) : −c cmd : program p a s s e d i n a s s t r i n g ( t e r m i n a t e s o p t i o n l i s t ) −d : debug o u t p u t from p a r s e r ( a l s o PYTHONDEBUG=x ) −E : i g n o r e e n v i r o n m e n t v a r i a b l e s ( s u c h a s PYTHONPATH) −h : p r i n t t h i s h e l p m e s s a g e and e x i t ( a l s o −−h e l p ) −i : i n s p e c t i n t e r a c t i v e l y a f t e r r u n n i n g s c r i p t , ( a l s o PYTHONINSPECT=x ) and f o r c e prompts , e v e n i f s t d i n d o e s n o t a p p e a r t o be a t e r m i n a l −m mod : r u n l i b r a r y module a s a s c r i p t ( t e r m i n a t e s o p t i o n l i s t ) −O : o p t i m i z e g e n e r a t e d b y t e c o d e ( a t a d ; a l s o PYTHONOPTIMIZE=x ) −OO : remove doc−s t r i n g s i n a d d i t i o n t o t h e −O o p t i m i z a t i o n s −Q a r g : d i v i s i o n o p t i o n s : −Qold ( d e f a u l t ) , −Qwarn , −Q w a r n a l l , −Qnew −S : don ’ t i m p l y ’ i m p o r t s i t e ’ on i n i t i a l i z a t i o n −t : i s s u e w a r n i n g s a b o u t i n c o n s i s t e n t t a b u s a g e (− t t : i s s u e e r r o r s ) −u : u n b u f f e r e d b i n a r y s t d o u t and s t d e r r ( a l s o PYTHONUNBUFFERED=x ) s e e man page f o r d e t a i l s on i n t e r n a l b u f f e r i n g r e l a t i n g t o ’−u ’ −v : v e r b o s e ( t r a c e i m p o r t s t a t e m e n t s ) ( a l s o PYTHONVERBOSE=x ) −V : p r i n t t h e Python v e r s i o n number and e x i t ( a l s o −−v e r s i o n ) −W a r g : w a r n i n g c o n t r o l ( a r g i s a c t i o n : m e s s a g e : c a t e g o r y : module : l i n e n o ) −x : s k i p f i r s t l i n e o f s o u r c e , a l l o w i n g u s e o f non−U n i x f o r m s o f #! cmd file : program r e a d from s c r i p t f i l e − : program r e a d from s t d i n ( d e f a u l t ; i n t e r a c t i v e mode i f a t t y ) a r g . . . : a r g u m e n t s p a s s e d t o program i n s y s . a r g v [ 1 : ] $ S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 9 / 17 Exécuter un programme Python Tout cours d’introduction à un langage de programmation ne saurait commencer sans Hello words !. #! / u s r / b i n / e n v p y t h o n h e l l o W o r d S t r i n g = " Hello word ! " print helloWordString Il existe plusieurs façons d’exécuter un programme Python. S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 10 / 17 Depuis l’interpréteur Python $ python Python 2 . 5 . 1 ( r 2 5 1 : 5 4 8 6 3 , Oct 5 2 0 0 7 , 1 3 : 3 6 : 3 2 ) [ GCC 4 . 1 . 3 20070929 ( p r e r e l e a s e ) ( Ubuntu 4 . 1 . 2 − 1 6 u b u n t u 2 ) ] on l i n u x 2 Type " help " , " copyright " , " credits " o r " license " f o r more information . >>> h e l l o W o r d S t r i n g = " Hello word ! " >>> p r i n t h e l l o W o r d S t r i n g H e l l o word ! >>> >>> est l’invite de l’interpréteur Python. Ctrl-D pour quitter. S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 11 / 17 Depuis un fichier $ l s s a y H e l l o . py s a y H e l l o . py $ f i l e s a y H e l l o . py s a y H e l l o . py : A S C I I t e x t $ $ c a t s a y H e l l o . py h e l l o W o r d S t r i n g = " Hello word ! " print helloWordString $ $ p y t h o n s a y H e l l o . py H e l l o word ! $ S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 12 / 17 Depuis un fichier $ l s s a y H e l l o . py s a y H e l l o . py $ c a t s a y H e l l o . py #! / u s r / b i n / e n v p y t h o n h e l l o W o r d S t r i n g = " Hello word ! " print helloWordString $ $ l s − l s a y H e l l o . py −rw−r−−r−− 1 v i a l e t t e v i a l e t t e 78 2007−12−18 2 3 : 4 7 s a y H e l l o . py $ . / s a y H e l l o . py b a s h : . / s a y H e l l o . py : P e r m i s s i o n d e n i e d $ chmod +x s a y H e l l o . py $ l s − l s a y H e l l o . py −r w x r −x r −x 1 g a r u l f o g a r u l f o 78 2007−12−18 2 3 : 4 7 s a y H e l l o . py $ . / s a y H e l l o . py H e l l o word ! $ S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 13 / 17 Depuis l’interpréteur La fonction execfile(filename ) exécute le code Python se trouvant dans le fichier filename . $ python Python 2 . 5 . 1 ( r 2 5 1 : 5 4 8 6 3 , Oct 5 2 0 0 7 , 1 3 : 3 6 : 3 2 ) [ GCC 4 . 1 . 3 20070929 ( p r e r e l e a s e ) ( Ubuntu 4 . 1 . 2 − 1 6 u b u n t u 2 ) ] on l i n u x 2 Type " help " , " copyright " , " credits " o r " license " f o r more information . >>> e x e c f i l e ( " sayHello . py " ) H e l l o word ! >>> S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 14 / 17 Depuis l’interpréteur À noter qu’il existe également la fonction exec(str ) qui exécute le code Python se trouvant dans la chaı̂ne de caractères str . $ python Python 2 . 5 . 1 ( r 2 5 1 : 5 4 8 6 3 , Oct 5 2 0 0 7 , 1 3 : 3 6 : 3 2 ) [ GCC 4 . 1 . 3 20070929 ( p r e r e l e a s e ) ( Ubuntu 4 . 1 . 2 − 1 6 u b u n t u 2 ) ] on l i n u x 2 Type " help " , " copyright " , " credits " o r " license " f o r more information . >>> e x e c ( " he ll o Wo rd St r in g = ’ Hello word ! ’ " + \ . . . " \ n " + " print h el lo Wo r dS tr in g " ) H e l l o word ! >>> m y S t r i n g = " " " ... he l lo Wo rd S tr in g = " H e l l o word ! " ... print hel lo W or dS tr i ng ... " " " >>> e x e c ( m y S t r i n g ) H e l l o word ! S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 15 / 17 Depuis un autre programme #i n c l u d e <Python . h> i n t main ( i n t a r g c , c h a r ∗ a r g v [ ] ) { Py Initialize (); P y R u n S i m p l e S t r i n g ( " he ll o Wo rd St r in g = \" Hello word !\" " ) ; P y R u n S i m p l e S t r i n g ( " print h el lo Wo r dS tr in g " ) ; return (0); } $ l s / u s r / i n c l u d e / p y t h o n 2 . 5 / Python . h / u s r / i n c l u d e / p y t h o n 2 . 5 / Python . h $ l s / u s r / l i b / l i b p y t h o n 2 . 5 . so / u s r / l i b / l i b p y t h o n 2 . 5 . so $ g c c −o s a y H e l l o s a y H e l l o . c − I / u s r / i n c l u d e / p y t h o n 2 . 5 / −l p y t h o n 2 . 5 $ ./ sayHello H e l l o word ! $ S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 16 / 17 Typage langage à typage dynamique Un langage dans lequel les types sont découverts à l’exécution, l’inverse du typage statique. langage fortement typé Un langage dans lequel les types sont toujours appliqués (un entier ne peut par exemple pas être traité comme une chaı̂ne sans conversion explicite). Python est à la fois à typage dynamique (il n’utilise pas de déclaration de type explicite) et fortement typé (une fois qu’une variable a un type, cela a une importance). S. Vialette (IGM-LabInfo, Université Paris-Est) (Trop) Brève introduction à Python 21 janvier 2008 17 / 17