Python tour


Published on

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.

1 Like
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Python tour

  1. 1. Introduction to Python A side-by-side comparison with JavaTamer Mohammed Abdul-RadiBackend Software Engineer at Cloud9ers ltd tamer_radi tamerradi
  2. 2. PythonDeveloped by Guido van Rossum during the1989 Christmas holidaysOpen sourceVery readableProgrammer friendly
  3. 3. Python VersionsPython 2.7 Python 3.1● Still widely used ● Present and Future● Have an extended of the language support ● Not backward● Django, Twisted, compatible and PIL supports ● Limited library Python 2.x only support ● Comparison was made on 31 May 2012, things may change later ● There are two tools "2to3" and "3to2" to convert scripts between versions
  4. 4. Python TourCompared to Java
  5. 5. Hello World Java Python 2.7public class Main { print "Hello World" public static void main(String[] args) { System.out.println("Hello World"); }} Python 3.x print("Hello World")
  6. 6. Lists Java Pythonimport java.util.Vector x = [a, b, c, d]public class Main { for item in x: public static void main(String[] args) { print item 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)); } }}
  7. 7. Lists vs Arrays Java Python Array Arrayint[] a = {1, 2, 3}; import arraySystem.out.println(a[0]) a = array.array(i,a[0] = 55 [1,2,3]) print a[0] List a[0] = 55List<Integer> a = new ArrayList<Integer>(); Lista.add(new Integer(1)) a = [1,2,3]System.out.println(a.get(0)) print a[0] a[0] = 55
  8. 8. Hashtables Java Pythonimport java.util.Hashtable x = {}public class Main { x[a] = 1 public static void main(String[] args) { print x[a] Hashtable x = new Hashtable(); x.put("a", new Integer(1)) System.out.println(x.get("a")); OR } x = {a : 1}} print x[a]
  9. 9. IO Java Pythonpublic class Main { f = open(file.txt, w) public static void main(String[] args) { f.write(Hello World) try { f.close() File f = new File(file.txt); PrintWriter ps = new PrintWriter( new OutputStreamWriter( new File OutputStream(f))); OR ps.print("Hello World") } catch (IOException e) { with open(file.txt, w) as f: e.printStackTrace(); f.write(Hello World) } }}
  10. 10. Classes Java Pythonpublic class Point { class Point(): public int x; def __init__(self, x, y) { public int y; self.x = x public MyClass(int x, int y) { self.y = y this.x = x } this.y = y p = Point(1, 2) } public static void main(String[] args) { p = new Point(1, 2) }}
  11. 11. Interesting Featuresin Python Programming Language
  12. 12. IntrospectionThe ability to examine something to determine● what it is● what it knows● what it is capable of doingIntrospection gives programmers a great dealof flexibility and control.Source:
  13. 13. Syntactic Sugar for very high level data structuresLists Tuplesl = [a, b, c, d] t = (a, b, c, d)print l[0] print t[0]l[0] = z StringsHashtables s = abcdd = {0:a, 100:b, 5:c} print s[0]print d[0]d[strings_too] = z Sets s = {0, 1, 2, 3}
  14. 14. Enhanced Forfor item in l: print itemfor char in s: There is no special type for chars; A char in python is print char string with length equals onefor item in t: print item
  15. 15. Enhanced Forfor key in d: print keyfor value in d.values(): print valuefor key, value in d.items(): print key, value
  16. 16. Positional & Keyword Argumentsdef my_function(a, b, c, d): print a, b, c, dmy_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)
  17. 17. 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!
  18. 18. Things you can kiss goodbye● Curly Braces to define Blocks● Semicolons (optional)● Switch case● Classic For Loops● Interfaces● Checked Exceptions
  19. 19. Things you wont miss in Python● Simplicity● Readability● True OOP● Fun!
  20. 20. Things Python sucks at● Lots of mathematical computationsYou can write the computation code in C ,orC++ and call it from Python!● Using multithreading with mutli-cores or CPUsYou can multiprocessing rather than multi-threading
  21. 21. 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)