python a practical perspective

21
Python A practical perspective

Upload: iulian-nicu-serbanoiu

Post on 10-May-2015

288 views

Category:

Travel


2 download

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

Page 1: Python   a practical perspective

PythonA practical perspective

Page 2: Python   a practical perspective

Purpose

Show some Python characteristics

Identify scenarios where it can be used

Learn by examples

Page 3: Python   a practical perspective

Foreword

Using a programming language doesn’t mean knowing everything about it

Experiment but focus on the purpose, not the language!

Page 4: Python   a practical perspective

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/

Page 5: Python   a practical perspective

Multi paradigm language

Imperative

Object oriented

Functional

Page 6: Python   a practical perspective

Indentation delimits blocks

Python C/C++/Java

if IsOk(): DoThatOne() DoAnotherOne()DoFinal()

if ( IsOk() ){ DoThatOne(); DoAnotherOne();}DoFinal();

Page 7: Python   a practical perspective

Modules

import module_name…

module_name.module_function(...)module_name.variableDetails: https://docs.python.org/2/tutorial/modules.html

Page 8: Python   a practical perspective

Functions

def MySum(param1, param2): result = param1 + param2 return result

Page 9: Python   a practical perspective

Classes

class Derived(Base): def Method(self,...): …

Details: https://docs.python.org/2/tutorial/classes.html

Page 10: Python   a practical perspective

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

Page 11: Python   a practical perspective

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]

Page 12: Python   a practical perspective

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

Page 13: Python   a practical perspective

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

Page 14: Python   a practical perspective

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']

Page 15: Python   a practical perspective

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

Page 16: Python   a practical perspective

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

Page 17: Python   a practical perspective

Enough, let’s walk the talk!

Page 18: Python   a practical perspective

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

Page 19: Python   a practical perspective

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] )

Iulian-Nicu Serbanoiu
regex exercise that modifies: "Test_Run_Task1" in "Test_Task1_Results" (remove Run, add Results)
Page 20: Python   a practical perspective

Notable applications using Python

Page 21: Python   a practical perspective

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/