3. What is python?
âąâŻ Python is a Interpreted, Interactive, Portable,
Functional / Object-Oriented programing
language.
âąâŻ Invented by âGuido Van Rossumâ in 1991.
âąâŻ free and open source develop by âpython
software foundationâ, available at
www.python.org
âąâŻ "nal release: Python 2.7.2 , python 3.2.2
4. Why python?
âąâŻ
âąâŻ
âąâŻ
âąâŻ
âąâŻ
âąâŻ
Multi purpose design (web, os. application ,scienti"c, âŠ)
Cross platform (wide portability)
Both Functional & O.O programming
Rapid in Prototyping, strong in enterprise system
Integrate with other languages (c/c++, java, fortran, âŠ)
Dynamic, Extensible, readable.
5. Why python?
âąâŻ
âąâŻ
âąâŻ
âąâŻ
âąâŻ
âąâŻ
Many library and module
Easy to use (simple & familiar syntax ,few restrictions & rules)
Automatic memory management (Garbage Collector)
Good element (Dictionary, List, ⊠)
Good built-in algorithms (hashing , sorting , âŠ)
Many companies & institutions around the world
use python (Google, Yahoo, NASA, YouTube, FriendFeed , âŠ)
6. Introduction to python
âąâŻ Start with python
â⯠Set up
âąâŻ Linux , mac os X. and most operating systems have Python
âąâŻ For windows, download python installer and run it.
â⯠Run
âąâŻ Use interactive mode by type âpythonâ in Terminal
âąâŻ âpython ["lename].pyâ run python code "les
â⯠lDE
âąâŻ Every text editor with a little understanding of code :
notepad++,Gedit, jedit, âŠ
âąâŻ IDLE python editor releasing by âpython.orgâ
âąâŻ KOMODO is a good one!
8. Introduction to python
âąâŻ Lists
â⯠e.g. aList = [631, âpythonâ, [331, âtbâ]]
â⯠Like indexable arrays, not like linked list
â⯠Same operators as for strings
â⯠More operations append(), insert(), pop(), reverse() and
sort()
âąâŻ Sets
â⯠e.g. aSet=set([âtabrizuâ,âploytechnicâ,âtehranâ])
â⯠add(x), remove(x), discard(x), pop(), clear(), issubset(),
issuperset(), âŠ
â⯠Union â|â, intersection â&â, di#erence â#â
9. Introduction to python
âąâŻ Tuples
Â
â⯠e.g. aTuple = (631, âpythonâ, (611, âSAâ))
â⯠Unlike
 lists
 and
 like
 strings
 &
 set
 tuples
 are
 immutable
Â
âąâŻ Dic6onaries
Â
Â
â⯠e.g. adict= {âtabrizâ:âpythonâ,âtehranâ:âJavaâ}
â⯠Lookup
Â
 adict[âtabrizâ] -> âpythonâ
Â
â⯠Insert
Â
 adict[âvalueâ] = 100
Â
â⯠Delete
Â
 del adict[âvalueâ]
â⯠Presencie
Â
 adict.has_key(âtehranâ) -> True
â⯠Itera6ons
Â
 keys(), values(), items()
10. Introduction to python
âąâŻ Variables
â⯠No need to declare, Not typed but need to initialize
â⯠Almost everything can be assigned to a variable (functions,
modules, classes)
â⯠All variable are reference (a=b means a & b refer to same object)
âąâŻ Flow of Control
â⯠if condition : statements (elif condition : statements)
[else : statements]
â⯠while condition : statements [else : statements]
â⯠for var in sequence : statements [else : statements]
â⯠Break & Continue
11. Introduction to python
âąâŻ Functions
â⯠def FunctionName(arg1, arg2, ...):
Statements
return (expression)
âąâŻ Classes
â⯠class ClassName(BaseClass1, BaseClass2...) :
Statements
â⯠x = ClassName() creates a new instance
21. Introduction to python
âąâŻ Modules
â⯠Usage:
 e.g.
Â
 import
 datetime
â⯠Partial usage: e.g. from datetime import time
â⯠bult-in , installed and beside of code .py "les modules
âąâŻ example
> Python test.py jim!
> Hello jim!
22. Tips & Tricks
None and empty cheaking
Â
my_object = 'Test' # True example!
# my_object = '' or my_object = None # False example!
if len(my_object) > 0:!
!print 'my_object is not empty'!
!
if len(my_object): # 0 will evaluate to False!
print 'my_object is not emptyâ!
if my_object != '':!
print 'my_object is not emptyâ!
!
if my_object: # an empty string will evaluate to False!
print 'my_object is not empty'
24. Tips & Tricks
In one line!
Â
list= [âtabriz', âtehran', âshiraz']!
print 'The three are: %s.' % ', '.join(list)!
# print the tree are tabriz, tehran, shiraz!
!
validation= True if list else 'Test is False'!
# validation is True!
!
!
!
!
!
25. Tips & Tricks
lambda
Â
def add(a,b): return a+b!
add2 = lambda a,b: a+b
squares = map(lambda a: a*a, [1,2,3,4])!
Squares = a*a for a in [1,2,3,4]!
squares is now [1,4,9,16]
26. Tips & Tricks
Lambda & one line for
Â
numbers = [1,2,3,4,5]!
numbers_under_4 = filter(lambda x: x < 4, numbers)!
numbers_under_4 = [number for number in numbers if number < 4]!
# numbers_under_4 = [1,2,3]!
!
squares = map(lambda x: x*x, filter(lambda x: x < 4, numbers))!
squares = [number*number for number in numbers if number < 4]!
# square is now [1,4,9]
27. Tips & Tricks
one line for Vs nested for
Â
print [(x, y, x * y) for x in (0,1,2,3) for y in (0,1,2,3) if x < y]!
# prints [(0, 1, 0), (0, 2, 0), (0, 3, 0), (1, 2, 2), (1, 3, 3), (2, 3, 6)]!
!
for x in (0,1,2,3):!
for y in (0,1,2,3):!
if x < y:!
print (x, y, x*y),!
# prints (0, 1, 0) (0, 2, 0) (0, 3, 0) (1, 2, 2) (1, 3, 3) (2, 3, 6)!
!
28. Tips & Tricks
Lambda âŠ
Â
numbers = [1,2,3,4,5]!
!
result = reduce(lambda a,b: a*b, numbers)!
!
result = 1!
for number in numbers:!
!result *= number!
!
# result is now 120
29. Tips & Tricks
âŠ.
Â
!
!
strings = ['a', 'b', 'c', 'd', 'eâ]!
for index, string in enumerate(strings):!
print index, string,!
# prints '0 a 1 b 2 c 3 d 4 eâ!
!
numbers = [1,10,100,1000,10000]!
if any(number < 10 for number in numbers):!
print 'Successâ!
# Output: 'Success!'
30. Tips & Tricks
âŠ.
Â
!
!
if all(number < 10 for number in numbers):!
print 'Success!â!
# Output: (nothing)!
!
!
test = True!
result = ['Test is False','Test is True'][test]!
# result is now 'Test is Trueâ!
31. Tips & Tricks
Default value
Â
!
def function(item, stuff = []):!
stuff.append(item)!
print stuff!
!
function(1)!
# prints '[1]â!
function(2)!
# prints '[1,2]' !!!!
!
!
!
32. Tips & Tricks
Arbitrary
 Numbers
 of
 Arguments
Â
!
!
!
def do_something_else(a, b, c, *args, **kwargs):!
print a, b, c, args, kwargs!
!
!
do_something_else(1,2,3,4,5,6,7,8,9, timeout=1.5)!
# prints '1, 2, 3, (4, 5, 6, 7, 8, 9), {"timeout":
1.5}'!
34. Tips & Tricks
!
Dict
 /
 Object
Â
!
class mydict(dict):!
def __getattr__(self, attr):!
if super(mydict,self).has_key(attr):!
return super(mydict,self).__getitem__(attr)!
!
def __setattr__(self,attr,value):!
super(mydict,self).__setattr__(attr,value)!
!
adict= {âappleâ:1 ,âbananaâ:2 , âpeachâ:3}!
mdict=mydict(adict)!
mdict.orange=10!
print mdict.apple, mdict.orange , mdict[âbananaâ] , mdict!
# prints '1
!
10
2 {âappleâ:1 ,âbananaâ:2 , âpeachâ:3 , âorangeâ:10}â!
35. More on python
âąâŻ Commercial Usage
ââŻ
ââŻ
ââŻ
ââŻ
MVC Web programming via web server modules: Tornado, cherrypy ,âŠ
Mobile programming :PyObjC, ASE , âŠ
Easily connect to famous DB: MSSQL, Mysql , Oracle,âŠ
designed for non-relational DB linke Cassandra, MongoDB, CouchDB.
What is Non-relational DB!?
âąâŻ No relation between data, Provide list, vector, nested data "elds
âąâŻ No force schema & type
âąâŻ Java script base syntax instead of SQL
âąâŻ Document-oriented , Key-Value and Object-oriented database
36. More on python
âąâŻ Integrating Python With Other Languages
â⯠SWIG - generate extension module from your .h "les
â⯠F2PY - Fortran to Python Interface Generator
â⯠Lython (archived page) - Lisp front-end for Python
â⯠JPype Java for CPython
âąâŻ Python interpreters
â⯠Cpython a bytecode interpreter (Orginal)
ââŻ
ââŻ
ââŻ
ââŻ
PyPy
 a
 JIT
 Compiler,
 more Speed
 &
 eïŹciency
Â
ShedSkin
 a
 Python
 to
 C++
 programming
 language
 compiler
Â
Jython a Java implemented of Python
Â
ironpython a .net implemented of python
37. Scienti"c module
âąâŻ Numeric
 Module
Â
â⯠NumPy
Â
Â
 Numerical
 Python
 adds
 a
 fast,
 compact,
 mul6dimensional
 array
Â
facility
 to
 Python
Â
â⯠SciPy
Â
 Includes
 modules
 for
 linear
 algebra,
 op6miza6on,
 integra6on,
Â
special
 func6ons,
 sta6s6cs,
 and
 others.
Â
Â
â⯠OpenOpt
 a
 framework
 for
 numerical
 op6miza6on
 and
 systems
 of
 linear/
non-Ââlinear
 equa6ons.
Â
Â
â⯠ALGLIB
Â
 numerical
 analysis
 library
 in
 C++
 and
 C#,
 with
 Python
 interfaces.
Â
Â
â⯠SpaceFuncs
 -Ââ
 a
 tool
 for
 2D,
 3D,
 N-Ââdimensional
 geometric
 modeling
 with
Â
possibili6es
 of
 parametrized
 calcula6ons,
 numerical
 op6miza6on
 and
Â
solving
 systems
 of
 geometrical
 equa6ons
 with
 automa6c
 diïŹeren6a6on.
Â
Â
38. Scienti"c module
âąâŻ Algorithm
 Module
Â
Â
â⯠Mlpy
Â
 Machine
 Learning
 Python
 and
 high-Ââperformance
 Python
Â
module
 for
 Predic6ve
 Modeling
Â
â⯠SciPy
 for
 signal
 and
 image
 processing,
 gene6c
 algorithms
Â
â⯠graph-Ââtool
Â
 A
 python
 module
 for
 eïŹcient
 analysis
 of
 graphs
 (aka.
Â
Networks)
Â
â⯠Pyevolve is evolutionary algoritm. Machin learning.
39. Scienti"c module
âąâŻ Grid
 CompuAng
 Module
Â
â⯠PyGlobus
 Globus
 toolkit
 bindings
 for
 python
Â
Â
â⯠PEG
Â
 Python
 Extensions
 for
 the
 Grid
Â
Â
â⯠Ganga
Â
 Grid
 job
 management
 interface.
Â
Â
â⯠DIANE
 Python
 user-Ââlevel
 middleware
 layer
 for
 Grids.
Â
Â
Â
Â
40. Scienti"c module
âąâŻ Other
 ScienAïŹc
 Module
Â
Â
â⯠ScienAïŹcPython
 is
 a
 collec6on
 of
 Python
 scien6ïŹc
 modules
Â
â⯠Thuban
 is
 a
 Python
 Interac6ve
 Geographic
 Data
 Viewer
Â
Â
â⯠Matplotlib
Â
 hp://matplotlib.sourceforge.net/
 -Ââ
 matplotlib
 is
 a
Â
python
 2D
 plo]ng
 library
Â
Â
â⯠Biopython
 -Ââ
 a
 set
 of
 freely
 available
 tools
 for
 biological
Â
computa6on
 and
 bioinforma6cs.
Â
â⯠PyMol
Â
 3D
 molecular
 viewer
Â
41. Scienti"c module
âąâŻ Exmaple
 Usige
 Pyevolve
â⯠Installing pakages
> apt-Ââget
 install
 python-Ââsetuptools!
> easy_install
 pyevolve!
Or instal .egg pakage
> easy_install
 /downloads/downloaded_package.egg
 !
43. Refrences
âąâŻ Python
 Homepage
Â
Â
 hJp://www.python.org/
Â
âąâŻ Python
 wiki
Â
Â
 hJp://wiki.python.org/
Â
âąâŻ Google
 Code
 University
Â
Â
 hJp://code.google.com/edu/
Â
âąâŻ Python
Â
 documentaAon
Â
Â
hJp://docs.python.org/