background preloader

Python

Facebook Twitter

Python — Basics of Python Dictionary: Looping & Sorting | YUJI TOMITA. Here some bits of info about python dictionaries & looping through them. Extra special beginner stuff. What is a dictionary? A python dictionary is an extremely useful data storage construct for storing and retreiving key:value pairs. Many languages implement simple arrays (lists in python) that are keyed by a integer. For example, if you made a list [1, 2] – the first value would be retrieved via [1, 2][0]. A dictionary is a little more advanced in that the keys can be many other things than integers. Will I remember that my_list[3] is my phone number? Major differences vs lists - Keys are any hashable object (say strings for simplicity) - Are NOT ordered (a list is by definition ordered) One way I like to think about them are as little variable containers.

In fact, variables are very much related to dictionaries! Watch and see: Looping through dictionaries Now, if you did a little experimenting, you would see that if you loop through a dictionary you loop through its keys. Conclusion Like this: Pythonforbeginners.com - Learn Python by Example. Bridge the Gap Between CS101 and CS253. If you are taking CS253 as a followup to CS101 there are a few Python concepts that you will encounter that were not covered in CS101. Because CS253 is designed to teach web engineering and not necessarily Python basics, it can be frustrating trying to learn the web concepts without an understanding of the fundamental Python code that makes it happen.

This tutorial presents content and links to resources that will help to "bridge the gap" between CS101 and CS253. Every good programmer knows how to learn on their own. Researching via google and the official documentation is vital. Asking good questions on the forums and in IRC channel cannot be underestimated as a learning tool. Contents Running Python on Your System You will want to install Python version 2. If you are using Mac or Linux you most likely already have Python on your system.

You can then execute Python code right in the interpreter for an immediate result: >>> def foo(a):... return a + 1... >>> x = 7>>> foo(x)8 >>> quit() $

Regular Expressions

Quickstart Guide to Using the Jinja2 Template Engine. What is Jinja 2 ? Jinja2 is the second major version of a Python library used to generate documents based on one or more predefined templates. One common use case is as part of the backend for web sites which need to create many similar pages. However, it can be used to produce any kind of text document, not merely those related to the web. This library was written by Armin Ronacher, with version 2.0 being first released in July of 2008. It is stable and largely feature complete. The newest version is 2.6, which was released in July of 2011. As to the name, "jinja" is the Japanese word for a Shinto shrine or temple. Advantages of Jinja Due to the fact that there are many templating engines for Python, one may legitimately wonder what makes Jinja stand apart.

Jinja has been used by prominent organizations, including Mozilla and SourceForge. Installation There are several ways to install Jinja. API Overview There are two key objects in the Jinja API: Environment and Template. <! Template Logic. Operators - What is the name of ** in python. [Tutor] how to understand unhashable type: 'list'

HowTo/Sorting. Original version by Andrew Dalke with a major update by Raymond Hettinger Python lists have a built-in sort() method that modifies the list in-place and a sorted() built-in function that builds a new sorted list from an iterable. There are many ways to use them to sort data and there doesn't appear to be a single, central place in the various manuals describing them, so I'll do so here. Sorting Basics A simple ascending sort is very easy -- just call the sorted() function. >>> sorted([5, 2, 3, 1, 4]) [1, 2, 3, 4, 5] You can also use the list.sort() method of a list. >>> a = [5, 2, 3, 1, 4] >>> a.sort() >>> a [1, 2, 3, 4, 5] Another difference is that the list.sort() method is only defined for lists.

Key Functions Starting with Python 2.4, both list.sort() and sorted() added a key parameter to specify a function to be called on each list element prior to making comparisons. For example, here's a case-insensitive string comparison: The same technique works for objects with named attributes. Python — Basics of Python Dictionary: Looping & Sorting | Useful Stuff. ‎www.cs.utexas.edu/users/arvindn/misc/knuth_song_complexity.pdf. Python tutorial. Code Like a Pythonista: Idiomatic Python. In this interactive tutorial, we'll cover many essential Python idioms and techniques in depth, adding immediately useful tools to your belt.

There are 3 versions of this presentation: ©2006-2008, licensed under a Creative Commons Attribution/Share-Alike (BY-SA) license. My credentials: I am a resident of Montreal,father of two great kids, husband of one special woman,a full-time Python programmer,author of the Docutils project and reStructuredText,an editor of the Python Enhancement Proposals (or PEPs),an organizer of PyCon 2007, and chair of PyCon 2008,a member of the Python Software Foundation,a Director of the Foundation for the past year, and its Secretary. In the tutorial I presented at PyCon 2006 (called Text & Data Processing), I was surprised at the reaction to some techniques I used that I had thought were common knowledge.

Many of you will have seen some of these techniques and idioms before. These are the guiding principles of Python, but are open to interpretation. Import this. Beautiful Soup: We called him Tortoise because he taught us. [ Download | Documentation | Hall of Fame | For enterprise | Source | Changelog | Discussion group | Zine ] You didn't write that awful page. You're just trying to get some data out of it. Beautiful Soup is here to help. Since 2004, it's been saving programmers hours or days of work on quick-turnaround screen scraping projects. Beautiful Soup is a Python library designed for quick turnaround projects like screen-scraping.

Beautiful Soup provides a few simple methods and Pythonic idioms for navigating, searching, and modifying a parse tree: a toolkit for dissecting a document and extracting what you need. Beautiful Soup parses anything you give it, and does the tree traversal stuff for you. Valuable data that was once locked up in poorly-designed websites is now within your reach. Interested? Getting and giving support If you have questions, send them to the discussion group. If you use Beautiful Soup as part of your work, please consider a Tidelift subscription. Download Beautiful Soup. Introduction to Computing : Explorations in Language, Logic, and Machines. PEP 8 -- Style Guide for Python Code. Code should be written in a way that does not disadvantage other implementations of Python (PyPy, Jython, IronPython, Cython, Psyco, and such).For example, do not rely on CPython's efficient implementation of in-place string concatenation for statements in the form a += b or a = a + b.

This optimization is fragile even in CPython (it only works for some types) and isn't present at all in implementations that don't use refcounting. In performance sensitive parts of the library, the ''.join() form should be used instead. This will ensure that concatenation occurs in linear time across various implementations.Comparisons to singletons like None should always be done with is or is not, never the equality operators.Also, beware of writing if x when you really mean if x is not None -- e.g. when testing whether a variable or argument that defaults to None was set to some other value.

The other value might have a type (such as a container) that could be false in a boolean context!