Mastering machine learning with python courses
Python Programming Language
What is Python
● Objectoriented
● Dynamic
● Easy to learn syntax
● Highlevel data types
● Scripting language
● Embeddable in C/C++
Guido Von Rossum
Python vs Java (or C++)
● No compilation
– Fast editbuilddebug cycle
● Dynamic Typing
– No need to declare variables for use
● Easy Syntax
– No curly braces, no semicolons, no new
Python vs Java (or C++)
● Embeddable
– Scripting support for your applications
● Interactive
– Create, view, change objects at runtime
● 50% less code
– Compact and natural syntax
● 300% more productive
– Closer to the way you think
Hello World
Java
public class HelloWorld { public static void main( String args[] ) {
.println( "Hello World" );
}
}
Python print 'Servus Austria'
Java + Swing
Java public class HelloSwing extends JFrame
{ public HelloSwing( String title ) { super( title );
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
Java public void actionPerformed( ActionEvent ev )
{ exit(); } } );
getContentPane().add( button ); }
public void exit(){ ( 0 ); }
public static void main( String args[] ){ HelloSwing frame = new HelloSwing( "Python" ); (); (); }
}
Python + XUL
Python import as lang
def exit( event ): (0)
Why Python ?
● One language can not do it all
– Single purpose scripting languages on the rise
● XHTML
● 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 ?
●Objectoriented
– Ideal for scripting
– Ideal for creating gluing together components written in 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 ?
●Mixable
– Extend Python with Components written in C++, Java, C
– Embed python into your app and call it from C, C++
– 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 ?
●Robust
– Exception Handling
– Automatic memory management (Garbage Collection)
– Dynamic Type Checking
– No unsafe pointers
●Dynamic
– Late binding language
– Add methods at runtime
– Call methods using reflection
Python features not there in Java
● syntactic sugar for lists
● syntactic sugar for maps/dictionaries
● raw strings
● for loop shortcuts (=foreach)
● named method parameters
● string formatting shortcuts
List
Java
List list = new LinkedList(); ( new Integer( 1 ) ); ( new Integer( 2 ) ); ( new Integer( 3 ) );
Python
list = [1, 2] list.append( 3 )
Maps
Java
Map map = new HashMap(); ( "one", new Integer( 1 ) ); ( "two", new Integer( 2 ) ); ( "three", new Integer( 3 ) ); .println( ( "one" ) );
Python
map = { "one" : 1, "two" : 2, "three" : 3 } print map[ "one" ]
Loops shortcut
Java
double sum = 0.0; for( Iterator it=nums.iterator(); it.hasNext() ) { sum += ((Double)()).doubleValue(); }
Python
sum = 0.0 for x in nums: sum = sum + x
Named Method Parameters
Java
JFrame frame = new JFrame( "Servus" ); frame.setSize( new Dimension( 200, 200 ) ); frame.setVisible( true );
Python frame = JFrame( "Servus", visible=1, size=(200,200) )
String Formatting Shortcuts
Java
double x = 10000.0 / 3.0; NumberFormat nf = NumberFormat.getNumberInstance(); nf.setMinimumFractionDigits( 2 ); nf.setMaximumFractionDigits( 2 ); String s = nf.format( x ); for( int i = s.length(); i < 10; i++ ) .print( ' ' );
.print( s );
Python
x = 10000.0 / 3.0 print "%10.2f" % x
Raw strings
Java Python
"\\$\\d+,\\d+\\." r'\$\d+,\d+\.'
"\\s((::)(\\w+))\\b" r'\s((::)(\w+))\b'
"c:\\sandbox\\doc\\talk" r'\s((::)(\w+))\b'
"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
Java Python
● Boolean ● String
● Char ● Byte ● Short ● Int | ● Int ● Long ● Float ● complex |
● Long ● Float
● double
Exception Handling
JavaPython
FileInputStream in = file = open( name )
new FileInputStream( new File( try: name ) );
process_file( file )
try finally:
{
file.close()
process_file( in );
} finally
{
in.close();
}
Python class
Pythonlass DynProps: Dynamic Properties/Getters
def __init__( self, **args ): self.props = args
def __getattr__( self, attribute ): return self.props[ attribute ]
capital = DynProps( austria="vienna", canada="ottawa" )
print capital.austria capital.props[ "peru" ] = "lima" print
Embed Python in Java
import .PythonInterpreter; import .*;
public class SimpleEmbedded { public static void main( String args[] ) throws PyException {
// create a phyton interpreter
PythonInterpreter interp = new PythonInterpreter();
// execute a statement ( "import sys" ); ( "print sys" );
Embed Python in Java
// create a int variable n with the value 7 ( "n", new PyInteger( 7 )); // print value of n ( "print n" );
// assign value to new variable x ( "x = 2+2" ); PyObject x= ( "x" );
// print value of x
.println( "x: " + x );
}
}
Python – C++ integration
● Boost.Python Library
● Key goals of the library
– 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
● Key goals of the library
– Standard C++ algos to Python objects
– Python LONG support
– 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 ?
● System Utilities
– system admin tools, portable shell scripts ● Internet Scripting
– 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 ?
● Database Programming
–
● Image Processing
– Python Image Library
● OpenGL Programming, Writing Games
– PyOpenGL, py3d
● Artifical Intelligence
–
Python Resources
● Boost Python – C++ tutorial
Special thanks
AppLabs Technologies Pvt. Ltd.
Coordinating for TWINCLING Saturday Meet (TSM) Providing LCD projector and Meeting space.
About Us
India's first, independent, "notforprofit",
OpenSource software development
& promotion society.