Date post: | 19-May-2015 |
Category: |
Entertainment & Humor |
Upload: | tamer-radi |
View: | 1,666 times |
Download: | 0 times |
Introduction to PythonA side-by-side comparison with Java
Tamer Mohammed Abdul-Radi Backend Software Engineer at Cloud9ers ltd tamer_radi tamerradi
Python
Developed by Guido van Rossum during the 1989 Christmas holidaysOpen sourceVery readableProgrammer friendly
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
Compared to Java
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")
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
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
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']
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')
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)
in Python Programming Language
Interesting Features
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
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}
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
Enhanced For
for key in d: print key for value in d.values(): print value for key, value in d.items(): print key, value
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)
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!
Things you can kiss goodbye
● Curly Braces to define Blocks● Semicolons (optional)● Switch case● Classic For Loops● Interfaces● Checked Exceptions
Things you won't miss in Python
● Simplicity● Readability● True OOP● Fun!
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
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)