python a practical perspective
DESCRIPTION
Python presentation with examples available on github. Main concepts can be found by exploring the small and concise code samples available. It presents some of Python's capabilities and characteristics. The code samples (end of June 2014) are tested with latest Python 2.7 but should be easily convertable to Python 3.TRANSCRIPT
PythonA practical perspective
Purpose
Show some Python characteristics
Identify scenarios where it can be used
Learn by examples
Foreword
Using a programming language doesn’t mean knowing everything about it
Experiment but focus on the purpose, not the language!
The Zen of Python (PEP* 20)
Beautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.…* Python Enhancement Proposalhttp://legacy.python.org/dev/peps/pep-0020/
Multi paradigm language
Imperative
Object oriented
Functional
Indentation delimits blocks
Python C/C++/Java
if IsOk(): DoThatOne() DoAnotherOne()DoFinal()
if ( IsOk() ){ DoThatOne(); DoAnotherOne();}DoFinal();
Modules
import module_name…
module_name.module_function(...)module_name.variableDetails: https://docs.python.org/2/tutorial/modules.html
Functions
def MySum(param1, param2): result = param1 + param2 return result
Classes
class Derived(Base): def Method(self,...): …
Details: https://docs.python.org/2/tutorial/classes.html
Some built-in data types
int, float, long, complex, string, file
Lists: [ 1, “string”, 2.3 ]Dictionaries: { “Kate”:33 , “John”:25, “Deanna”:42 }Sets: set([1,2,1,6,7]) → [1,2,6,7]Tuple: (1, “Nigel”, 23)
Details: https://docs.python.org/2/library/stdtypes.html
Immutable objects
Cannot be changed after creation.Examples: numbers, strings, tuples (the minority)x = some immutable objecty = xy = … # change value of yx has the same valueDetails: https://docs.python.org/2/reference/datamodel.html and Please read [2]
Immutable objects (2)>>> x=39>>> y=x>>> y=41>>> print(x)39
>>> s1='Rob the bank!'>>> s2=s1>>> s2='Then take me home'>>> print(s1)Rob the bank!
>>> s="It's Python, ... yo!">>> s[2]='a'Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: 'str' object does not support item assignment
Mutable objects
Can be changed after creationExamples: lists, dictionaries, classes (the majority)x = some mutable objecty = xy = … # change value of yx has changed its value
Mutable objects (2)>>> d1={"Deanna":"Nick Cave", "Jackie":"Placebo"}>>> d2=d1>>> d2["Jackie"]="Sinead O'Connor">>> print(d1){'Deanna': 'Nick Cave', 'Jackie': "Sinead O'Connor"}
>>> a1=['The Mono Jacks', 'Grimus', 'Urma']>>> a2=a1>>> a2.append('Byron')>>> print(a1)['The Mono Jacks', 'Grimus', 'Urma', 'Byron']
Some popular internal modules
Full list here https://docs.python.org/2.7/py-modindex.html
Coverage:● process/thread management - threading, subprocess● serialization/persistence - pickle, sqlite3, struct, bsddb, minidom● network communication - smtplib, poplib, ftplib, sockets, httplib,
urllib2, asyncore● encoding/compression - bz2, zlib, tarfile, zipfile, base64● file/directory handling - os, tempfile, os.path, stat● security - ssl, md5● string matching/parsing - re, fnmatch, ast
Some listed here: https://wiki.python.org/moin/UsefulModules
Coverage:● database handling - sqlalchemy, mysql-python● GUI - wxpython, pyqt● Scientific/Math functionality - NumPy, SciPy● Web development - django, web2py● Image processing - pil, pyqtgraph● Networking - tornado, twisted, Gevent● Process listing and manipulation - psutil
Some popular external modules
Enough, let’s walk the talk!
Code for this presentation
https://github.com/undergraver/PythonPresentation/
01_intro - some of Python’s characteristics02_typical - common Python usages03_demo - few polished examples04_regex - regular expressions in Python
Notes for examples
Please read the documentation for each python module used. Start from [3]
wxpythonUI uses wxpython [5] python library + wxformbuilder [7] for ui design
process list/kill uses psutil [6] python library
For regex is is recommended to read the documentation ( [4] )
Notable applications using Python
References[1] https://developers.google.com/edu/python/[2] http://www.slideshare.net/amiable_indian/introduction-to-python[3] https://docs.python.org/ [4] https://docs.python.org/2/library/re.html[5] http://www.wxpython.org/ [6] https://code.google.com/p/psutil/ [7] http://sourceforge.net/projects/wxformbuilder/