Python tour
Upcoming SlideShare
Loading in...5
×
 

Python tour

on

  • 1,689 views

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 ...

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.

Statistics

Views

Total Views
1,689
Views on SlideShare
1,238
Embed Views
451

Actions

Likes
1
Downloads
13
Comments
5

4 Embeds 451

http://abdulradi.com 440
http://localhost 6
http://us-w1.rockmelt.com 3
http://tabdulradi.tumblr.com 2

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

15 of 5 Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Python tour Python tour Presentation Transcript

    • Introduction to Python A side-by-side comparison with JavaTamer Mohammed Abdul-RadiBackend Software Engineer at Cloud9ers ltd tamer_radi tamerradi
    • PythonDeveloped by Guido van Rossum during the1989 Christmas holidaysOpen sourceVery readableProgrammer friendly
    • 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
    • Python TourCompared to Java
    • 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")
    • 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)); } }}
    • 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
    • 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]
    • 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) } }}
    • 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) }}
    • Interesting Featuresin Python Programming Language
    • 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: http://www.ibm.com/developerworks/library/l-pyint/index.html
    • 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}
    • 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
    • Enhanced Forfor key in d: print keyfor value in d.values(): print valuefor key, value in d.items(): print key, value
    • 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)
    • 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 wont miss in Python● Simplicity● Readability● True OOP● Fun!
    • 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
    • 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)