Python Intro Created by Dave Park Copyright 2011 Points International, Ltd. python >>> 'Getting to the essence of your cod...
lost in a sea of layers...
>>> "Where's the good stuff?" >>> "Ceremony vs. Essence"
Sorting Lists - Java public static void sortByLastTrade(List<Stock> portfolio) {    Comparator<Stock> c = new Comparator<S...
Sorting Lists - Python >>> portfolio_tuple = [ ('APPL', 203.25, 204.50), ('MSFT', 29.12, 29.08), ('ORCL', 21.90, 21.83) ] ...
>>> ['indents', 'lists', 'dictionaries', 'objects', 'etc...']
Indents <ul><li>public class IndentExample { </li></ul><ul><li>   private boolean indent; </li></ul><ul><li>   public void...
Indents <ul><li>public class IndentExample  { </li></ul><ul><li>   private boolean indent; </li></ul><ul><li>   public voi...
Code Style  <ul><ul><li>4 spaces per indentation level. </li></ul></ul><ul><ul><li>No hard tabs. </li></ul></ul><ul><ul><l...
Indents <ul><li>def fib(n):                    </li></ul><ul><li>     print 'n =', n             </li></ul><ul><li>     if...
Indents <ul><li>def fib(n):                     </li></ul><ul><li>.... print 'n =', n              </li></ul><ul><li>.... ...
REPL <ul><li>Read Evaluate Print Loop. </li></ul><ul><li>Like a lot of other languages, Python has a REPL for quick and ea...
Lists, Dictionaries <ul><li># defining a list </li></ul><ul><li>beatles = ['john', 'paul', 'george', 'ringo'] </li></ul><u...
List Comprehensions <ul><li>>>> freshfruit = ['  banana', '  loganberry ', 'passion fruit  '] </li></ul><ul><li>>>> [fruit...
Objects <ul><li>import urllib </li></ul><ul><li>import httplib </li></ul><ul><li>from urlparse import urlparse </li></ul><...
Built In Functions <ul><li>>>> len([1,2,3]) </li></ul><ul><li>3 </li></ul><ul><li>>>> len('dave park is cool') </li></ul><...
More Stuff (in brief) <ul><li>Resources </li></ul>Frameworks/Tools <ul><ul><li>Official Python Docs -  http://docs.python....
Upcoming SlideShare
Loading in …5
×

Python - Getting to the Essence - Points.com - Dave Park

1,444 views

Published on

Intro to Python. Created by Dave Park at Points.com.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,444
On SlideShare
0
From Embeds
0
Number of Embeds
397
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • A tuple is a heterogeneous  immutable list
  • controversy!
  • Python - Getting to the Essence - Points.com - Dave Park

    1. 1. Python Intro Created by Dave Park Copyright 2011 Points International, Ltd. python >>> 'Getting to the essence of your code.'
    2. 2. lost in a sea of layers...
    3. 3. >>> &quot;Where's the good stuff?&quot; >>> &quot;Ceremony vs. Essence&quot;
    4. 4. Sorting Lists - Java public static void sortByLastTrade(List<Stock> portfolio) {   Comparator<Stock> c = new Comparator<Stock>() {     public int compare(Stock o1, Stock o2) {       return o1.getLastTrade() - o2.getLastTrade();     }   };   Collections.sort(portfolio, c); } public static void sortByOpen(List<Stock> portfolio) {   Comparator<Stock> c = new Comparator<Stock>() {     public int compare(Stock o1, Stock o2) {       return o1.getOpen() - o2.getOpen();     }   };   Collections.sort(portfolio, c); }
    5. 5. Sorting Lists - Python >>> portfolio_tuple = [ ('APPL', 203.25, 204.50), ('MSFT', 29.12, 29.08), ('ORCL', 21.90, 21.83) ] >>> sorted(portfolio_tuple, key=lambda stock: stock[1])   # sort by last Trade [('ORCL', 21.90, 21.83), ('MSFT', 29.12, 29.08), ('APPL', 203.25, 204.50)] >>> sorted(portfolio_tuple, key=lambda stock: stock[2])   # sort by last Open [('ORCL', 21.90, 21.83), ('MSFT', 29.12, 29.08), ('APPL', 203.25, 204.50)]
    6. 6. >>> ['indents', 'lists', 'dictionaries', 'objects', 'etc...']
    7. 7. Indents <ul><li>public class IndentExample { </li></ul><ul><li>  private boolean indent; </li></ul><ul><li>  public void doSomething( Date asOf ) { </li></ul><ul><li>    Date now = new Date(); </li></ul><ul><li>    if( asOf.getTime() < now.getTime() ) { </li></ul><ul><li>      System.out.println( &quot;We are doing something&quot; ); </li></ul><ul><li>    } </li></ul><ul><li>  } </li></ul><ul><li>} </li></ul>
    8. 8. Indents <ul><li>public class IndentExample { </li></ul><ul><li>  private boolean indent; </li></ul><ul><li>  public void doSomething( Date asOf ) { </li></ul><ul><li>    Date now = new Date(); </li></ul><ul><li>    if( asOf.getTime() < now.getTime() ) { </li></ul><ul><li>      System.out.println( &quot;We are doing something&quot; ); </li></ul><ul><li>    } </li></ul><ul><li>  } </li></ul><ul><li>} </li></ul>
    9. 9. Code Style  <ul><ul><li>4 spaces per indentation level. </li></ul></ul><ul><ul><li>No hard tabs. </li></ul></ul><ul><ul><li>Never mix tabs and spaces. </li></ul></ul><ul><ul><li>One blank line between functions. </li></ul></ul><ul><ul><li>Two blank lines between classes. </li></ul></ul>
    10. 10. Indents <ul><li>def fib(n):                    </li></ul><ul><li>    print 'n =', n             </li></ul><ul><li>    if n > 1:                  </li></ul><ul><li>        return n * fib(n - 1) </li></ul><ul><li>    else:                      </li></ul><ul><li>        print 'end of the line' </li></ul><ul><li>        return 1 </li></ul>
    11. 11. Indents <ul><li>def fib(n):                    </li></ul><ul><li>.... print 'n =', n             </li></ul><ul><li>.... if n > 1:                  </li></ul><ul><li>........ return n * fib(n - 1) </li></ul><ul><li>.... else:                      </li></ul><ul><li>........ print 'end of the line' </li></ul><ul><li>........ return 1 </li></ul>
    12. 12. REPL <ul><li>Read Evaluate Print Loop. </li></ul><ul><li>Like a lot of other languages, Python has a REPL for quick and easy experimentation. </li></ul>
    13. 13. Lists, Dictionaries <ul><li># defining a list </li></ul><ul><li>beatles = ['john', 'paul', 'george', 'ringo'] </li></ul><ul><li># looping over a list </li></ul><ul><li>for member in beatles:  </li></ul><ul><li>    print member </li></ul><ul><li># in conditional </li></ul><ul><li>if 'paul' in beatles:  </li></ul><ul><li>    print 'list contains', 'paul' </li></ul><ul><li># compare against values, not references </li></ul><ul><li>if beatles == ['mick', 'keith']: </li></ul><ul><li># defining a dictionary </li></ul><ul><li>also_known_as_map = {'singer':'john', 'bass':'paul', 'guitar':'george', 'drummer':'ringo'} </li></ul><ul><li># grabbing a value based on key </li></ul><ul><li>also_known_as_map['singer'] </li></ul>
    14. 14. List Comprehensions <ul><li>>>> freshfruit = ['  banana', '  loganberry ', 'passion fruit  '] </li></ul><ul><li>>>> [fruit.strip() for fruit in freshfruit] </li></ul><ul><li>['banana', 'loganberry', 'passion fruit'] </li></ul><ul><li>>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'} </li></ul><ul><li>>>> for k, v in knights.iteritems(): </li></ul><ul><li>...     print k, v </li></ul><ul><li>... </li></ul><ul><li>gallahad the pure </li></ul><ul><li>robin the brave </li></ul>
    15. 15. Objects <ul><li>import urllib </li></ul><ul><li>import httplib </li></ul><ul><li>from urlparse import urlparse </li></ul><ul><li>  </li></ul><ul><li>class PieService: </li></ul><ul><li>    def __init__(self): </li></ul><ul><li>        self._headers = {&quot;Content-type&quot;: &quot;application/x-www-form-urlencoded&quot;} </li></ul><ul><li>    def flush_cache(self,url): </li></ul><ul><li>        params = {} </li></ul><ul><li>        return self.make_request(url + '/flushcache', params, &quot;GET&quot; ) </li></ul><ul><li>  </li></ul><ul><li>    def start_transfer(self,url,domain,source,service_code,data):       </li></ul><ul><li>        params = { 'Domain': domain, 'Source': source, 'ServiceCode': service_code, 'Data': data } </li></ul><ul><li>        return self.make_request(url + '/start', params, &quot;POST&quot;) </li></ul><ul><li>   </li></ul><ul><li>>>> my_pie = PieService() </li></ul><ul><li>>>> my_pie.flush_cache( 'http://localhost:45601/pieshell/' ) </li></ul><ul><li>Woosh...ClientTM[primary-pie+192.168.253.148:48013+pie+t3+] </li></ul>
    16. 16. Built In Functions <ul><li>>>> len([1,2,3]) </li></ul><ul><li>3 </li></ul><ul><li>>>> len('dave park is cool') </li></ul><ul><li>17 </li></ul><ul><li>>>> sum([1,2,3,4,5,6,7,8,9,10]) </li></ul><ul><li>55 </li></ul><ul><li>>>> help(sum) </li></ul><ul><li>Help on built-in function sum in module __builtin__: </li></ul><ul><li>sum(...) </li></ul><ul><li>    sum(sequence[, start]) -> value </li></ul><ul><li>     </li></ul><ul><li>    Returns the sum of a sequence of numbers (NOT strings) plus the value </li></ul><ul><li>    of parameter 'start' (which defaults to 0).  When the sequence is </li></ul><ul><li>    empty, returns start. </li></ul><ul><li>(END)  </li></ul>
    17. 17. More Stuff (in brief) <ul><li>Resources </li></ul>Frameworks/Tools <ul><ul><li>Official Python Docs -  http://docs.python.org/ </li></ul></ul><ul><ul><li>pip </li></ul></ul><ul><ul><li>virutal_env </li></ul></ul><ul><ul><li>django </li></ul></ul><ul><ul><li>flask </li></ul></ul><ul><ul><li>robot </li></ul></ul><ul><ul><li>ipython </li></ul></ul><ul><ul><li>PyDev - Eclipse plugin </li></ul></ul><ul><ul><li>PyCharm - Jetbrains plugin </li></ul></ul>

    ×