Python Core

Strings

Functions

Argument Lists

*args tuple/list

**kwargs dictionary

def foo(*args):
    print args

Unpacking

Tuples

Tuple assignment

a, b = b, a

Tuple comparison

Lists

Dictionaries

Sorting

from operator import itemgetter
d = {'spam':7, 'eggs': 14, 'ham':6}
#sort by key
print sorted(d.iteritems(), reverse=True)  # return a list of tuples
print sorted(d.keys(), reverse=True)  # return just a list of keys
# sort by value
print sorted(d.iteritems(), key=itemgetter(1), reverse=True)  #return a list of tuples
print sorted(d.iteritems(), key=lambda x: x[1], reverse=True)
print sorted(d, key=d.__getitem__, reverse=True)  #return just a list of values

Files

Regular Expressions

re.search(pat, str, flags=0) returns a match object or None otherwise.

re.findall(pat, str, flags=0) returns a list of all matches.

re.sub(pat, repl, str, count=0, flags=0) the replacement string can include \1, \2 which refer to the text from group(1), group(2) and so on from the original text:

str = 'spam foo@bar.com ham baz@qux.com eggs'
match = re.search(r'([\w.-]+)@([\w.-]+)', str)  # using raw string is convinient
if match:
    print 'match:', match.group()  # foo@bar.com
    print 'submatch:', match.group(1)  # foo
else:
    print 'not found'

print re.findall(r'[\w.-]+@[\w.-]+', str)  # ['foo@bar.com', 'baz@qux.com']
# return a list of tuples instead of strings submatches > 1
print re.findall(r'([\w.-]+)@([\w.-]+)', str)  # [('foo', 'bar.com'), ('baz', 'qux.com')]

print re.sub(r'([\w.-]+)@([\w.-]+)', r'\1@quxx.com', str)  # spam foo@quxx.com ham baz@quxx.com eggs

OOP

__init__

__str__

Operator Overloading

__add__ and __radd__

ref

Polymorphism

Inheritance

Standard Library