TM
TWINCLING
freedom of innovation
TWINCLING.org
Python Programming Language
11th February, 2006
Hyderabad, AP India
What is Python
TM
TWINCLING
freedom of innovation
● Objectoriented
● Dynamic
TWINCLING.org
● Easy to learn syntax
● Highlevel data types
● Scripting language
● Embeddable in C/C++
Guido Von Rossum
Python vs Java (or C++)
TM
TWINCLING
freedom of innovation
● No compilation
– Fast editbuilddebug cycle
● Dynamic Typing
TWINCLING.org
– No need to declare variables for use
● Easy Syntax
– No curly braces, no semicolons, no new ...
Python vs Java (or C++) ...
TM
TWINCLING
freedom of innovation
● Embeddable
– Scripting support for your applications
● Interactive
TWINCLING.org
– Create, view, change objects at runtime
● 50% less code
– Compact and natural syntax
● 300% more productive
– Closer to the way you think
Hello World
TM
TWINCLING
freedom of innovation
Java
public class HelloWorld
{
public static void main( String args[] )
{
TWINCLING.org
System.out.println( "Hello World" );
}
}
Python
print 'Servus Austria'
Java + Swing
TM
TWINCLING
freedom of innovation
Java public class HelloSwing extends JFrame
{
public HelloSwing( String title )
{
super( title );
TWINCLING.org
addWindowListener( new WindowAdapter() {
public void windowClosing( WindowEvent ev )
{
exit();
}
} );
JButton button = new JButton( "Servus Austria" );
button.setPreferredSize( new Dimension( 200, 50 ) );
button.addActionListener( new ActionListener() {
Java + Swing ...
TM
TWINCLING
freedom of innovation
Java public void actionPerformed( ActionEvent ev )
{
exit();
}
} );
TWINCLING.org
getContentPane().add( button );
}
public void exit(){
System.exit( 0 );
}
public static void main( String args[] ){
HelloSwing frame = new HelloSwing( "Python" );
frame.pack();
frame.show();
}
}
Python + XUL
TM
TWINCLING
freedom of innovation
Python import java.lang as lang
def exit( event ):
lang.System.exit(0)
TWINCLING.org
<window title="Python" onclosing="exit()">
<button label="Hello World"
style="width: 200; height: 50;"
onclick="exit()" />
</window>
Why Python ?
TM
TWINCLING
freedom of innovation
● One language can not do it all
– Single purpose scripting languages on the rise
● XHTML
TWINCLING.org
● XUL (menus, toolbars, forms, grids, trees)
● SVG (charts, maps, logos ...)
● CSS (visual styling)
● Xpath (xml tree node addressing)
● SQL (data queries)
● XSLT (templates)
● Python/Scripting
Why Python ? ...
TM
TWINCLING
freedom of innovation
● Objectoriented
– Ideal for scripting
– Ideal for creating gluing together components written in
TWINCLING.org
Java, C# or C++
– Easy reuse through classes, polymorphism, operator
overloading, multiple inheritance
● Portable
– Written in ANSI C
– Runs anywhere
– Python scripts run on any Python runtime
Why Python ? ...
TM
TWINCLING
freedom of innovation
● Mixable
– Extend Python with Components written in C++, Java, C
– Embed python into your app and call it from C, C++
TWINCLING.org
– Python on windows supports COM
● Powerful
– Ease of use of scripting language
– Built in object types
– Extensive libraries
– Automatic memory management
– Modules, Classes and Exceptions
Why Python ? ...
TM
TWINCLING
freedom of innovation
● Robust
– Exception Handling
– Automatic memory management (Garbage Collection)
TWINCLING.org
– Dynamic Type Checking
– No unsafe pointers
● Dynamic
– Late binding language
– Add methods at runtime
– Call methods using reflection
Python features not there in Java
TM
TWINCLING
freedom of innovation
● syntactic sugar for lists
● syntactic sugar for maps/dictionaries
● raw strings
TWINCLING.org
● for loop shortcuts (=foreach)
● named method parameters
● string formatting shortcuts
List
TM
TWINCLING
freedom of innovation
Java
List list = new LinkedList();
list.add( new Integer( 1 ) );
list.add( new Integer( 2 ) );
list.add( new Integer( 3 ) );
TWINCLING.org
Python
list = [1, 2]
list.append( 3 )
Maps
TM
TWINCLING
freedom of innovation
Java
Map map = new HashMap();
map.put( "one", new Integer( 1 ) );
map.put( "two", new Integer( 2 ) );
map.put( "three", new Integer( 3 ) );
TWINCLING.org
System.out.println( map.get( "one" ) );
Python
map = { "one" : 1, "two" : 2, "three" : 3 }
print map[ "one" ]
Loops shortcut
TM
TWINCLING
freedom of innovation
Java
double sum = 0.0;
for( Iterator it=nums.iterator(); it.hasNext() )
{
sum += ((Double)it.next()).doubleValue();
}
TWINCLING.org
Python
sum = 0.0
for x in nums:
sum = sum + x
Named Method Parameters
TM
TWINCLING
freedom of innovation
Java
JFrame frame = new JFrame( "Servus" );
frame.setSize( new Dimension( 200, 200 ) );
frame.setVisible( true );
TWINCLING.org
Python
frame = JFrame( "Servus", visible=1, size=(200,200) )
String Formatting Shortcuts
TM
TWINCLING
freedom of innovation
Java
double x = 10000.0 / 3.0;
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMinimumFractionDigits( 2 );
nf.setMaximumFractionDigits( 2 );
String s = nf.format( x );
TWINCLING.org
for( int i = s.length(); i < 10; i++ )
System.out.print( ' ' );
System.out.print( s );
Python
x = 10000.0 / 3.0
print "%10.2f" % x
Raw strings
TM
TWINCLING
freedom of innovation
Java Python
"\\$\\d+,\\d+\\." r'\$\d+,\d+\.'
"\\s((::)(\\w+))\\b" r'\s((::)(\w+))\b'
"c:\\sandbox\\doc\\talk" r'\s((::)(\w+))\b'
TWINCLING.org
"Christina says, \"Python”“ 'Christina says, "Python”'
In Python you can use triplequotes (""") strings
for multiline text snippets without escaping newlines or single or double quotes
Raw String Especially Useful for Regular Expressions (Regex)
Data types
TM
TWINCLING
freedom of innovation
Java Python
● Boolean ● String
● Char ● Int
● Byte ● Long
TWINCLING.org
● Short ● Float
● Int ● complex
● Long
● Float
● double
Exception Handling
TM
TWINCLING
freedom of innovation
Java Python
FileInputStream in = file = open( name )
new FileInputStream( new File( try:
name ) );
process_file( file )
try
TWINCLING.org
finally:
{
file.close()
process_file( in );
}
finally
{
in.close();
}
Python class
TM
TWINCLING
freedom of innovation
Python lass DynProps: Dynamic Properties/Getters
def __init__( self, **args ):
self.props = args
def __getattr__( self, attribute ):
TWINCLING.org
return self.props[ attribute ]
capital = DynProps( austria="vienna", canada="ottawa" )
print capital.austria
capital.props[ "peru" ] = "lima"
print capital.peru
Embed Python in Java
TM
TWINCLING
freedom of innovation
import org.python.util.PythonInterpreter;
import org.python.core.*;
public class SimpleEmbedded
TWINCLING.org
{
public static void main( String args[] ) throws PyException
{
// create a phyton interpreter
PythonInterpreter interp = new PythonInterpreter();
// execute a statement
interp.exec( "import sys" );
interp.exec( "print sys" );
Embed Python in Java ...
TM
TWINCLING
freedom of innovation
// create a int variable n with the value 7
interp.set( "n", new PyInteger( 7 ));
// print value of n
TWINCLING.org
interp.exec( "print n" );
// assign value to new variable x
interp.exec( "x = 2+2" );
PyObject x= interp.get( "x" );
// print value of x
System.out.println( "x: " + x );
}
}
Python – C++ integration
TM
TWINCLING
freedom of innovation
● Boost.Python Library
● Key goals of the library
TWINCLING.org
– Reference / Pointer Support
– Globally Registered Type Coercions Dave Abrahams
– Full Cross module Support
– Improve Overloading support
– C++ to Python Exception Translation
– Default Argument Support
– Generic C++ to Python Object Interface
Python – C++ integration ...
TM
TWINCLING
freedom of innovation
● Key goals of the library
– Standard C++ algos to Python objects
– Python LONG support
TWINCLING.org
– Improved builtin Numeric Type Coercion
– Python Iterator support
– Automatic C++ object initialization
– DocString support
– C++ long long support
– Code Footprint Reduction
– Data memory Footprint Reduction
What is Python used for ?
TM
TWINCLING
freedom of innovation
● System Utilities
– system admin tools, portable shell scripts
● Internet Scripting
TWINCLING.org
– CGI scripts, parse HTML, process XML, email tools
● User Interfaces (Uis) rapid prototyping
● Component Glue
– scripting for apps, COM scripting
● Distributed Programming
– COM, CORBA, XMLRPC
What is Python used for ? ...
TM
TWINCLING
freedom of innovation
● Database Programming
– http://www.python.org/peps/pep-0249.html
● Image Processing
TWINCLING.org
– Python Image Library
http://www.pythonware.com/products/pil/
● OpenGL Programming, Writing Games
– PyOpenGL, py3d
● Artifical Intelligence
– http://www.strout.net/python/ai/
Python Resources
TM
TWINCLING
freedom of innovation
● http://www.python.org/
● http://wiki.python.org/moin/PythonBooks
● Boost Python – C++ tutorial
TWINCLING.org
http://www.boost.org/libs/python/doc/tutorial/doc/html/index.html
Special thanks
TM
TWINCLING
freedom of innovation
TWINCLING.org
AppLabs Technologies Pvt. Ltd.
Coordinating for TWINCLING Saturday Meet (TSM)
Providing LCD projector and Meeting space.
http://www.applabs.com/
About Us
TM
TWINCLING
freedom of innovation
TWINCLING.org
India's first, independent, "notforprofit",
OpenSource software development
& promotion society.
www.twincling.org
groups.yahoo.com/group/twincling