Python: Programming Language
Python: Programming Language
Python: Programming Language
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++
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
Python
print 'Servus Austria'
Java + Swing
TM
TWINCLING
freedom of innovation
TWINCLING
freedom of innovation
getContentPane().add( button );
}
TWINCLING
freedom of innovation
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
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
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
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 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
// print value of n
TWINCLING.org
// 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
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