21
Introduction to Python A side-by-side comparison with Java Tamer Mohammed Abdul-Radi Backend Software Engineer at Cloud9ers ltd tamer_radi tamerradi

Python tour

Embed Size (px)

DESCRIPTION

This presentation contains a quick tour in Python world. First by By comparing Java code, and the equivalent Python side by side, Second by listing some cool features in Python, finally by listing downs and ups of Python in usage; when to use python and when not.

Citation preview

Page 1: Python tour

Introduction to PythonA side-by-side comparison with Java

Tamer Mohammed Abdul-Radi Backend Software Engineer at Cloud9ers ltd tamer_radi tamerradi

Page 2: Python tour

Python

Developed by Guido van Rossum during the 1989 Christmas holidaysOpen sourceVery readableProgrammer friendly

Page 3: Python tour

Python Versions

Python 2.7● Still widely used● Have an extended

support● Django, Twisted,

and PIL supports Python 2.x only

Python 3.1● Present and Future

of the language● Not backward

compatible● Limited library

support

● Comparison was made on 31 May 2012, things may change later● There are two tools "2to3" and "3to2"' to convert scripts between versions

Page 4: Python tour

Compared to Java

Python Tour

Page 5: Python tour

Hello World

Javapublic class Main { public static void main(String[] args) { System.out.println("Hello World"); }}

Python 2.7print "Hello World"

Python 3.xprint("Hello World")

Page 6: Python tour

Lists

Javaimport java.util.Vectorpublic class Main { public static void main(String[] args) { Vector x = new Vector();

x.addElement("a") x.addElement("b") x.addElement("c") x.addElement("d") for (int i = 0 ; i < x.length; i++) { System.out.println(x.elementAt(i)); } }}

Pythonx = ['a', 'b', 'c', 'd']for item in x: print item

Page 7: Python tour

Lists vs Arrays

Java

Arrayint[] a = {1, 2, 3};System.out.println(a[0])a[0] = 55

ListList<Integer> a = new ArrayList<Integer>();a.add(new Integer(1))System.out.println(a.get(0))

Python

Arrayimport arraya = array.array('i', [1,2,3])print a[0]a[0] = 55

Lista = [1,2,3]print a[0]a[0] = 55

Page 8: Python tour

Hashtables

Javaimport java.util.Hashtablepublic class Main { public static void main(String[] args) { Hashtable x = new Hashtable();

x.put("a", new Integer(1)) System.out.println(x.get("a")); }}

Pythonx = {}x['a'] = 1print x['a']

OR x = {'a' : 1}print x['a']

Page 9: Python tour

IOJava

public class Main { public static void main(String[] args) { try {

File f = new File('file.txt'); PrintWriter ps = new PrintWriter(

new OutputStreamWriter( new File OutputStream(f))); ps.print("Hello World") } catch (IOException e) { e.printStackTrace();

} }}

Pythonf = open('file.txt', 'w')f.write('Hello World')f.close()

OR with open('file.txt, 'w') as f:

f.write('Hello World')

Page 10: Python tour

ClassesJava

public class Point { public int x; public int y; public MyClass(int x, int y) { this.x = x this.y = y } public static void main(String[] args) { p = new Point(1, 2) }}

Pythonclass Point(): def __init__(self, x, y) { self.x = x self.y = y }p = Point(1, 2)

Page 11: Python tour

in Python Programming Language

Interesting Features

Page 12: Python tour

Introspection

The ability to examine something to determine● what it is● what it knows● what it is capable of doingIntrospection gives programmers a great deal of flexibility and control.

Source: http://www.ibm.com/developerworks/library/l-pyint/index.html

Page 13: Python tour

Syntactic Sugar for very high level data structures

Listsl = ['a', 'b', 'c', 'd'] print l[0]l[0] = 'z' Hashtablesd = {0:'a', 100:'b', 5:'c'}print d[0]d['strings_too'] = 'z'

Tuplest = ('a', 'b', 'c', 'd')print t[0] Stringss = 'abcd'print s[0] Setss = {0, 1, 2, 3}

Page 14: Python tour

Enhanced For

for item in l: print item for char in s: print char for item in t: print item

There is no special type for chars; A char in python is string with length equals one

Page 15: Python tour

Enhanced For

for key in d: print key for value in d.values(): print value for key, value in d.items(): print key, value

Page 16: Python tour

Positional & Keyword Arguments

def my_function(a, b, c, d): print a, b, c, d my_function(1, 2, 3, 4)my_function(a=1, b=2, c=3, d=4)

my_function(1, b=2, c=3, d=4)my_function(1, 2, c=3, d=4)my_function(1, 2, d=4, c=3)

Page 17: Python tour

OOP

● Everything is an object● No Primitives, integers are objects● Functions are objects,

○ you can send a function as parameter to another function

○ you can have a list of functions● Classes are objects!

Page 18: Python tour

Things you can kiss goodbye

● Curly Braces to define Blocks● Semicolons (optional)● Switch case● Classic For Loops● Interfaces● Checked Exceptions

Page 19: Python tour

Things you won't miss in Python

● Simplicity● Readability● True OOP● Fun!

Page 20: Python tour

Things Python sucks at

● Lots of mathematical computations

● Using multithreading with mutli-cores or CPUs

You can write the computation code in C ,or C++ and call it from Python!

You can multiprocessing rather than multi-threading

Page 21: Python tour

What is Python good for?

● String processing (regular expressions, Unicode, calculating differences between files)

● Internet protocols (HTTP, FTP, SMTP, XML-RPC, POP, IMAP, CGI programming)

● Software engineering (unit testing, logging, profiling, parsing Python code)

● Operating system interfaces (system calls, file systems, TCP/IP sockets)