background preloader

Python

Facebook Twitter

Scientific Python Lectures. Learn Python Through Public Data Hacking. How not to write Python code. Lately I’ve been reading some rather unclean Python code. Maybe this is mainly because the author(s) of the code had no in-depth knowledge of the Python language itself, the ‘platform’ delivered with cPython,… Here’s a list of some of the mistakes you should really try to avoid when writing Python code: Some days ago RealNitro pointed me at this list of essential Python readings.

“Idiomatic Python” is a must-read, even for experienced Python developers. That’s about it for now, maybe I’ll add some more items to this list later on. If you have some other hints, comments! Posted in Development, Technology. Tagged with Development, python. By Nicolas – February 8, 2008. Pandas. Learn Python. Understanding Python's "with" statement. Fredrik Lundh | October 2006 | Originally posted to online.effbot.org Judging from comp.lang.python and other forums, Python 2.5’s new with statement (dead link) seems to be a bit confusing even for experienced Python programmers. As most other things in Python, the with statement is actually very simple, once you understand the problem it’s trying to solve. Consider this piece of code: set things up try: do something finally: tear things down Here, “set things up” could be opening a file, or acquiring some sort of external resource, and “tear things down” would then be closing the file, or releasing or removing the resource.

If you do this a lot, it would be quite convenient if you could put the “set things up” and “tear things down” code in a library function, to make it easy to reuse. Def controlled_execution(callback): set things up try: callback(thing) finally: tear things down def my_function(thing): do something controlled_execution(my_function) This wasn’t very difficult, was it? The Python “with” Statement by Example.

Python’s with statement was first introduced five years ago, in Python 2.5. It’s handy when you have two related operations which you’d like to execute as a pair, with a block of code in between. The classic example is opening a file, manipulating the file, then closing it: with open('output.txt', 'w') as f: f.write('Hi there! ') The above with statement will automatically close the file after the nested block of code. Here’s another example. This code sample uses a Context object (“cairo context”) to draw six rectangles, each with a different rotation.

Cr.translate(68, 68) for i in xrange(6): cr.save() cr.rotate(2 * math.pi * i / 6) cr.rectangle(-25, -60, 50, 40) cr.stroke() cr.restore() That’s a fairly simple example, but for larger scripts, it can become cumbersome to keep track of which save goes with which restore, and to keep them correctly matched. By themselves, pycairo’s save and restore methods do not support the with statement, so we’ll have to add the support on our own. 10 Resources to Learn Python Programming Language.

Scrapy | An open source web scraping framework for Python. Python Resources for Data Science. Learn Python The Hard Way, 2nd Edition — Learn Python The Hard Way, 2nd Edition. Python for Informatics: Exploring Information. Think Python: How to Think Like a Computer Scientist. How to Think Like a Computer Scientist by Allen B.

Downey This is the first edition of Think Python. It uses Python 2, with notes on differences in Python 3. Buy this book at Amazon.com Download Think Python in PDF. Read Think Python in HTML. Example programs and solutions to some problems are here (links to specific examples are in the book). Description Think Python is an introduction to Python programming for beginners. Some examples and exercises are based on Swampy, a Python package written by the author to demonstrate aspects of software design, and to give readers a chance to experiment with simple graphics and animation.

Think Python is a Free Book. If you have comments, corrections or suggestions, please send me email at feedback{at}thinkpython{dot}com. Other Free Books by Allen Downey are available from Green Tea Press. Download Precompiled copies of the book are available in PDF. Python 3.0 Most of the book works for Python 2.x and 3.0. Michael Kart at St. Earlier Versions. Quantities and Units in Python.

Scientific Python Lectures. Book. Natural Language Processing with Python – Analyzing Text with the Natural Language Toolkit Steven Bird, Ewan Klein, and Edward Loper This version of the NLTK book is updated for Python 3 and NLTK 3. The first edition of the book, published by O'Reilly, is available at (There are currently no plans for a second edition of the book.) 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

Bibliography Term Index This book is made available under the terms of the Creative Commons Attribution Noncommercial No-Derivative-Works 3.0 US License. Learning Python, 5th Edition. PythonMonk - Interactive Python tutorials. NPR Puzzle: Finding Synonyms with Python and WordNet | Data Dork. This week’s puzzle asks: From Alan Meyer of Newberg, Ore.: Think of a common word that’s six letters long and includes a Q. Change the Q to an N, and rearrange the result to form a new word that’s a synonym of the first one. What are the words? This puzzle is a good opportunity to play with some very cool computational language tools available through the Natural Langauge Toolik (NLTK). NLTK is a group of libraries and functions that contain powerful tools for symbolic and statistical natural language processing (NLP). Check out the free NLTK book and go through the first chapter and the examples and tutorials will blow your mind.

Be forewarned and/or foredelighted that the book contains heavy amounts of linguistic jargon, stats, and programming. Before we get to using the NLTK, let’s break down this puzzle into multiple steps. 1. Approach: 1. 2. 3. This is best demonstrated through example and the use of one our language’s most versatile words – shit. Like this: Like Loading... Text Processing in Python (a book) A couple of you make donations each month (out of about a thousand of you reading the text each week). Tragedy of the commons and all that... but if some more of you would donate a few bucks, that would be great support of the author. In a community spirit (and with permission of my publisher), I am making my book available to the Python community. Minor corrections can be made to later printings, and at the least errata noted on this website.

Email me at <mertz@gnosis.cx> . A few caveats: (1) This stuff is copyrighted by AW (except the code samples which are released to the public domain).