background preloader

Python

Facebook Twitter

Python Shortcuts for the Python Beginner - Max Burstein's Blog. Python Shortcuts for the Python Beginner (Posted on January 26th, 2013) The following are just a collection of some useful shortcuts and tools I've found in Python over the years.

Python Shortcuts for the Python Beginner - Max Burstein's Blog

Hopefully you find them helpful. Swapping Variables x = 6y = 5x, y = y, xprint x>>> 5print y>>> 6 Inline if Statement print "Hello" if True else "World">>> Hello Concatenations The last one is a pretty cool way to combine objects of two different types. nfc = ["Packers", "49ers"]afc = ["Ravens", "Patriots"]print nfc + afc>>> ['Packers', '49ers', 'Ravens', 'Patriots']print str(1) + " world">>> 1 worldprint `1` + " world">>> 1 worldprint 1, "world">>> 1 worldprint nfc, 1>>> ['Packers', '49ers'] 1 Number Tricks #Floor Division (rounds down)print 5.0//2>>> 2#2 raised to the 5th powerprint 2**5>> 32 Be careful with division and floating point numbers. print .3/.1>>> 2.9999999999999996print .3//.1>>> 2.0 Numerical Comparison This is a pretty cool shortcut that I haven't seen in too many languages.

List Comprehension Into this: Python: The Dictionary Playbook - Amir Rachum. I so often come across various kinds of boilerplate code regarding dictionaries in Python, that I decided to show some of it here and share the more concise way of performing the same operations. Presenting: The Dictionary Playbook . This one is pretty simple, but I’m amazed as to how it’s missed - finding out if a key exists in the dictionary.

The Lame Version dct . has_key ( key ) The Python Way key in dct For those programmers who master the “Are You There” play, there’s usually another simple, yet annoying behavior. Do This You Must Not not key in dct English, Do You Speak It? Key not in dct This one is really popular. The Boilerplate if key not in dct : dct [ key ] = 0 dct [ key ] = dct [ key ] + 1 The Awesome Way dct [ key ] = dct . get ( key , 0 ) + 1 dct . get ( key [, default ]) returns dct [ key ] if it exists, and default if not. The Even More Awesome If you’re using Python 2.7 and you want to count up amounts of stuff, you can use Counter .

And here’s a more complete example: Spelling It Out. 7 Python Libraries you should know about. In my years of programming in Python and roaming around GitHub's Explore section, I've come across a few libraries that stood out to me as being particularly enjoyable to use.

7 Python Libraries you should know about

This blog post is an effort to further spread that knowledge. I specifically excluded awesome libs like requests, SQLAlchemy, Flask, fabric etc. because I think they're already pretty "main-stream". If you know what you're trying to do, it's almost guaranteed that you'll stumble over the aforementioned. This is a list of libraries that in my opinion should be better known, but aren't. 1. pyquery (with lxml) pip install pyquery For parsing HTML in Python, Beautiful Soup is oft recommended and it does a great job. Just how slow? What immediately stands out is how fast lxml is. So either slow and easy to use or fast and hard to use, right? Wrong! Enter PyQuery Oh PyQuery you beautiful seductress: from pyquery import PyQuerypage = PyQuery(some_html) last_red_anchor = page('#container > a.red:last') Easy as pie. 2. dateutil.